Real time sensor data from the Intel Edison via Websockets
Tags: iot, hardware, javascript
Published: 2020-08-22 19:23:45


***(Note: these posts are migrated from my previous [medium.com](https://medium.com/@monkeytypewritr) blog)***

It’s been a while since my last post [Control your Intel Edison in Realtime with Websockets!](https://dev.to/w4ilun/updated-control-your-intel-edison-in-realtime-with-websockets-l9a) There’s been tons of updates on documentation, tools, and libraries that allow setting up and development a lot easier. Check out the [downloads](https://software.intel.com/en-us/iot/hardware/edison/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](https://software.intel.com/en-us/iot/library/edison-getting-started) or my previous [post](https://dev.to/w4ilun/updated-control-your-intel-edison-in-realtime-with-websockets-l9a).*

### 1. Read sensor data using the UPM repository

UPM is a high level repository for sensors/actuators that uses [MRAA](https://github.com/intel-iot-devkit/mraa). Check out the list of supported sensors [here](http://iotdk.intel.com/docs/master/upm/modules.html). 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](https://github.com/intel-iot-devkit/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](http://www.seeedstudio.com/depot/Grove-Temperature-Sensor-p-774.html) from my [Grove Starter Kit](http://www.seeedstudio.com/depot/Grove-starter-kit-plus-Intel-IoT-Edition-for-Intel-Galileo-Gen-2-and-Edison-p-1978.html):

![](https://cdn-images-1.medium.com/max/2000/1*b2exyH9GCcd84lnR9LD4ow.png)

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](https://en.wikipedia.org/wiki/Thermistor).

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](https://cdn-images-1.medium.com/max/7680/1*yYh8gmefhcFN1qIsSMvDcg.jpeg)

UPM supports the Grove Temperature Sensor and also includes example [code](https://github.com/intel-iot-devkit/upm/blob/master/examples/javascript/grovetemp.js). I am going to *wget* the example code on my Intel Edison and run it:

 <iframe src="https://medium.com/media/3f4918de00df1a578bec250b51095644" frameborder=0></iframe>

…and you should see the following output:

![it’s quite hot in my room](https://cdn-images-1.medium.com/max/2000/1*D1LJgCp1dp-KjQF0V3dRiQ.png)

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:

{% gist https://gist.github.com/w4ilun/d057a3a062967f57eb3f %}

{% gist https://gist.github.com/w4ilun/987d338c3b3111d8dd9f %}

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

![](https://cdn-images-1.medium.com/max/2000/1*1frZEtNHPThbQjK3aN1kTg.png)

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?