If you are following this blog series, I hope you are comfortable with running a big Linux image on your Galileo as explained in my previous post.
In this post we will be using the Intel Galileo board to run a web server which will serve a page for controlling an LED connected to a board. This means that if your board is on a network any user can access the page served by the board and switch the LED on or off. The implications and extensions of this example app can be for controlling any device connected to the board via the web.
In this post we will work using a different linux image called the Internet of Things version. This image comes with many plugins for interfacing the with the GPIO pins and running web servers. You can download this new image from the Intel IoT downloads page. More details on this image and its usage can be found at the intel pages. Also, I would like to point out that the steps for installing this image on the SD card are quite straightforward but also different from installing the default image I mentioned in my older article and require downloading of additional software. You can find detailed instructions for installing this image for windows platform on the intel page.
Once the image is installed you can connect your device to your router and have it available on your network. Now you can run a web server directly on the image using python or the LightHTTP package which comes with it. However, the Inter IoT community has released an XDK which I will be using in this post to configure the web server and send signals to my GPIO pins to switch the LED on/off. The XDK is for running Node.js code on the board and comes with sample code for interfacing with the pins.
The XDK will detect the board if it is present on the network and will deploy the code. It has easy options for starting the deployed app and stopping it all without physically connecting to the Galileo. The editing experience on it is a bit clunky but it serves the purpose of file deployment. I edited my main code in another editor and used the XDK only for deployment.
I used the IoT local temperature example as a template and modified its code to configure the Node.js server and the board interaction.
The app requires 3 important components and all the code is available at: https://github.com/napsternxg/IntelGalileoIoT_LEDOnOff
- LED circuit connected to Galileo
- Web page with On/Off switch served by the Galileo
- Sending the input from web page to the pins.
The final product looks like the following images (click image for more pics):
In my web app I have used the Socket.io plugin for sending and receiving the signals asynchronously. The main component of the code for interfacing with the GPIO pins is the one using mraa plugin.
var mraa = require('mraa'); //require mraa console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Galileo Gen1 & Gen2) myOnboardLed.dir(mraa.DIR_OUT); //set the gpio direction to output var ledState = false; //Boolean to hold the state of Led
After initializing the pins we just need to use the following piece of code to switch the LED on/off.
myOnboardLed.write(1); //Send High leading to LED ON myOnboardLed.write(0); //Send Low leading to LED OFF
Overall this was a major step for me in understanding the concept of internet of things and getting my first internet of thing (simple LED) to be controlled over the internet at the flick of a button.