How to use an ESP8266 with Johnny-Five
I wrote this short tutorial for a PDXNode “Nodebots” hackathon. It should work on Windows, Mac, and Linux boxes. I'm reposting it here, since it may be useful.
Prerequisite Software
- Node.js v8.x
- Arduino IDE v1.8.5
- CP2104 driver (found linked here; if your dev board needs a different driver, install that instead)
A toolchain to build native modules may be required. windows-build-tools may be the quickest way to get setup on Windows. macOS users will need to install XCode; Linux users will need to install build-essential
, python
, and likely some other stuff.
You're welcome to try a newer (or older version) of Node.js--likewise Arduino IDE--but YMMV. The firmware can also be flashed via means other than Arduino IDE, if you are so inclined.
@christianmello writes that macOS users may be able to install the drivers via Homebrew:
$ brew tap homebrew/cask-drivers $ brew cask install silicon-labs-vcp-driver
Add ESP8266 Board Support to Arduino IDE
- Launch Arduino IDE
- Open Preferences
- Add
http://arduino.esp8266.com/stable/package_esp8266com_index.json
to the Additional Boards Manager URLs input.
If you have something in this field already, you can add multiple URLs by delimiting with commas. - Click OK
- Navigate to menu Tools > Board.. > Boards Manager
- Find esp8266 by ESP8266 Community in the list. Click Install.
- Once this is complete, click OK.
Flash Dev Board
-
Plug in dev board via USB to your computer.
-
Back in Arduino IDE, in menu Tools > Board, select “Adafruit Feather Huzzah ESP8266” (or your appropriate dev board) from the list.
-
In menu Tools > Port, select the proper port.
- On Windows, this will be
COMx
. - On Linux, this will likely look like
/dev/ttyUSBx
. - On Mac, this will likely look like
/dev/tty.xxxxx
. - If you only see Bluetooth-related stuff, or otherwise can't find an appropriate port, ensure your driver is working, check your USB cable, check your dev board, etc.
- On Windows, this will be
-
In menu File > Examples … Examples for any board > Firmata choose StandardFirmataWifi.
-
Modify this sketch by uncommenting line 85:
#define SERIAL_DEBUG
. This will allow you to view debug output in Arduino IDE's Serial Monitor. -
There will be a tab for a file
wifiConfig.h
. Click this tab to openwifiConfig.h
. -
On line 119, enter the name of your WiFi network (in double quotes), e.g.,
char ssid[] = "foobar";
wherefoobar
is your WiFi network name. -
If using a secured network (requiring a password), on line 151, enter the WiFi network password in double quotes, e.g.
char wpa_passphrase[] = "foobar-password";
. Otherwise, uncomment line 183 for an unsecured network. -
Click the Upload button (icon is a “right arrow”) in the toolbar. This should flash your board with the firmware.
-
To confirm things are working, click the Serial Monitor button in the toolbar (icon is a magnifying glass).
-
Change the baud rate to 9600
-
Assert that you see something like:
connected with SON OF ZOLTAR, channel 11 dhcp client start... ip:10.0.0.49,mask:255.255.255.0,gw:10.0.0.1 IP will be requested from DHCP ... Attempting to connect to WPA SSID: SON OF ZOLTAR WiFi setup done scandone .SSID: SON OF ZOLTAR IP Address: 10.0.0.49 signal strength (RSSI): -39 dBm
…where
SON OF ZOLTAR
is your WiFi network’s name.Note and/or copy the IP address. You'll need it later. This is important!
-
Close the serial monitor window.
Install Johnny-Five & Node.js Modules
- Create a new directory.
- Run
npm init -y
to generate an emptypackage.json
. - Execute
npm install johnny-five etherport-client
. - Create
blink.js
:
'use strict';
const {
EtherPortClient
} = require('etherport-client');
const five = require('johnny-five');
const board = new five.Board({
port: new EtherPortClient({
host: '10.0.0.49',
port: 3030
}),
repl: false
});
const LED_PIN = 2;
board.on('ready', () => {
board.pinMode(LED_PIN, five.Pin.OUTPUT);
// the Led class was acting hinky, so just using Pin here
const pin = five.Pin(LED_PIN);
let value = 0;
setInterval(() => {
if (value) {
pin.high();
value = 0;
} else {
pin.low();
value = 1;
}
}, 500);
});
Replace 10.0.0.49
with the IP address you noted from the serial monitor.
You may need to change LED_PIN
to 13
. I forget what the number is for the builtin LED on Huzzah (this guide was tested on a Wemos D1 Mini, which is functionally equivalent in most ways which matter).
- Save this file, and execute it. You should see something like this:
$ node hello.js
1532643089553 SerialPort Connecting to host:port: 10.0.0.49:3030
1532643089556 Connected Connecting to host:port: 10.0.0.49:3030
You should also see the onboard LED blink, toggling on and off every half-second. When satisfied, hit Ctrl-C
to quit.
Any corrections or improvements appreciated!