簡體   English   中英

Raspberry Pi GPIO引腳不會切換

[英]Raspberry pi GPIO pins wont switch

所以我有一個有趣的問題。 我寫了一個程序來控制房間的氣候。 該程序作為python程序運行,並具有Web界面。 該程序運行良好,但是如果我重新啟動pi,則所有GPIO引腳都會打開。 然后,我運行我的python程序,直到我轉到開關頁面,它們才會關閉。 正確的是,在加載所有命令時,請打開和關閉引腳,程序才能正常運行。 在啟動后我加載該頁面一次后,它不再影響程序,並且使pin命令不再掛起。

這是該頁面的代碼。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<html>

<head>
    <link rel="shortcut icon" href="icon.png" /> </head>

<head>
    <html>

    <head>
        <title>Grow Hub</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
        <style>
            .jumbotron {
                margin-bottom: 25px;
                height: 130px;
                border-bottom: 2px solid black;
            }

            .container {
                width: 100%;
            }
        </style>
    </head>

    <body>

        <body>
            <div class="jumbotron">
                <div class="container">
                    <?php include 'menu.php';?>
                        <center>
                </div>
            </div>
            <div class="container">
                <center>
                    <!-- On/Off button's picture -->
                    <?php    //this php script generate the first page in function of the gpio's status      $status = array(0,2,3,4,5,6,21,22);     $pins = array(0,2,3,4,5,6,21,22);   for ($i = 0; $i < count($status); $i++) {      $pin = $pins[$i];       //set the pin's mode to output and read them        system("gpio mode ".$pin." out");       exec ("gpio read ".$pin, $status[$i], $return );        //if off        if ($status[$i][0] == 0 ) {         echo ("<img id='button_".$pin."' src='data/img/red/red_".$pin.".png' alt='off'/><br>");         }       //if on         if ($status[$i][0] == 1 ) {         echo ("<img id='button_".$pin."' src='data/img/green/green_".$pin.".png' alt='on'/><br>");      }        }   ?>
            </div>
            <!-- javascript -->
            <script src="script.js"></script>
            <div class="container"> </div>
            </center>
        </body>

    </html>

這是script.js的代碼

//JavaScript, use pictures as buttons, sends and receives values to/from the Rpi //These are all the buttons
var button_0 = document.getElementById("button_0");
var button_2 = document.getElementById("button_2");
var button_3 = document.getElementById("button_3");
var button_4 = document.getElementById("button_4");
var button_5 = document.getElementById("button_5");
var button_6 = document.getElementById("button_6");
var button_21 = document.getElementById("button_21");
var button_22 = document.getElementById("button_22");
//this function sends and receives the pin's status
function change_pin(pin, status) { //this is the http request
        var request = new XMLHttpRequest();
        request.open("GET", "gpio.php?pin=" + pin + "&status=" + status);
        request.send(null); //receiving information
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                return (parseInt(request.responseText));
            } //test if fail
            else if (request.readyState == 4 && request.status == 500) {
                alert("server error");
                return ("fail");
            } //else        
            else {
                return ("fail");
            }
        }
    } //these are all the button's events, it just calls the change_pin function and updates the page in function of the return of it.

