Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to build a simple LED RESTful service in Raspberry PI?

raspberry pi node.js restful service

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 aoiregion

aoiregion

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 22 July 2014 - 05:04 PM

Hi guys, I would like to seek advise or help on how to build a simple LED RESTful service in Raspberry PI. I need to be able to get my LED to light up or off whenever I type localhost:3000/GET/1/1 or something. New to it and hope you guys could help me out with it as soon as possible, thanks in advance, guys.



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 23 July 2014 - 07:04 AM

You should check Kadence's blog series. She used leds  as email and download alerts.

 

K's blog.



#3 aoiregion

aoiregion

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 23 July 2014 - 06:46 PM

You should check Kadence's blog series. She used leds  as email and download alerts.

 

K's blog.

 

BlackRabbit, I would like to seek help on my LED REST API. My issue is my other 2 LED never light up when I execute GET and PUT method for it. I have 3 LED - Red, Blue and Green but whenever I node my LED REST API and execute GET and PUT method, only my Red LED in pin 7 light up. The strange thing is there is no error for all my pins and I'm lost now.

 

Hardware I'm using is Raspberry PI.

 

I have done npm install and npm install pi.gpio.

 

This is my led_rest_api.js:

var express = require('express');
var bodyParser = require('body-parser');
var gpio = require('pi-gpio');

var app = express();
app.use(bodyParser.json());
app.set('port', process.env.PORT || 3000);

app.get('/:pin', function(req, res){
  var pin = req.params.pin;

  gpio.open(pin, 'input', function(err) {
    gpio.read(pin, function(err, value) {
      res.send(200, {value: value});
      gpio.close(pin);
    });
  });
});

app.put('/:pin', function(req, res) {
  var pin = req.params.pin;
  var value = req.body.value;

  gpio.open(pin, 'output', function(err) {
    gpio.write(pin, value, function(err) {
      res.send(200);
      gpio.close(pin);
    });
  });
});

var server = app.listen(app.get('port'), function() {
  console.log('Listening on port %d', server.address().port);
});

Hope you could help me out with it as soon as possible, thanks in advance, guys






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download