Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help on my LED REST API

    I would like to seek help on my LED REST API. I have finished constructed my basic REST API for my Raspberry PI. What I'm trying to do now is allow my LED to light up whenever I key localhost:3000/7/1 and key localhost:3000/7/0 to switch off. But my problem now is my REST API can't works and hope you guys could help me out with it as soon as possible, thanks in advance, guys.

    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 || 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);

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,757
    Thanks
    41
    Thanked 191 Times in 190 Posts
    what isn't working? are you getting a compilation error? is it just seeming to do nothing? details details details

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •