I've been following a guide for implementing GPIO pin control via a web interface running on my PI. The guide uses quick2wire-gpio-admin for controlling the GPIO pins and for serving the web page (my reputation on this site is too low to post more links but its all in the guide). The outcome is a web page with two buttons, an on button and an off button. The relay is supposed to switch between its two states.
It all works but the state, high or low, of the GPIO pin isn't kept for more than maximum a second. Then it seems to toggle back into a floating position. I would like the state to be kept until I push a button on the web page to change state. This is the code:
// Requires
var express = require('express');
var path = require('path');
var querystring = require("querystring");
var url = require('url');
var gpio = require('pi-gpio');
// Create app
var app = express();
var port = 3700;
// Set views
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'views')));
// Serve files
app.get('/interface', function(request, response){
response.sendfile('views/interface.html')
});
// Send commands to PI
app.get("/send", function(request, response){
// Get data
var queryData = url.parse(request.url, true).query;
console.log("State " + queryData.state + " received.");
// Apply command
if (queryData.state == 'on') {
gpio.open(7, "output", function(err) {
gpio.write(7, 1, function() {
gpio.close(7);
});
});
}
if (queryData.state == 'off') {
gpio.open(7, "output", function(err) {
gpio.write(7, 0, function() {
gpio.close(7);
});
});
}
// Answer
response.writeHead(200, {"Content-Type": "text/html"});
response.end();
});
// Start server
app.listen(port);
console.log("Listening on port " + port);
I found a solution by adding pull up and pull down resistors to respective state as in the code below:
...
gpio.open(7, "output pullup", function(err) {
...
and
...
gpio.open(7, "output pulldown", function(err) {
...
My question
I'm wondering if this approach of using a pull up to keep the state high and a pull down to keep the state low is a good solution? I'm fairly new at working with the PI and have very little experience in how to handle its GPIO pins.
Thank you for your time.
gpio.close()
do here?/sys/class/gpio
interface. So thatclose()
method literally just writes the pin number to/sys/class/gpio/unexport
; AFAIK that shouldn't change the state of the pin (you can export a pin, set it to out, set it high, unexport it, export it again and it is still a high output).