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

  1. Launch Arduino IDE
  2. Open Preferences
  3. 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.
  4. Click OK
  5. Navigate to menu Tools > Board.. > Boards Manager
  6. Find esp8266 by ESP8266 Community in the list. Click Install.
  7. Once this is complete, click OK.

Flash Dev Board

  1. Plug in dev board via USB to your computer.

  2. Back in Arduino IDE, in menu Tools > Board, select “Adafruit Feather Huzzah ESP8266” (or your appropriate dev board) from the list.

  3. 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.
  4. In menu File > Examples … Examples for any board > Firmata choose StandardFirmataWifi.

  5. Modify this sketch by uncommenting line 85: #define SERIAL_DEBUG. This will allow you to view debug output in Arduino IDE's Serial Monitor.

  6. There will be a tab for a file wifiConfig.h. Click this tab to open wifiConfig.h.

  7. On line 119, enter the name of your WiFi network (in double quotes), e.g., char ssid[] = "foobar"; where foobar is your WiFi network name.

  8. 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.

  9. Click the Upload button (icon is a “right arrow”) in the toolbar. This should flash your board with the firmware.

  10. To confirm things are working, click the Serial Monitor button in the toolbar (icon is a magnifying glass).

  11. Change the baud rate to 9600

  12. 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!

  13. Close the serial monitor window.

Install Johnny-Five & Node.js Modules

  1. Create a new directory.
  2. Run npm init -y to generate an empty package.json.
  3. Execute npm install johnny-five etherport-client.
  4. 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).

  1. 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!