![]() |
In the previous tutorial, we discovered the Johnny-Five framework that allows to create connected objects (and more) based on Arduino, Raspberry Pi (or Orange Pi) in Javascript. Johnny-Five is also a very good solution to replace the GPIO of an Orange Pi by an Arduino Nano v3. In the previous tutorials, we used an Arduino connected to a USB port. In this tutorial, we will use the WiFi Firmata (StandardFirmataWiFi) available from the Arduino IDE which allows you to communicate with an ESP8266 in Wi-Fi.
Why not use the Firmware Serial UDP project?
If you do an internet search with the ESP8266 Johnny-Five keywords, you will also find this Serial UDP firmware project. This firmware is available in the form of a binary which installs with esptool.py of Espressif (read this article for more information). You will first need to install Python 2.7 on your computer. I have encountered difficulties connecting to an ESP8266. This is probably a configuration error. Since the source code is not offered on GitHub (or another platform), I advise you to use the firmwareWiFi that works very well. It is simpler to install and is fully configurable.
Install the StandardFirmataWiFi firmware on the ESP8266
The StandardFirmataWiFi firmware provides access to all Arduino features via a TCP serial link. It is compatible with Arduino shields, Shield 101, MKR1000 boards and all ESP8266. For example, you can add an ESP-01 to an Arduino Uno, which is much cheaper than a WiFi Shield.
Connect the ESP8266 to the computer and open the Arduino IDE. In the examples menu you will find a submenu named Firmata. Select the StandardFirmataWiFi firmware.
By default, debugging is disabled. Look for the line containing the string #define SERIAL_DEBUG and remove comments (//).
Then go to the wifiConfig.h file to configure the connection parameters
- Step 3 (or look for ssid): Specify the WiFi network to which the ESP8266 must connect
- Step 4: Specifies a fixed IP (STATIC_IP_ADDRESS). In this case, the subnet mask (SUBNET_MASK) and the IP of the router (GATEWAY_IP_ADDRESS)
- Step 5: SERVER_PORT, the server connection port
- Step 6: The security type of the WiFi network. Defaults to WPA (WIFI_WPA_SECURITY). Complete the password corresponding to the security type (wpa_passphrase for WPA).
The configuration is complete. Upload the program, it’s over!
To verify that everything is correct, open the 9600 baud serial monitor. You can also retrieve the IP address of the ESP8266 if you have not set it.
Example: driving a led, reading a BMP180 in WiFi
Let’s go to the test now. We will take the previous example and adapt it to the new configuration.
Necessary material
Wemos D1 Mini | |
5V/3A micro-usb power supply | |
BMP180 | |
Led | |
220Ω resistor | |
Jumper Dupont | |
Breadboard |
Circuit
The LED is connected to pin D3 (GPIO-0). On the Wemos D1 mini, the I2C bus (SDA – D2, SCL – D1).
Javascript code
To establish communication with the ESP8266 we will install the npm etherport-client package (package page)
sudo npm install firmata etherport-client
After the installation is complete, create a new
cd ../.. cd nodebot nano j5ESPDemo.js
At the beginning of the script, we will initialize the object that you allow us to establish the serial connection with the ESP8266 in WiFi
var EtherPortClient = require("etherport-client").EtherPortClient;
To connect to the ESP8266, just create an EtherPortClient object. This object requires two parameters, the IP address of the ESP and the port.
var board = new five.Board({ port: new EtherPortClient({ host: "xxx.xxx.xxx.xxx", // IP ESP8266 port: 3030 }), timeout: 10000, repl: false });
For the rest, the programming is perfectly identical to the previous tutorials.
Why is the npm Firmata package not used?
On the internet, you will find many tutorials that establish communication through the Firmata package. This package allows you to control the GPIO of the Arduino in javascript. This is also the goal of the Johnny-Five framework. The complete API for managing the board is detailed here. In my opinion, it is therefore not necessary to add this “over-layer” to your projects. I did not encounter any difficulties communicating in I2C with the BMP180 and driving the GPIO from the Blynk application.
ESP8266 + Johnny-Five + Blynk
Let’s go one step further by integrating the ESP8266 into Blynk. Thanks to Johnny-Five you will not have any programming to do on the ESP8266. If you’re starting out with Blynk, start here. Here is a diagram showing the architecture of the system. Everything happens in WiFi:
- The Blynk application runs on a WiFi-connected smartphone (or tablet) to the local Blynk server
- The Blynk server runs on a Raspberry Pi 3 connected to the WiFi local area network.
- The javascript code can be run on the Blynk server. Here it runs on an Orange Pi Lite (WiFi, 512MB) itself connected via WiFi to the local Blynk server.
- The Wemos d1 mini runs on battery power using a battery shield and a 1100mAh LiPo battery.
Project Bl- ynk
Create a new project and add:
- 2 Gauges (V0 for temperature and V1 for barometer)
- 1 switch type switch (V2)
Javascript code
Paste the project code into a new script by changing the following parameters:
- Token: the token of the Blynk project received by email
- IP of the local Blynk server (how to install a local Blynk server on Raspberry Pi)
- IP ESP8266
- Port
var Blynk = require('blynk-library'); var five = require("johnny-five"); var EventEmitter = require('events').EventEmitter; var EtherPortClient = require("etherport-client").EtherPortClient; var AUTH = 'b065eb0a6e36434da42367b3fa7c3340'; // Remplacer par votre Token Blynk - Replace by your Blynk Token var event = new EventEmitter(); // Evenements javascript - Javascript Events var DEBUG = true; //false; // Active les messages de mise au point - Activate debug message // Setup Blynk var blynk = new Blynk.Blynk(AUTH, options = { // Connecteur au serveur Blynk local - Local Blynk server connector connector : new Blynk.TcpClient( options = { addr:"IP_SERVEUR_BLYNK", port:8442 } ) }); var V0 = new blynk.VirtualPin(0); // Temperature var V1 = new blynk.VirtualPin(1); // Barometer var V2 = new blynk.VirtualPin(2); // On/Off led var temp, pa; blynk.on('connect', function() { console.log("Blynk ready."); }); blynk.on('disconnect', function() { console.log("DISCONNECT"); }); // update host to the IP address for your ESP board var board = new five.Board({ port: new EtherPortClient({ host: "192.168.1.73", port: 3030 }), timeout: 10000, repl: true, debug: true }); board.on("ready", function() { console.log("READY!"); var led = new five.Led(0); var bmp180 = new five.Multi({ controller: "BMP180", freq: 5000 }); bmp180.on("change", function() { console.log("Temperature BMP180 ", this.thermometer.celsius, "°C"); console.log("Atm. Pressure BMP180 ", this.barometer.pressure * 10 , "hPa"); temp = this.thermometer.celsius; pa = this.barometer.pressure * 10; }); board.on("exit", function(){ led.stop(); led.off(); }); event.on('V2', function(param){ if ( param == 1 || param == true ) { if ( DEBUG ) { console.log("Start strobe led"); } led.strobe(500); } else { if ( DEBUG ) { console.log("Stop strobe Led"); } led.stop(); led.off(); } }); }); setInterval(function() { if ( temp != undefined ) { if ( DEBUG ) { console.log('Temperature:', temp + ' C'); } V0.write(temp); } if ( pa != undefined ) { if ( DEBUG ) { console.log('Humidity: ', pa + ' hPa'); } V1.write(pa); } }, 5000); V2.on('write', function(param){ if ( DEBUG ) { console.log("V2 ", param); } event.emit('V2',param); });
Save with CTRL + X then Y and then run the script node j5ESPDemo.js
node j5ESPBlink.js Connecting to TCP: 192.168.1.24 8442 1491226899573 SerialPort Connecting to host:port: 192.168.1.73:3030 1491226899605 Connected Connecting to host:port: 192.168.1.73:3030 Connected Authorized Blynk ready. 1491226905821 Repl Initialized >> READY! Temperature BMP180 20.9 °C Atm. Pressure BMP180 983.7 hPa V0 [ '1' ] Start strobe led Temperature BMP180 20.9 °C Atm. Pressure BMP180 983.67 hPa V0 [ '0' ] Stop strobe Led Temperature BMP180 20.9 °C Atm. Pressure BMP180 983.63 hPa
As you can see in this photograph, the Wemos works on battery (1100 mAh). This is not an ideal situation because you can not take advantage of the ESP8266 standby. This is a good example of architecture that could be set up to develop a remote control.
Here, by installing the StandardFirmataWiFi firmware we cut the cord between the computer (or the mini PC Raspberry Pi …) and the ESP8266. In a few lines of code, it is very simple to drive or recover measurements from an ESP8266 in WiFi.
- Connecting an ESP8266 to Blynk on WiFi with Johnny-Five (Firmata WiFi)
- IoT development based on Orange Pi, Arduino (Firmata), Nodejs, Blynk and Johnny-Five
- Start programming with Node Js and Johnny-Five: IoT and robotics based on Arduino, Raspberry Pi, Orange Pi
- Orange Pi (Armbian): replace the GPIO by an Arduino/Firmata, Node-RED and Johnny-Five