DEV Community

w4ilun
w4ilun

Posted on

1 1

Real time sensor data from the Intel Edison via Websockets

(Note: these posts are migrated from my previous medium.com blog)

It’s been a while since my last post Control your Intel Edison in Realtime with Websockets! There’s been tons of updates on documentation, tools, and libraries that allow setting up and development a lot easier. Check out the downloads page to get the new installer and latest firmware.

The following tutorial assumes an Intel Edison already updated and setup with connectivity. If you have a fresh out of the box Intel Edison, please follow this guide or my previous post.

1. Read sensor data using the UPM repository

UPM is a high level repository for sensors/actuators that uses MRAA. Check out the list of supported sensors here. If the sensor you are using is already supported by UPM, you will save a lot of time by getting a high level interface.

If the sensor you are using is not found in UPM, feel free to submit an issue or a pull request on Github. You can of course always use MRAA to read raw data from your sensor (e.g. via Analog, I²C, SPI, UART, etc.)

For this tutorial, I will be using the temperature sensor from my Grove Starter Kit:

This temperature sensor uses a thermistor to detect temperature. A thermistor is a device that will change its resistance when the temperature changes. By measuring the voltage output from this sensor with an analog pin, we are able to determine the temperature by looking up the sensor’s datasheet. Luckily for us, UPM has done that for use already. Learn more about thermistors here.

I connected my temperature sensor to analog pin A0, as shown in the picture below:

you can connect to any analog pin you like, just make sure you reference it correctly in your code

UPM supports the Grove Temperature Sensor and also includes example code. I am going to wget the example code on my Intel Edison and run it:

…and you should see the following output:

it’s quite hot in my room

If you look at the code in grovetemp.js, you will see that it is basically instantiating the temperature sensor object and repeatedly calling temp.value() to get the latest temperature reading.

2. Pushing sensor data via Websockets

Well that was easy :) Now all we have to do is push this data via websockets to a UI, using Socket.IO like the last tutorial:

var groveSensor = require('jsupm_grove'); //UPM sensor
var app = require('express')(); //Express Library
var server = require('http').Server(app); //Create HTTP instance
var io = require('socket.io')(server); //Socket.IO Library
var tempSensor = new groveSensor.GroveTemp(0); //temperature sensor connected to analog pin 0
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html'); //serve the static html file
});
io.on('connection', function(socket){
var interval = setInterval(function(){
socket.emit('temperature', { celsius: tempSensor.value() }); //read the temperature every 500ms and send the reading
}, 500);
socket.on('disconnect', function(){
clearInterval(interval);
});
});
server.listen(3000); //run on port 3000
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.1.js"></script>
<script>
$(function(){
var socket = io.connect('http://INTEL_EDISON_IP_ADDRESS:3000');
var temperature = $('#temperature');
socket.on('temperature',function(data){
temperature.html(data.celsius);
});
});
</script>
</head>
<body>
<h2>The current temperature is:</h2>
<h1><span id="temperature"></span>C</h1>
</body>
</html>
view raw index.html hosted with ❤ by GitHub

Put these two files on your Intel Edison and run:

node tempsensorsocket.js

Be sure to have express and socket.io installed already, and change the IP address in index.html

You should be seeing the live temperature data in your browser, try touching the sensor to see the temperature go up :)

In 40 lines of code we were able to stream live temperature sensor data via websockets! This is only possible with great open source libraries like Socket.IO, Express, UPM, and MRAA.

This tutorial itself serves as a starting point and there are tons of other things you could add on to it. A fancy CSS dashboard? Cloud Analytics? Turning on your AC when it gets too hot? Start the sprinklers?

What will you make?

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay