MaxLAB
MaxLAB

Reputation: 23

Raspberry PI server - GPIO ports status JSON response

I'm struggling for a couple of days. Question is simple, is there a way that can I create a server on Raspberry PI that will return current status of GPIO ports in JSON format?

Example:

Http://192.168.1.109:3000/led

{
  "Name": "green led",
  "Status": "on"
}

I found Adafruit gpio-stream library useful but don't know how to send data to JSON format.

Thank you

Upvotes: 0

Views: 1429

Answers (2)

MaxLAB
MaxLAB

Reputation: 23

Thank You, this was very helpful. I did not express myself well, sorry for that. I don't want to send data (for now) i just want to enter web address like 192.168.1.109/led and receive json response. This is what I manage to do for now. I don't know if this is the right way. PLS can you review this or suggest better method..

var http = require('http'); 
var url = require('url');  
var Gpio = require('onoff').Gpio;

var led = new Gpio(23, 'out');

http.createServer(function (req, res) {

   res.writeHead(200, {'Content-Type': 'text/html'});
   var command = url.parse(req.url).pathname.slice(1);
    switch(command) {
     case "on":
        //led.writeSync(1);
        var x = led.readSync();
        res.write(JSON.stringify({ msgId: x }));
        //res.end("It's ON");
        res.end();
        break;
     case "off":
       led.writeSync(0);
       res.end("It's OFF");
       break;
    default:
       res.end('Hello? yes, this is pi!');
     }
}).listen(8080);

Upvotes: 1

jishi
jishi

Reputation: 24634

There are a variety of libraries for gpio interaction for node.js. One issue is that you might need to run it as root to have access to gpio, unless you can adjust the read access for those devices. This is supposed to be fixed in the latest version of rasbian though.

I recently built a node.js application that was triggered from a motion sensor, in order to activate the screen (and deactivate it after a period of time). I tried various gpio libraries but the one that I ended up using was "onoff" https://www.npmjs.com/package/onoff mainly because it seemed to use an appropriate way to identify changes on the GPIO pins (using interrupts).

Now, you say that you want to send data, but you don't specify how that is supposed to happen. If we use the example that you want to send data using a POST request via HTTP, and send the JSON as body, that would mean that you would initialize the GPIO pins that you have connected, and then attach event handlers for them (to listen for changes).

Upon a change, you would invoke the http request and serialize the JSON from a javascript object (there are libraries that would take care of this as well). You would need to keep a name reference yourself since you only address the GPIO pins by number.

Example:

var GPIO = require('onoff').Gpio;
var request = require('request');
var x = new GPIO(4, 'in', 'both');

function exit() {
  x.unexport();
}

x.watch(function (err, value) {
  if (err) {
    console.error(err);
    return;
  }

  request({
    uri: 'http://example.org/',
    method: 'POST',
    json: true,
    body: { x: value } // This is the actual JSON data that you are sending
  }, function () {
    // this is the callback from when the request is finished
  });
});

process.on('SIGINT', exit);

I'm using the npm modules onoff and request. request is used for simplifying the JSON serialization over a http request.

As you can see, I only set up one GPIO here. If you need to track multiple, you must make sure to initialize them all, distinguish them with some sort of name and also remember to unexport them in the exit callback. Not sure what happens if you don't do it, but you might lock it for other processes.

Upvotes: 1

Related Questions