DEV Community

Cover image for (Updated) Control your Intel Edison in Realtime with Websockets!
w4ilun
w4ilun

Posted on

2

(Updated) Control your Intel Edison in Realtime with Websockets!

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

The Intel Edison is an excellent platform for prototyping your next IoT project. This small little device is powered by an Intel® Atom™ SoC dual-core CPU and includes an integrated WiFi, Bluetooth LE module. In this tutorial, we will go over the “Hello World” equivalent to get started with the Intel Edison: Blinking an LED. We will then take it one step further and control the LED with a web interface via Websockets. Lets go!

1. Setting up your Intel Edison Board

For this tutorial, we will be using the Intel® Edison and Arduino Breakout Kit. To assemble and setup your Intel Edison board, you can follow this guide here: https://communities.intel.com/docs/DOC-23147 .

This is an important step as we’ll need Internet connectivity to download all the necessary libraries, as well as communicating with the outside world (The I in IoT!). Check your connectivity by visiting its IP address once Wi-Fi has been setup:

Once Wi-Fi has been setup, visit the IP address of the Intel Edison.

Visiting the IP address in a browser should show a configuration page

Note: After the Wi-Fi configuration was complete, the configuration page did not load for me when I visited the IP address in the browser. I had to manually start the server on the Intel Edison by entering the following command in the terminal:

configure_edison --server

2. Interfacing with the board I/O pins

The Intel Edison Arduino breakout board has 20 I/O pins, 6 of which are Analog, and 14 of which are digital. These can be used for any sensor inputs (i.e. potentiometers, photodiodes, temperature, etc.) or outputs (e.g. controlling motors/servos with a PWM signal with the digital pins). The following table lists the I/O pins and their numbers:

To interface with these I/O pins, we will need to install a library that provides us with an API. Intel provides a library for its IoT dev boards called **MRAA, **which is a “Low Level Skeleton Library for IO Communication on GNU/Linux platforms”. The library is open source and is available on github: https://github.com/intel-iot-devkit/mraa

Installing the library is simply running these commands on the Intel Edison:

echo "src mraa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/mraa-upm.conf
opkg update
opkg install libmraa0

Once the library has been installed, try running the blinking LED example found in the github repo:

var m = require('mraa'); //require mraa
console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console
var myLed = new m.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Galileo Gen1 & Gen2)
myLed.dir(m.DIR_OUT); //set the gpio direction to output
var ledState = true; //Boolean to hold the state of Led
periodicActivity(); //call the periodicActivity function
function periodicActivity()
{
myLed.write(ledState?1:0); //if ledState is true then write a '1' (high) otherwise write a '0' (low)
ledState = !ledState; //invert the ledState
setTimeout(periodicActivity,1000); //call the indicated function after 1 second (1000 milliseconds)
}
view raw gistfile1.js hosted with ❤ by GitHub
  1. Save the above snippet to a file (e.g. blink.js)

  2. Run it with Node!

    node blink.js
    

Hurray!

3. Installing Express/Socket.IO for Websocket communication

We’ll be installing the Express and Socket.IO libraries to allow Websocket communication with our Intel Edison. Express is a web framework for NodeJS, it will be serving our static assets (web page with control interface) and provide an HTTP server instance. **Socket.IO **will be the Websocket library of choice.

  1. Install these libraries by running the following commands (the save flag adds these libraries to the package.json file):

    npm install express --save
    npm install socket.io --save
    
    1. Create and save the following two code snippets in the same directory:

blinkWithSockets.js

var m = require('mraa'); //IO Library
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 blinkInterval = 1000; //set default blink interval to 1000 milliseconds (1 second)
var ledState = 1; //set default LED state
var myLed = new m.Gpio(13); //LED hooked up to digital pin 13
myLed.dir(m.DIR_OUT); //set the gpio direction to output
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html'); //serve the static html file
});
io.on('connection', function(socket){
socket.on('changeBlinkInterval', function(data){ //on incoming websocket message...
blinkInterval = data; //update blink interval
});
});
server.listen(3000); //run on port 3000
blink(); //start the blink function
function blink(){
myLed.write(ledState); //write the LED state
ledState = 1 - ledState; //toggle LED state
setTimeout(blink,blinkInterval); //recursively toggle pin state with timeout set to blink interval
}

index.html

<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');
$('#slider').on('input change',function(){
socket.emit('changeBlinkInterval',$(this).val());
});
});
</script>
</head>
<body>
<h1>Slide to change blink interval!</h1>
<input id="slider" type="range" min="100" max="1000" value="1000" />
</body>
</html>
view raw index.html hosted with ❤ by GitHub

Note: Remember to replace your IP address in index.html. Also, feel free to use any port other than 3000.

  1. Run the code and visit your browser:

    node blinkWithSockets.js
    

  1. Move the slider around and see the LED blink faster and slower!

  1. Please see this post on how to control your device from outside your LAN!

And that’s it! With the current setup, you can send data to your Intel Edison via Websockets. The Socket.IO library allows bi-directional communication, which means you can also relay sensor data from the I/O pins back to the client in real-time as well! In a future tutorial, I will describe how this can be accomplished and perhaps integrate with a third party API as well.

Please feel free to reach out for any questions or suggestions ☺

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay