2

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.

6
  • I don't know this API, but what will gpio.close() do here?
    – goldilocks
    May 13, 2015 at 12:01
  • @goldilocks: According to the pi-gpio documentation gpio.close() is aliased to unexport if that helps you. May 13, 2015 at 12:24
  • Alright. That alone shouldn't change the state of the pin, I think.
    – goldilocks
    May 13, 2015 at 12:42
  • @goldilocks I'd guess the close is switching the gpio back to an input rather than an output. The pull-ups and downs will then come into effect.
    – joan
    May 13, 2015 at 17:44
  • @joan I was just looking at the source, and it is mostly a wrapper around this gpio-admin tool which is mostly a wrapper around the /sys/class/gpio interface. So that close() 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).
    – goldilocks
    May 13, 2015 at 18:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.