Giasone
Published © GPL3+

Rest API on your Tessel 2

Expose data gathered on your Tessel 2 trough a REST interface.

BeginnerProtip1 hour487
Rest API on your Tessel 2

Things used in this project

Story

Read more

Schematics

ambient-scheme.png

Code

ambient-rest.js

JavaScript
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
/*********************************************
Basic REST API for ambient module to get
ambient light and sound levels.

 * Created by Gianluca Urgese on 5/21/2016.
 
*********************************************/

var tessel = require('tessel'),
	router = require('tiny-router');
var ambientlib = require('ambient-attx4');

var ambient = ambientlib.use(tessel.port['A']);

var lights = {
    green: tessel.led[2],
    blue: tessel.led[3]
};

router
    .get('/', function(req, res) {
        res.send('Simple Tessel 2 Rest API');
        console.log("home");
    })
    .get('/light', function(req, res){
    	ambient.getLightLevel( function(err, lightdata) {
      		if (err) throw err;
        	console.log("Light level:", lightdata.toFixed(8));
        	var t = lightdata.toFixed(8);
        	res.send("Light level value is " + t);
        	lights.green.write(1);
        	setTimeout(function(){
                lights.green.write(0);
            }
            , t * 1000);

        });
    })
    .get('/sound', function(req, res){
        ambient.getSoundLevel( function(err, sounddata) {
        	if (err) throw err;
        	console.log("Sound Level:", sounddata.toFixed(8));
        	var t = sounddata.toFixed(8);
        	res.send("Sound level value is " + t);
        	lights.green.write(1);
        	setTimeout(function(){
                lights.green.write(0);
            }
            , t * 1000);
      	});
        
    });

ambient.on('ready', function () {
   setTimeout(function(){
        router.listen(8080);
        console.log("Listening on port 8080");
        lights.blue.write(1);
    },10000);
});

ambient.on('error', function (err) {
  console.log(err);
});

Credits

Giasone

Giasone

2 projects • 14 followers
Developer, maker

Comments

Add projectSign up / Login