pátek 9. prosince 2016

Bluemix IOT

Kombinace cloudu (IBM Bluemix) a IOT

Senzory postavené na Arduino platformě komunikují naměřená data do Bluemix Node.js aplikace. Hodnoty jsou ukládány do Bluemix MySql databáze.

Tabulka pro záznam údajů:

create table T_MEASURE(C_ID int AUTO_INCREMENT, C_CHANNEL varchar(255), C_DEVICE varchar(255), C_TIME_CREATED timestamp, C_FIELD varchar(255), C_VALUE int, primary key(C_ID))

alter table T_MEASURE add index(C_CHANNEL)

alter table T_MEASURE add index(C_CHANNEL, C_DEVICE)

IOT zařízení posílá naměřené hodnoty prostřednictvím HTTP GET do Bluemix, kde se uloží do databáze:

http://BLUEMIX_LOGIN.eu-gb.mybluemix.net/sendValue?channel=channel1&sensor=water&value=1234

Aplikační kód App.js v Bluemix


// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require("express");

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require("cfenv");

// create a new express server
var app = express();

// This application requires mysql
var mysql = require('mysql');

// Use the valid user credential and database name
var connection=mysql.createConnection(
{
   host     : 'us-cdbr-iron-east-04.cleardb.net',
   user     : 'USER',
   password : 'PASSWORD',
   database : 'DATABASE'
}
);

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));


app.get("/sendValue", function(req,res){
   
     var channel = req.query.channel;
     var sensor = req.query.sensor;
     var value = req.query.value;
     var timestamp = Date.now;
   
     var post  = {C_CHANNEL: channel, C_SENSOR: sensor, C_VALUE: value};
 var query = connection.query('INSERT INTO T_MEASURE SET ?', post, function(err, result) {
   console.log(err);
   console.log(result);
 });
    res.send('Inserted');

});

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
  // print a message when the server starts listening
  console.log("server starting on " + appEnv.url);
});

Žádné komentáře:

Okomentovat