Nodejs

use obniz with Nodejs

What is Node.js

Node.js is software which run your JavaScript code on your pc. Not browser.
With browser, you should let the page open to use obniz.
But Node.js will run on your pc or server. So it is easy to run codes forever.
Save temparature to dropbox everyhour is easy.
And Node.js has various open source library named npm.
Easy to integrate more webservices.

  • Easy to run JavaScript continuously
  • various open source library

Install Nodejs

You need to install Node.js on your pc

https://nodejs.org/

Create Nodejs Project

Make your first project.
Node.js project is just folder.
make a empty folder. folder name is everything ok.
And open shell (Windows? Open PowerShell).
and cd to that folder and do this command

npm init

questions can be skipped by press Enter key.
After done, package.json file appear on the folder.
this is project file.
And make the empty text file named "app.js" on same folder.
Now you can run app.js on your pc.

Install obniz.js

To use obniz on your project, you should install obniz.js on your project.
In HTML, we include it by adding <script> tag.
But in Node.js, we can install it by name.

npm install obniz 

Just do this.
obniz is avaiable on app.js file.

Program

Open app.js on Editor.
First include obniz.

var Obniz = require("obniz");

this is similar to <script> on HTML.
But this only include installed programs.
You can use Obniz because you did npm install obniz.

After this program is same as HTML.
First connect an obniz.

var Obniz = require("obniz");

var obniz = new Obniz("1234-5678");
obniz.onconnect = async function () {

}

And connect parts and use it.

var Obniz = require("obniz");

var obniz = new Obniz("1234-5678");
obniz.onconnect = async function () {
  var servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
  servo.angle(90);
}

To run your app.js, type this on shell

node app.js 

Until you terminate it(Cntrl+C) this program runs continuously

npm

npm is package management software.
You can install programs not only obniz.

Twitter
https://github.com/desmondmorris/node-twitter

Dropbox
https://github.com/dropbox/dropbox-sdk-js

And so many libraries.
This is big advantage compared from browser and other program languages.



Next : With AWS Lambda