Introduction: Blynk + JavaScript in 20 Minutes [Raspberry Pi, Edison, Espruino ...]

Picture of Blynk + JavaScript in 20 Minutes [Raspberry Pi, Edison, Espruino ...]

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!):
https://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:

  1. Check if JavaScript is installed on your device
  2. Install Blynk library: https://github.com/vshymanskyy/blynk-library-js

Step 1: Node.js and Blynk Installation

Picture of 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:

v0.10.38

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 build-essential
    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:

    1. https://software.intel.com/en-us/creating-bootable...
    2. https://learn.sparkfun.com/tutorials/galileo-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

    ________________________
    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:

    1. Create a new dashboard of type Generic, and send yourself an Auth Token.
    2. Add a Value Display widget and bind it to V9
    3. Add a Slider widget and bind it to V1
    4. 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: cloud.blynk.cc 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

    otavio.asg7 (author)2017-10-29

    Hello, i'm new with JS and raspberry, so i'm sorry if it's a basic question. I'm now searching for a way to use push notification to notify my app when ah simple button state (GIPO 2) change, but i can't find any example about that, can anyone help me with that.

    prasadsiriwardana (author)2017-05-13

    I got this message if I enter "sudo npm install -g npm"

    "sudo: npm: command not found"

    What can I do?. How I install it?

    sudo apt install npm

    TheLoneWanderer (author)2017-03-13

    does pi zero work?

    mrcoolkaran007 (author)2015-11-18

    I get the error cannot locate bindings file. When I try to run
    node example.js
    Everything is installed npm,onoff,blynk-library

    xman5735 (author)mrcoolkaran0072017-01-17

    follow this super simple instruction set and then redo the first page. i believe the OP figured we already did this. http://weworkweplay.com/play/raspberry-pi-nodejs/

    :)

    mrhappymac made it! (author)2016-03-27

    Great Primer Thanks! Everything worked great on My PI.

    vshymanskyy (author)2015-10-13

    As I noted, you should check that NODE_PATH environment variable is set and correct.

    Funguypro (author)vshymanskyy2015-11-27

    I got the same error and did the "export NODE_PATH......" thing and it still wont work. Any tips?

    AlexanderG36 (author)2015-11-16

    I wanted to use the LCD Widget and have done so:

    var lcd = new blynk.WidgetLCD(2);
    lcd.write(0,0, 'Hello world');

    But I only get this error after starting my script:

    var lcd = new blynk.WidgetLCD(2);
    ^
    TypeError: undefined is not a function
    at Object.<anonymous> (/home/pi/Projects/Nodejs_1/blynk-test.js:12:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

    What is wrong?

    Is the LCD Widget currently not supported by the nodejs library or do I use a wrong syntax.

    Is there a list of all supported commands?

    mac.mstc (author)2015-11-14

    at step 2 i am getting output like

    Connecting to SSL: cloud.blynk.cc 8441
    Connecting to SSL: cloud.blynk.cc 8441
    Connecting to SSL: cloud.blynk.cc 8441
    Connecting to SSL: cloud.blynk.cc 8441
    Connecting to SSL: cloud.blynk.cc 8441

    mdlawrence (author)2015-10-26

    Hi -

    I am trying to use some of the other widgets and get errors trying to use anything other than WidgetTerminal. For example, if I include the line:

    var led = new blynk.WidgetLED(7);

    I get the following error:

    var led = new blynk.WidgetLED(7);

    ^

    TypeError: undefined is not a function

    at Object.<anonymous> (/home/pi/blynk-test2.js:13:11)

    at Module._compile (module.js:460:26)

    at Object.Module._extensions..js (module.js:478:10)

    at Module.load (module.js:355:32)

    at Function.Module._load (module.js:310:12)

    at Function.Module.runMain (module.js:501:10)

    at startup (node.js:129:16)

    at node.js:814:3

    Any help on how to use the LED widget would be appreciated! Thanks.

    About This Instructable

    34,869views

    74favorites

    License:

    More by vshymanskyy:Raspberry Pi + Node.js + Blynk App + DHT11/DHT22/AM2302Blynk + JavaScript in 20 minutes [Raspberry Pi, Edison, Espruino ...]Control quadcopter from your phone in 30 minutes! [ESP8266 + A7105 + Blynk App for iOS/Android]
    Add instructable to: