Building a Serverless Sensor Network in Javascript - Josh Marinacci
-
Upload
withthebest -
Category
Technology
-
view
33 -
download
1
Transcript of Building a Serverless Sensor Network in Javascript - Josh Marinacci
About Your Speaker
Josh Marinacci
Head of Developer Relations at PubNub
Very nasty cold
Keep it short
Sensor Network
● Small, fairly dumb devices connected to the Internet● Collect data in realtime● Do something with the data, in realtime
Sensor Options
● Adafruit.com● Weather: (temperature, humidity, wind speed, etc.)● Cameras● Buttons and switches
Arduino MKR1000
● Built in WiFi● Lots of IO● Power Saving● Built in LiPo● Turn into Soft AP● No host PC required● Have to code in C● pubnub.com/blog/tag/mkr1000
Controlling with JavaScript
1. var five = require('johnny-five');
2. var board = new five.Board();
3.
4. board.on('ready', function() {
5. var led = new five.Led(13); // pin 13
6. led.blink(500); // 500ms interval
7. });
Controlling with JavaScript1. pubnub.subscribe({
2. channel: 'smart-led',
3. callback: function(m) {
4. if(led) {
5. r = (m.color === 'red') ? m.brightness : r;
6. g = (m.color === 'green') ? m.brightness : g;
7. b = (m.color === 'blue') ? m.brightness : b;
8.
9. led.color({red: r, blue: b, green: g});
10.
11. console.log( 'color change to...' );
12. console.log( led.color() );
13. }
14. }
15. error: function(err) {console.log(err);}
16. });
Serverless Architecture
● Combine micro-services● No server administration, hosting, owning, etc.● Run code at higher level: Module, Object, Function● Pay by usage● Leverage microservices● Not all or nothing. Usually hybrid.
PubNub BLOCKS
● Computation in global Data Stream Network● Just Javascript w/ Promises● Distributed data store● Secure: NodeJS + cgroups + DSN● Web or CLI● Fast, low latency● Pay for just what you use● Just released. Free until Dec.
EON: Charts + MapsCreating Realtime Data Visualization
https://www.pubnub.com/developers/eon/
var five = require('johnny-five');
five.Board().on('ready', function() {
var temperature = new five.Thermometer({
controller: 'DS18B20', pin: 2
});
photoresistor = new five.Sensor({
pin: 'A2', freq: 250
});
temperature.on('data', function() {
console.log(this.celsius + '°C', this.fahrenheit + '°F');
temp = this.celsius;
});
photoresistor.on('data', function() {
console.log('photoresistor: ' + this.value);
light = this.value;
});
setInterval(sendData, 3000);
});
Using Johnny-Five to get the data from Arduino!
We’re going to publish the data from sensors every 3 seconds
function sendData() {
var data = {
'temperature': temp,
'light': light
};
pubnub.publish({
channel: 'temperature-photoresistor',
message: data,
});
}
Publishing data to PubNub
<div id="chart">
<div id="light"></div>
<div id="temp"></div>
<div id="pot"></div>
</div>
<script type="text/javascript">
var pubnub = PUBNUB.init({
subscribe_key :'sub-c-d784e128-da7d-11e5-9511-0619f8945a4f'
});
</script>
eon.chart({
channel: 'temperature-photoresistor',
generate: {
bindto: '#light',
},
pubnub: pubnub,
transform: function(m) {
return {
eon: {
light: m.light
}
}
}
});
transform() takes raw data to transform to fit in the schema that EON understands!
{'temperature': 20,
'light': 350 };
Receive raw data
eon.chart({
channel: 'temperature-photoresistor',
generate: {
bindto: '#temp,
},
pubnub: pubnub,
transform: function(m) {
return {
eon: {
temp: m.temp
}
}
}
});
var five = require('johnny-five');
five.Board().on('ready', function() {
potentiometer.on('data', function() {
// value range 0 - 1023
console.log(this.value, this.raw);
pubnub.publish({
channel: 'potentiometer',
message: { eon: {
'potentiometer': this.value
}}
});
});
});
Note:I am sending the object differently this time.
Instead of the raw data,{pot: value}, I am publishing {eon:{pot: value}}, so EON can read the data directly.
You don’t need to use transform(), when you receive the data.
eon.chart({
channel: 'potentiometer',
generate: {
bindto: '#pot',
data: {
type: 'gauge'
}
},
pubnub: pubnub
});
eon.chart({
channel: 'potentiometer',
generate: {
bindto: '#pot',
data: {
type:'gauge'
},
gauge: {
label:{
format: function(value, ratio){
return value; //returning here
the value, not in %
},
},
min: 0,
max: 1023
},
...
...
color: {
pattern: ['#FF0000', '#F6C600', '#60B044'],
threshold: {
values: [341, 682]
}
}
},
pubnub: pubnub
});
0 to 341 = red342 to 682 = yellow683 to 1023 = green
●●●●