Introduction: 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:
- Check if JavaScript is installed on your device
- Install Blynk library: https://github.com/vshymanskyy/blynk-library-js
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:
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:
- https://software.intel.com/en-us/creating-bootable...
- 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:
- 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: 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! ;)
17 Comments
2 years ago
I'm taking
"bash: line 233: syntax error: unexpected end of file"
at the
"curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bas"
what should I do??
Question 4 years ago
hello,
I follow instructions and the IOT work well. now I try to reconnect the hardware each time my pi restart.
I modify my rc.local and the last line for connecting the hardware don't execute correctly.
have you some idea for write correctly the line ?
node /home/pi/python/index.js '42fcbb1715e64bc19cb520a1045ddd5c' &
thank.
---------------------------------------------------------------------------------
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# start serveur blynk
java -jar /home/pi/serveur/server-0.41.2-java8.jar -dataFolder /home/pi/serveur/data -serverConfig /home/pi/serveur/server.properties -mailConfig /home/pi/serveur/mail.properties &
# start dyndns for static IP from internet
/usr/local/bin/noip2 &
# start hardware compatible with blynk appliaction
node /home/pi/python/index.js '42fcbb1715e64bc19cb520a1045ddd5c' &
exit 0
regards,
Joel14600fr
Question 4 years ago
I'm trying to follow the steps given here but get the following error on both my windows 7 laptop as well as my RaspberryPi 3B. Both connect to my home WiFi network which inturn is connected to my ADSL modem.
C:\Users\mojo>blynk-client <my auth token>
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/
Give Blynk a Github star! => https://github.com/vshymanskyy/blynk-library-js
Connecting to: blynk-cloud.com 8441
{ Error: connect ECONNREFUSED 188.166.206.43:8441
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '188.166.206.43',
port: 8441 }
On my laptop I am able to run blynk-ser successfully to relay data from an Arduino UNO connected to the laptop's USB port. Any help would be appreciated. Thanks
4 years ago
If you get "permission denied" isues with installing the onoff module...
--unsafe-perm flag should fix your issue according to: nodejs/node-gyp#454
This is definitely not a good solution. But for now --unsafe-perm should do the trick
5 years ago
where do we save this file ?
5 years ago
Basic question: Where do you save the "blynk-test.js" file?
Reply 5 years ago
yes I have the same question. where do I save this file?
5 years ago
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.
6 years ago
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?
Reply 6 years ago
sudo apt install npm
6 years ago
does pi zero work?
7 years ago
I get the error cannot locate bindings file. When I try to run
node example.js
Everything is installed npm,onoff,blynk-library
Reply 6 years ago
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/
:)
7 years ago
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?
7 years ago
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
7 years ago
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.
7 years ago
As I noted, you should check that NODE_PATH environment variable is set and correct.