IOT Enabled by Amin Nagpure - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Sending Data to the Cloud

 

we are going to create simple program in nodejs, which will just post data to the cloud, we are going to use thingspeak cloud services.

i hope you know programming in nodejs, or at least have understanding of it, since i will be providing the code, you just need to know how to use it

i have written ebook on nodejs with koa2, you can download that, just go to amazon.com or .fr,.uk,.in or any related to your country and search for nodejs with koa2 amin nagpure, my ebook will show up.

just download the latest stable version of nodejs

https://nodejs.org/en/

and paste this code, later you can modify it according to your will

and the code i'm pasting below is, name it as “tempupdate.js”

========code starts here===============

var slport=require("serialport");

var request=require("request");

var sp=new slport.SerialPort("COM4",

{

budrate:9600;

parser:slport.parsers.readline("\n");

});

 

sp.on("open", function()

{

 

sp.on('data', function(data){

var datajson=JSON.parse(data.toString());

 

request({

url:"https://api.thingspeak.com/update",

method:"post"

headers:{

"x-myserctuser":"mysecretpasswor"

},

form:{

field1:JSON.stringify(dataJson.Tempreature);

field2:JSON.stringify(dataJson.Humidity);

}

},

function(err,response,body){

if (err)

{

console.log(err);

}

else{

console.log(response.statusCode,body);

}

});

});

});

 

=============Code ends here==========================

nodejs is event driven programming language, complex tasks can be easily done with few lines of codes in nodejs.

now your program will post data to the cloud services of thingspeak.

data is posted to the cloud service

https://thingspeak.com/

later you can login and check the data

 

thanks for reading my ebook, for any errors or issues comment on my blog, i will answer