Saturday, 16 September 2017

HOW TO TAKE A VALUE FROM ARDUINO AND SEND IT TO THE INTERNET WITHOUT ETHERNET (THING SPEAK.COM) USING NODE JS IN UBUNTU:

First you need to download Arduino in your ubuntu to download arduino from ubuntu execute this line

sudo apt-get update && sudo apt-get install arduino arduino-core

For example i took ultrasonic sensor to generate the data you can take whatever you want






connection diagram for ultrasonic and arduino

connect pin 

vcc to arduino vcc 
ground to arduino ground
trigpin to arduino 9 th port
echo to arduino 10 th 

then exec following line of command using Arduino IDE

  1. /*
  2. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3. *
  4. * Crated by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. // defines pins numbers
  9. const int trigPin = 9;
  10. const int echoPin = 10;
  11. // defines variables
  12. long duration;
  13. int distance;
  14. void setup() {
  15. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  16. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  17. Serial.begin(9600); // Starts the serial communication
  18. }
  19. void loop() {
  20. // Clears the trigPin
  21. digitalWrite(trigPin, LOW);
  22. delayMicroseconds(2);
  23. // Sets the trigPin on HIGH state for 10 micro seconds
  24. digitalWrite(trigPin, HIGH);
  25. delayMicroseconds(10);
  26. digitalWrite(trigPin, LOW);
  27. // Reads the echoPin, returns the sound wave travel time in microseconds
  28. duration = pulseIn(echoPin, HIGH);
  29. // Calculating the distance
  30. distance= duration*0.034/2;
  31. // Prints the distance on the Serial Monitor
  32. Serial.print("Distance: ");
  33. Serial.println(distance);
  34. }

Then it will produce the Serial out put You can see the out put using serial monitor in arduino IDE

NEXT STEP IOT:

Download node js in your ubuntu system...

it is very easy installation procedure:

Step 1: Add Nodejs PPA

Node.js is available in two versions, first is current (most recent version) and another is LTS. Select which version you need to install on the system. Then use one of following commands to install PPA on your system. Node.js officially provides these PPA’s.
Use Current Release: At te last update of this tutorial, Node.js 8.0 is the current Node.js release available.
$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
Use LTS Release : At te last update of this tutorial, Node.js 6.11 is the LTS release available.
$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
For this tutorials, we are using latest current release and added their PPA to my system.

Step 2: Install Nodejs and NPM

After adding required PPA file lets install Node package. NPM will also be installed with node.js. This command will also install many other dependent packages on your system.
$ sudo apt-get install nodejs

Step 3: Check Node.js and NPM Version

After installing node.js verify and check the installed version. You can find more details about current version on node.js official website.
$ node -v 

v8.2.1
Also, check the version of installed npm.
$ npm -v 

5.3.0
thats all for installation about node js

after install node js if you want to look how the node js and all click here...

then you have add two things in your node js that is serialport , thingspeakclient 


Before going for node js we have to lok about thingspeak for post all our data and get our data ...
it will give you the free api to see our data in cloud... lets look at that


here you have to create one account ... then make one channel for post your data

click the new channel to create channel

then enter the label name for identification


then click save then your channel will be saved.

lets look at the api 



go to the Api-keys to see the api of your channel...

connect the arduino to your laptop using usb

then choose your port in arduino then copy the port name and paste here

var port = new SerialPort('/dev/ttyACM1', { autoOpen: false });   <-- this code is in node js 7th line

look at the write key and channel id for post the data you have to enter your channel id and write key instead of mine...

client.attachChannel(<channel id>, { writeKey:'<writekey>', readKey:'<readkey>'}, function(){

   if (i==1){
     console.log("connected!!!")
     i++;
   }
 });

client.updateChannel(<channel id>, {<channel field name>: buff}, function(err, resp) {
// console.log(resp)
    if (!err && resp > 0) {
        console.log('update successfully. Entry number was: ' + resp);
        console.log('value is: ' + buff);
    }
});


// client.getChannelFeeds(<channel id>,'<channel field name>', function(err, resp) {
//  console.log(resp)

//     if (!err && resp > 0) {
//         console.log('update successfully. Entry number was: ' + resp);
//     }
// });


you can see the channel id and write key and read from the same api-key page




Thats all we will see node js to post the data

create ultrasonic.js file in your desktop

var ThingSpeakClient = require('thingspeakclient');
var client = new ThingSpeakClient();

var request = require('request');
var http = require('http');
var http = null;
var SerialPort = require('serialport');
var port = new SerialPort('/dev/ttyACM1', { autoOpen: false });
i=1;
port.open(function (err) {
  if (err) {
    return console.log('Error opening port: ', err.message);
  }

  // Because there's no callback to write, write errors will be emitted on the port: 
  port.write('main screen turn on');
});

// The open event is always emitted 
port.on('open', function() {
  return console.log('Port opened');
  // open logic 
});
// Switches the port into "flowing mode" 
// port.on('data', function (data) {
//   console.log('Data:', data);
// });

// Read data that is available but keep the stream from entering "flowing mode" 
port.on('readable', function () {
  // console.log('Data:', port.read());
  var buff = new Buffer(port.read(), 'utf8'); //no sure about this
  // console.log('data received: ' + buff.toString());
  // debugger

  client.attachChannel(321131, { writeKey:'WNCHK5PZ0E6QLA11', readKey:'XMKWNQW2I7JQJGXS'}, function(){

   if (i==1){
     console.log("connected!!!")
     i++;
   }
 });


client.updateChannel(321131, {field1: buff}, function(err, resp) {
// console.log(resp)
    if (!err && resp > 0) {
        console.log('update successfully. Entry number was: ' + resp);
        console.log('value is: ' + buff);
    }
});


// client.getChannelFeeds(321131,'field1', function(err, resp) {
//  console.log(resp)

//     if (!err && resp > 0) {
//         console.log('update successfully. Entry number was: ' + resp);
//     }
// });


});

copy and paste this code in your ultrasonic.js 

to run the js execute this line node ultrasonic.js

each and every 15 second the api will get updated in thingspeak.com

here is the example data in thingspeak like this you will get datas


enjoy coding with electronic...

No comments:

Post a Comment