[英]Raspberry pi GPIO pins wont switch
So I have an interesting problem. 所以我有一个有趣的问题。 I have wrote a program to control the climate in a room. 我写了一个程序来控制房间的气候。 The program runs as a python program and has a web interface. 该程序作为python程序运行,并具有Web界面。 The program works great but if I restart the pi all the GPIO pins turn on. 该程序运行良好,但是如果我重新启动pi,则所有GPIO引脚都会打开。 Then I run my python program and they dont turn off untill I go to my switches page. 然后,我运行我的python程序,直到我转到开关页面,它们才会关闭。 Right when that loads all the commands turn pins on and off and the program functions correctly. 正确的是,在加载所有命令时,请打开和关闭引脚,程序才能正常运行。 After I have loaded that page once after startup it doesnt effect the program anymore and make the pin commands hang anymore. 在启动后我加载该页面一次后,它不再影响程序,并且使pin命令不再挂起。
Here is the code for that page. 这是该页面的代码。
<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>
Here is the code for the script.js 这是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;
}
}
});
When the Raspberry Pi boots up, the default for the GPIO pins is all GPIO are in INPUT mode, GPIO 0-8 have pull-ups to 3V3 enabled, GPIO 9-27 have pull-downs to 0V enabled. 当Raspberry Pi启动时,GPIO引脚的默认值为所有GPIO都处于INPUT模式,GPIO 0-8启用了上拉至3V3,GPIO 9-27下拉了至0V。
Your Javascript does not have an initialise routine which changes this as it only changes the states when the buttons are pressed. 您的Javascript没有初始化例程,因此不会更改它,因为它仅在按下按钮时才更改状态。
Create a function which rune onload to set the pins to the default state you want. 创建一个函数,将符文加载,以将引脚设置为所需的默认状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.