button_0.addEventListener("click", function() {
    //if red 
    if (button_0.alt === "off") {
        //use the function  
        var new_status = change_pin(0, 0);
        if (new_status !== "fail") {
            button_0.alt = "on"
            button_0.src = "data/img/green/green_0.png";
            return 0;
        }
    } //if green
    if (button_0.alt === "on") {
        //use the function      
        var new_status = change_pin(0, 1);
        if (new_status !== "fail") {
            button_0.alt = "off"
            button_0.src = "data/img/red/red_0.png";
            return 0;
        }
    }
});
button_2.addEventListener("click", function() {
    //if red 
    if (button_2.alt === "off") {
        //use the function  
        var new_status = change_pin(2, 0);
        if (new_status !== "fail") {
            button_2.alt = "on"
            button_2.src = "data/img/green/green_2.png";
            return 0;
        }
    }
    //if green  
    if (button_2.alt === "on") {
        //use the function  
        var new_status = change_pin(2, 1);
        if (new_status !== "fail") {
            button_2.alt = "off"
            button_2.src = "data/img/red/red_2.png";
            return 0;
        }
    }
});
button_3.addEventListener("click", function() {
    //if red 
    if (button_3.alt === "off") {
        //use the function  
        var new_status = change_pin(3, 0);
        if (new_status !== "fail") {
            button_3.alt = "on"
            button_3.src = "data/img/green/green_3.png";
            return 0;
        }
    }
    //if green  
    if (button_3.alt === "on") {
        //use the function  
        var new_status = change_pin(3, 1);
        if (new_status !== "fail") {
            button_3.alt = "off"
            button_3.src = "data/img/red/red_3.png";
            return 0;
        }
    }
});
button_4.addEventListener("click", function() {
    //if red 
    if (button_4.alt === "off") {
        //use the function      
        var new_status = change_pin(4, 0);
        if (new_status !== "fail") {
            button_4.alt = "on"
            button_4.src = "data/img/green/green_4.png";
            return 0;
        }
    }
    //if green 
    if (button_4.alt === "on") {
        //use the function 
        var new_status = change_pin(4, 1);
        if (new_status !== "fail") {
            button_4.alt = "off"
            button_4.src = "data/img/red/red_4.png";
            return 0;
        }
    }
});
button_5.addEventListener("click", function() {
    //if red 
    if (button_5.alt === "off") {
        //use the function  
        var new_status = change_pin(5, 0);
        if (new_status !== "fail") {
            button_5.alt = "on"
            button_5.src = "data/img/green/green_5.png";
            return 0;
        }
    }
    //if green 
    if (button_5.alt === "on") {
        //use the function  
        var new_status = change_pin(5, 1);
        if (new_status !== "fail") {
            button_5.alt = "off"
            button_5.src = "data/img/red/red_5.png";
            return 0;
        }
    }
});
button_6.addEventListener("click", function() {
    //if red 
    if (button_6.alt === "off") {
        //use the function      
        var new_status = change_pin(6, 0);
        if (new_status !== "fail") {
            button_6.alt = "on"
            button_6.src = "data/img/green/green_6.png";
            return 0;
        }
    }
    //if green 
    if (button_6.alt === "on") {
        //use the function      
        var new_status = change_pin(6, 1);
        if (new_status !== "fail") {
            button_6.alt = "off"
            button_6.src = "data/img/red/red_6.png";
            return 0;
        }
    }
});
button_21.addEventListener("click", function() {
    //if red    
    if (button_21.alt === "off") {
        //use the function  
        var new_status = change_pin(21, 0);
        if (new_status !== "fail") {
            button_21.alt = "on"
            button_21.src = "data/img/green/green_21.png";
            return 0;
        }
    }
    //if green 
    if (button_21.alt === "on") {
        //use the function  
        var new_status = change_pin(21, 1);
        if (new_status !== "fail") {
            button_21.alt = "off"
            button_21.src = "data/img/red/red_21.png";
            return 0;
        }
    }
});
button_22.addEventListener("click", function() {
    //if red 
    if (button_22.alt === "off") {
        //use the function  
        var new_status = change_pin(22, 0);
        if (new_status !== "fail") {
            button_22.alt = "on"
            button_22.src = "data/img/green/green_22.png";
            return 0;
        }
    }
    //if green 
    if (button_22.alt === "on") {
        //use the function  
        var new_status = change_pin(22, 1);
        if (new_status !== "fail") {
            button_22.alt = "off"
            button_22.src = "data/img/red/red_22.png";
            return 0;
        }
    }
});

當Raspberry Pi啟動時,GPIO引腳的默認值為所有GPIO都處於INPUT模式,GPIO 0-8啟用了上拉至3V3,GPIO 9-27下拉了至0V。

您的Javascript沒有初始化例程,因此不會更改它,因為它僅在按下按鈕時才更改狀態。

創建一個函數,將符文加載,以將引腳設置為所需的默認狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM