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...

Thursday, 14 September 2017

HOW TO DO THE SIMPLE MARKS ENTER TABLE WITH REACT JS WITH LOCAL STORAGE EXPLAINED:

here i have entered the todo list type project with localstorage ...

First you have to install npm and node for react js...

For install that things click here

then make the react app :

create-react-app <your app name>

if you dont want to di this all then check this git hub 

Clone the file and make use of it...

copy paste this repo to ur file ...

exec this line in terminal git clone git@github.com:thisisajith/react-with-local-storage.git


Wednesday, 13 September 2017

HOW TO GET THE RAILS VALUE IN ANGULAR JS (Connection between angular and rails) EASIEST WAY TO CONNECT ANGULAR JS TO RAILS:

First of all you need to know about Angular js and Rails (MVC) 

Go to this links to know About Angular JS and Rails:
    

This is the websites i learned how to make a program in above languages You can use any of websites to learn...

If you want to add Angular Js to rails

gem 'angularjs-rails'
gem 'angularjs-rails-resource', '~> 1.1.1'

First gem is for Add the angular js frame work to your rails app.
Second gem is to use the rails routes in angular js.
put this inside of your gem file and add these commands inside the application.js

//= require lib/angular.min

//= require angularjs/rails/resource

Put these lines after jquery because jquery wants to boot first .

Here you have to add these lines in your Angular controller


$scope.init_jsons = function(){
   if(typeof json != 'undefined')
      {
        angular.forEach(json, function (value,key){
        $scope[key] = value;
       })
   }
$scope.init_jsons();

then Add all rails variable to the script tag like this

<script>
var json=<%= @json.to_json.html_safe%>
</script>


Above method you have to pass the object to the json and we can use the object key values directly ...

 <script>
var json=
{
name:<%= @name.to_json.html_safe%>
marks:<%= @marks.to_json.html_safe%>
}
</script>

And here you can just pass the single rails variable inside the Angular controller... Then you can use the name and marks directly for the json variable ...

then you can start coding :-)

Tuesday, 12 September 2017

HOW TO GET THE VALUES FROM JOIN TABLES:

You have to create two tables .

For example i take customer and product tables.

Consider you need to know about the product which is is particular customer First of all you need to create the rails app...

rails new jointable -d mysql

The above code will create the rails app with the mysql database...

The you need to create the models and join tables ☺

For model creation:
 1. rails g model customer name:string mail:string
 2. rails g model Product name:string





For the Join table :
1. rails g model Join
   

in db file you can create the migration data:

class CreateJoins < ActiveRecord::Migration[5.1]
def change
create_table :joins do |t|
t.references :customer
t.references :product
t.timestamps
end
end
end


Then you have to create  the migration :
   For the migration :
         rake db:migrate
   If you face any Problem :
         just run this : rake db:drop db:create db:migrate

Then you have to create the assosiation for Join table :


class Customer < ApplicationRecord
has_many :joins
has_many :products , through: :joins
end

class Join < ApplicationRecord
belongs_to :customer
belongs_to :product
end

class Product < ApplicationRecord
has_many :joins
has_many :customers , through: :joins
end

These Assosiation You have to create ...

Then open the rails console see the result how it comes...

In the rails console:


Thankyou friends ...