Blynk + JavaScript in 20 minutes

Learn how to use Blynk with Node.js on a variety of platforms.

Categories: Intermediate

Ever wanted to control "some THING" from your smartphone?

Now it’s really easy.
Blynk
is a universal remote control, that can also display and plot data (and works over internet). It makes prototyping Internet of Things applications so easy, that many people call it "
IoT on-the-go
".

For example, this is how to
control a quad-copter from your phone (in 30 minutes!)
:
http://www.instructables.com/id/Control-quadcopter…

Also, here is
the official documentation
.

Blynk is supporting
lots
of prototyping
hardware platforms and configurations.
 And it has released a
Node.js / Espruino
module recently, so now it can also be used on:

  • Regular computers with Windows/Linux/OS X
  • Single-board computers like Edison, Raspberry Pi, Beagle Bone,…
  • OpenWrt – based routers and boards like VoCore,Carambola, TL-MR3020
  • Espruino – compatible microcontrollers

This is a
really
nice and strong addition to
the list of supported platforms.

Today I’m going to show you some simple steps how to start Blynking using JavaScript. Before we start, we need to do few preparation steps:

  • Check if JavaScript is installed on your device

Step 1: Node.js and Blynk installation

Check if Node.js is installed.

Open the console (terminal) on your board and type:

node --version


or

nodejs --version


You should see something like:

v6.2.1


If it doesn’t print the version or shows an error, please follow Node.js installation guide for your platform. I’ll give you some hints:

Raspberry Pi / BeagleBone

Check that your board is connected to the internet, run in it’s terminal:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -


The previous command updates our package repository to include the required packages. Now, let’s install
Node.js
and Blynk!

sudo apt-get update && sudo apt-get upgrade

sudo apt-get install -y build-essential nodejs

sudo npm install -g npm

sudo npm install -g onoff

sudo npm install -g blynk-library


OpenWRT (VoCore, Carambola, WRTnode, TL-MR3020…)

I was able to run it on VoCore, it should also be easy to do it on other OpenWrt-based computers. Everything I needed was to add Espruino package to my OpenWrt source and build it:
https://github.com/vshymanskyy/OpenWRT-Espruino-pa…

For OpenWrt you can start with this example:
https://github.com/vshymanskyy/blynk-library-js/bl…

Intel Edison

Node.js should be already there… Just follow the Getting Started guide:

https://learn.sparkfun.com/tutorials/edison-getti…

After verifying the Node.js is on your board, and the board is connected to the internet, run:

npm install -g mraa blynk-library


Intel Galileo

You will need to use an SD card that contains the latest Intel® IoT Developer Kit image, and use USB-to-UART to access it:

After verifying the Node.js is on your board, and the board is connected to the internet, run:

npm install -g mraa blynk-library


Espruino Pico

Did you notice that tiny micro-controller running JS?
http://www.espruino.com/Pico

It has no internet connectivity out-of-the box, but you can add a cheap ESP8266 to get it:
http://www.espruino.com/ESP8266

(There are other options, read here:
http://www.espruino.com/Internet
)

After establishing internet connection, you can start with this example:
https://github.com/vshymanskyy/blynk-library-js/bl…

Other boards,PC with Linux, Windows …

Just google how to install Node.js 😉

Then run in the console:

npm install -g blynk-library


…TROUBLESHOOTING…

1.
npm install command might fail on your board, saying something like this (I got this on Galileo):

npm ERR! Error: SSL Error: CERT_NOT_YET_VALID


In this case, you just need to update time on your system using
"date"
command 🙂

2.
Note that on some boards, the default direct pin control won’t work.But you are always free to do
ANYTHING you want using Virtual Pins
!!!

We have prepared the environment, and now it is time to play!

Step 2: Writing a simple script

In the Blynk mobile App:

  • Create a new dashboard of type
    Generic
    , and send yourself an
    Auth Token
    .
  • Add a
    Value Display
    widget and bind it to
    V9
  • Add a
    Slider
    widget and bind it to
    V1
  • Press
    Run
    (triangle in the upper right corner)

Let’s check simple built-in test script.

Note:

NODE_PATH
environment variable should point to the place where npm stores globally installed modules. If you get something like "
Error: Cannot find module blynk-library
", you should run in the console (the path might be different):

export NODE_PATH=/usr/local/lib/node_modules


Now, run on your board (put your auth token):

blynk-client 715f8cafe95f4a91bae319d0376caa8c


It should print something like:

Connecting to SSL: blynk-cloud.com 8441

Connected, authorized

Blynk ready.


Press
Ctrl+C
to exit.

If it doesn’t work, check if:

  • you used a correct auth-token from your mobile project
  • internet connection is OK
  • simple Node.js scripts work

Usually there should be no problems.

Now let’s write our own script.

TCP connection

First try a TCP connection example. It is insecure, but easier to start.

On the board, create a new file (call it
blynk-test.js
):

var Blynk = require('blynk-library');

var AUTH = 'YOUR_AUTH_TOKEN';

var blynk = new Blynk.Blynk(AUTH, options = {

 connector : new Blynk.TcpClient()

});

var v1 = new blynk.VirtualPin(1);

var v9 = new blynk.VirtualPin(9);

v1.on('write', function(param) {

 console.log('V1:', param[0]);

});

v9.on('read', function() {

 v9.write(new Date().getSeconds());

});


Replace
YOUR_AUTH_TOKEN
with your token from the App.

There are two Virtual Pins specified here:
v1
and
v9
. These are actions for your widgets.

When you run the script, the project on your phone should start working:

  • The
    Value Display
    widget should show current time seconds.
  • Moving a
    Slider
    should make script printing current value.

Also, if
mraa
or
onoff
package is installed, you should be able to read/write digital pins
out-of-the-box
.

SSL connection (default)

var Blynk = require('blynk-library');

var AUTH = 'YOUR_AUTH_TOKEN';

var blynk = new Blynk.Blynk(AUTH);

var v1 = new blynk.VirtualPin(1);

var v9 = new blynk.VirtualPin(9);

v1.on('write', function(param) {

 console.log('V1:', param[0]);

});

v9.on('read', function() {

 v9.write(new Date().getSeconds());

});


It should work same way, but use a secured connection.

For more examples, check this folder:
https://github.com/vshymanskyy/blynk-library-js/tr…

Now you’re ready to start exploring Blynk widgets and features 🙂

Step 3: Adding a Terminal widget

For example, let’s add a
Terminal
widget to you phone project and bind it to virtual pin
V3
.In your script, add these lines:

var term = new blynk.WidgetTerminal(3);

term.on('write', function(data) {

 term.write('You wrote:' + data + '\n');

 blynk.notify("HAHA! " + data);

});


Every time you input text into terminal, it will send it back and also push you a notification!
Note, that it won’t send you more than 1 notification per minute…You can also:

  • Send tweets and e-mails
  • Plot & store hardware sensor data
  • Bind phone App controls to any actions on the script side
  • Control Arduino and other prototyping platforms
  • Get phone sensor data (soon)
  • and much more!

Next time, I will explain how to use Virtual Pins to do PWM and read advanced sensor data.

Hope you enjoyed it.. Waiting for your comments and suggestions how to improve the article.And happy Blynking! 😉

Comments are not currently available for this post.