Intel Galileo programming with node.js
In a previous tutorial I introduced the Grove starter Kit with Intel Galileo Gen 2 and how to work with it. You can check it before starting with this one (you have to be familiar with Intel Galileo and Arduino IDE anyway to start here). In that tutorial all we did was capturing data (from a temperature sensor), in this one we’ll be storing the data we captured in a mysql database using node.js . Quite easy if you’re used to web or mobile development. Easy anyway if you’re not !
Setting a Node.js server and MySql database
Github and the internet are full of examples of Node.js simple web servers to perform basic MySql cruds. You can search a tutorial on the steps to build one if you need to. For this tutorial, I provide you with the following code source for our node.js web server that we’ll use to send and receive our sensors data to/from the database.
We’ll be working on the server.js file. It creates and initiates a mysql database connection. You can configure your host and database name and password there :
var express = require('express') var mysql = require('mysql') var app = express() var con = mysql.createConnection({ host: "localhost", user: "root", password: "root", database: "iotdb" }) con.connect(function(err) { if (err) throw err console.log("Connected!") })
then it uses calls to Express.js ‘s middleware functions to access to the request object (req
), the response object (res
) . Here is an example of a simple hello world Express application:
var express = require('express') var app = express() app.get('/', function (req, res) { res.send('Hello Here') }) })
Which makes the following js code to add a temperature value to our database : ( you have obviously to create the mySql database manually for this tutorial )
/*** ADD ***/ app.get('/add', function (req, res) { var query = con.query("INSERT INTO temperatures (value) VALUES ("+req.query.value+")", function(err, rows) { if (err) console.log("Error inserting : %s ",err ); else res.json({msg:"inserted successfully"}) }); })
We’ll also use a call to app.listen() which returns the HTTP server instance created for us by Express to configure port number and server’s address:
// Port Number const port = 4300; // server address const address = '172.19.6.189'; app.listen(port, address, () => { console.log('Server started on address '+address+' and on port '+port); });
Once you’ve cloned the code for the server and created your MySQL database manually, update the Server.js file to match your database configurations and add your ip address and port number. Then start your server using node command :
You can use Postman or simply your browser to test your server before starting on Arduino IDE.
It’s recommanded you check all your operations before starting with the board and sensors :
Sending captured data to server
First, perform all needed connections to your Intel Galileo Gen 2 board, your temperature sensor and an ethernet cable connection included :
Then, we’ll have the following code to run on the loop method of your sketch on Arduino IDE
void loop() { int val = analogRead(pinTemp); float resistance = (float)(1023-val)*10000/val; // Calculate the temperature based on the resistance value. int temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //Serial.println(temperature); if(temperature>16.0){ if (client.connect(server,4300)) { // adding temp String s = "GET /add?value="; s+=temperature; client.println(s); client.println(); } } delay(3000); } }
we simply made a simple call to our service’s get /add?value . Note that you have to add your board’s MAC adress and you server’s IP address to this sketch. Here is the full code for this simple server call .
That’s all about storing data retrieved from sensors through the internet ! You’ll simply need to capture that data and build web services that allow you to manage it and make it exchangeable between ” things “.
Our next IoT tutorial is on controlling those sensors through a mobile app. This tutorial is the seed, if you got it right everything following will be even easier.
Recent Comments