How to build a simple LED RESTful service in Raspberry PI?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to build a simple LED RESTful service in Raspberry PI?
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.
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:
Code:
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 guys could help me out with it as soon as possible, thanks in advance, guys
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
What is the format of the number sent to res.send()? Jut guessing looking at the code I would expect that sending "200" is lighting the red LED to level 2 (whatever that is) and to set the level for green to 2 you would send "020" etc.. Have you tried this?
Are you using Raspberry Pi? I don't touch it.Two years before,I'm a electronic engineer,do work with schematic design,pcb layout,write device driver codes & applications.But now,I employ a new company for coding.Coding everything,I hate this life style.
I hope someday I can play and research Raspberry Pi as usual.
What is the format of the number sent to res.send()? Jut guessing looking at the code I would expect that sending "200" is lighting the red LED to level 2 (whatever that is) and to set the level for green to 2 you would send "020" etc.. Have you tried this?
res.send(200) is when a Number is given without any of the previously mentioned bodies, then a response body string is assigned for you. For example 200 will respond will the text "OK", and 404 "Not Found" and so on.
Quote:
Originally Posted by shell.albert
Are you using Raspberry Pi? I don't touch it.Two years before,I'm a electronic engineer,do work with schematic design,pcb layout,write device driver codes & applications.But now,I employ a new company for coding.Coding everything,I hate this life style.
I hope someday I can play and research Raspberry Pi as usual.
Yes, I'm using Raspberry PI.
Thanks anyway, shell.albert xD
Hope you can hands on to Raspberry PI as soon as possible cause it's fun and also help me out(Kidding xD) xD
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
Unfortunately since I don't have the same setup that doesn't make much sense to me -- the pin map seems an on obfuscation but I'm srue it makes sense in context.
What I was getting at is that led_rest_api.js obviously works as it is calling pi-gpio.js adn an LED is lighting. So, can you for example modify pi-gpio.js so that another LED lights every time? Or could you hard-code led_rest_api.js to output whatever pi-gpio.js is expecting for a colour other than red? If I could make sense of the code I could be of more help but on the surface this looks to me like you may just have made a small syntax error.
Unfortunately since I don't have the same setup that doesn't make much sense to me -- the pin map seems an on obfuscation but I'm srue it makes sense in context.
What I was getting at is that led_rest_api.js obviously works as it is calling pi-gpio.js adn an LED is lighting. So, can you for example modify pi-gpio.js so that another LED lights every time? Or could you hard-code led_rest_api.js to output whatever pi-gpio.js is expecting for a colour other than red? If I could make sense of the code I could be of more help but on the surface this looks to me like you may just have made a small syntax error.
I have re-edited my led_rest_api.js and they should actually works but quite a few error.
Needed help for it.
This is my re-edited led_rest_api.js:
Code:
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 || 3030);
app.post('/:pin/1', function(req, res)
{
var pin = req.params.pin;
gpio.open(pin, 'output', function(err){
if (pin === 7){
//Set pin 7 high (1)
gpio.write(7, 1, function()
{
gpio.close(7);
}
return gpio;
} if (pin === 11){
//Set pin 11 high (1)
gpio.write(11, 1, function()
{
gpio.close(11);
}
return gpio;
} if (pin === 16){
//Set pin 16 high (1)
gpio.write(16, 1, function()
{
gpio.close(16);
}
return gpio;
}
}
});
app.post('/:pin/0', function(req, res)
{
var pin = req.params.pin;
gpio.open(pin, 'output', function(err){
if (pin === 7){
//Set pin 7 low (0)
gpio.write(7, 0, function()
{
gpio.close(7);
}
return gpio;
} if (pin === 11){
//Set pin 11 low (0)
gpio.write(11, 0, function()
{
gpio.close(11);
}
return gpio;
} if (pin === 16){
//Set pin 16 low (0)
gpio.write(16, 0, function()
{
gpio.close(16);
}
return gpio;
}
}
});
var server = app.listen(app.get('port'), function() {
console.log('Listening on port %d', server.address().port);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.