Alejandro GómezUbiMaker
Published

Location-based Noise Sensor using Tessel (No GPS!)

Monitor noise levels using Tessel, Google's Location API and Ubidots

Work in progress2,513
Location-based Noise Sensor using Tessel (No GPS!)

Things used in this project

Hardware components

Tessel 1 (discontinued - see Tessel 2)
Tessel 1 (discontinued - see Tessel 2)
×1
Battery
×1
Ambient Module for Tessel
Plugged into Port A
×1

Story

Read more

Code

file_14419.js

JavaScript
var http = require('http');
var https = require('https');
var req = null;

//for tessel
var tessel = require('tessel');
var ambientlib = require('ambient-attx4');
var ambient = ambientlib.use(tessel.port['A']);
var wifi = require('wifi-cc3000');

//for ubidots
var id_sound = '54b6f72b762542082df7ff42';    // Your variable id
var token = 'kA8XWNQRHFrXVQakaJVY3Zg2qRU1lq'; // Your Ubidots Token
var sound = {
  value: 66,
  context: {
                lat: 6.77,
                lng: -75.74
             }
}

//for googlelocation api
var wifidata = {
    'wifiAccessPoints': [
    {
        'macAddress': '5c:f9:38:9e:3f:e8',
        'signalStrength': -65,
        'age': 0,
        'channel': 11
    }
    ]
};

data2googlelocation(data2ubidots);

//data2ubidots();

ambient.on('ready', function () {
	getlevel();
	function getlevel(){
		ambient.getSoundLevel( function(err, sdata) {
			sound.value = sdata.toFixed(8);
			console.log(sound.value);
			setTimeout(getlevel,100);
		});
	}
});


function data2ubidots(){
    var valueString = JSON.stringify(sound);
    var headers = {
        'Content-Type': 'application/json',
        'Content-Length': valueString.length,
        'X-Auth-Token': token,
        'Connection': 'keep-alive'
    };
    var options = {
        host: 'things.ubidots.com',
        port: 80,
        path: '/api/v1.6/variables/'+id_sound+'/values',
        method: 'POST',
        headers: headers
    }
    req = http.request(options, function(res){
        res.setEncoding('utf-8');
        var responseString = '';
        res.on('data', function(data){
            responseString += data;
        });
        res.on('end', function(){
            console.log('sending value to ubidots');
            var resultObject = JSON.parse(responseString);
            console.log(resultObject);
	 //setTimeout(data2Ubidots,1);
        });
        req.on('error', function(e){
            console.log('problem with request: '+ e.message);
        });
    });
    setTimeout(data2ubidots,1000);
    req.write(valueString);
    req.end();
}

function data2googlelocation(callback){
    var valueString = JSON.stringify(wifidata);
    var headers = {
        'Content-Type': 'application/json'
    };
    var options = {
        host: 'www.googleapis.com',
        port: 443,
        path: '/geolocation/v1/geolocate?key=YOUR_GOOGLE_API_KEY_HERE',
        method: 'POST',
        headers:headers
    };
    req = https.request(options,function(res){
        res.setEncoding('utf-8');
        var responseString = '';
        res.on('data',function(data){
            responseString += data;
        });
        res.on('end', function(){
            console.log('sending value to googleapi');
            var resultObject = JSON.parse(responseString);
            sound.context.lat = resultObject.location.lat;
            sound.context.lng = resultObject.location.lng;
            console.log(sound.context.lat);
            console.log(sound.context.lng);
            callback();
        });
    });
    req.write(valueString);
    req.end();
}

wifi.on('disconnect', function(err, data){
                  if (!wifi.isBusy()) {
                    connect();
                    }else {
                      console.log("is busy, trying again");
                      setTimeout(function(){connect();},1000);
                    }
                console.log("disconnect emitted", err, data);
          });

function connect(){
      wifi.connect({
        security: 'wpa2'//or wpa,unsecured,wep
        , ssid: 'Atom_Med'
        , password: 'atommed2015'
        , timeout: 30 // in seconds
      });
    }

Credits

Alejandro Gómez

Alejandro Gómez

3 projects • 18 followers
UbiMaker

UbiMaker

53 projects • 229 followers
Maker @ ubidots.com

Comments