Skip to content

Instantly share code, notes, and snippets.

@ajfisher
Last active October 10, 2021 00:51
Show Gist options
  • Save ajfisher/5fe60fe7d8c49b3223f0 to your computer and use it in GitHub Desktop.
Save ajfisher/5fe60fe7d8c49b3223f0 to your computer and use it in GitHub Desktop.
ESP8266 Transparent bridge to J5

#ESP8266 Transparent bridge to J5

How to use an ESP8266 as a transparent bridge to enable wifi between your arduino and johnny five. AKA, how to get rid of your serial cable and use WiFi instead!

Update: For a long time, this process used 5shekel's TCP transparent bridge which works okay but hasn't been updated much and with Johnny Five, TCP isn't the best protocol for this type of messaging.

As a result there was a desire to move to UDP instead for performance and this has been accomplished thanks to the hard work of Riven from MakeBlock, Mark Wolfe, Andy Gelme and Luis Montes.

ESP8266 Config

You need to make sure you have both esptool.py as well as esptool-ck. The first will help with testing you can talk to the board and erasing it and the second does a better job flashing to the ESP8266 module.

Put your ESP8266 in flash mode then

esptool.py --port /dev/ttyDEVICE erase_flash

It's important to wipe it clean so it gets rid of any user settings you might have. After that it's time to write the bins to the clean memory. The bins are attached to this gist and are built from Mark Wolfe's project.

esptool -cp /dev/ttyDEVICE -cd ck -ca 0x00000 -cf 0x00000.bin \
 -ca 0x40000 -cf 0x40000.bin

Reset the ESP8266 and drop out of flash mode and you should see an AP come up called NBD_XXXXXX where XXXXXX is the last 6 hex values of the mac address. So mine looks like NBD_FBE7C3. By default the network is open however you can configure that by connecting to the AP and then pointing a web browser at 192.168.4.1

Johnny Five setup

From Johnny Five we use the UDP-serial project to create a virtual serial port across UDP.

Dependencies:

npm install johnny-five firmata udp-serial

By default the address we want to talk to is 192.168.4.1 and the UDP port is 1025 so confiure them in the options.

Arduino setup

Load standard firmata and change the firmata speed to 115200 instead of 57600 ( search 57600 in the standardfirmata sketch - it's the only instance). Compile and upload to arduino.

Connect TX / RX pins to arduino and ESP01, add power to each one, grounds together.

Running it all

After that, run the script ncluded in this gist and it should work quite well you should get a nicely blinking light. Now use this structure to go make awesome wireless robots.

Acknowledgments:

Putting this together was made much easier by the people laid the foundations and also put up with me asking questions, asking for builds as my environment was toasted and generally being demanding due to the lead up to NodeBots Day.

  • Riven from MakeBlock who started down this path as part of NodeBots Day China but we ran out of time. This baseline is excellent to have worked from.
  • Luis Montes who provided insight on how the Johnny Five network stream would work and who quickly produced UDP-Serial such that the whole nodebots community can now work with this as an IO transport method.
  • Mark Wolfe who spent considerable time debugging what was happening and refined the build process so we now have a stable and replicable build environment to work on this further.
  • Andy Gelme who debugged a gnarly bridge issue that looked to be a critical blocker and refined the bridge code so that things are nice and zippy.

If you use this code and you like it, ping a message to those listed above because they have done an awesome job producing this in a very short time period.

'use strict';
var VirtualSerialPort = require('udp-serial').SerialPort;
var firmata = require('firmata');
var five = require("johnny-five");
//create the udp serialport and specify the host and port to connect to
var sp = new VirtualSerialPort({
host: '192.168.4.1',
type: 'udp4',
port: 1025
});
//use the serial port to send a command to a remote firmata(arduino) device
var io = new firmata.Board(sp);
io.once('ready', function(){
console.log('IO Ready');
io.isReady = true;
var board = new five.Board({io: io, repl: true});
board.on('ready', function(){
console.log('five ready');
//Full Johnny-Five support here:
var led = new five.Led(13);
led.blink();
});
});
@Pierrecl
Copy link

Pierrecl commented May 10, 2016

Hi ajfisher, thank you very much, i have found all i want.

@sarkarstanmoy, 192.168.4.1 is the default IP address assigned to ESP, it should be work.
But did you tried to setup a static IP ? For exemple you need to create a LUA file with ESPlorer to define your configuration, like this :

 wifi.setmode(wifi.STATION)
 wifi.sta.config("YOUR SSID","YOUR KEY")
 wifi.sta.connect()
 wifi.sta.setip({ip="IP ADRESS HERE",netmask="NETMASK HERE",gateway="GATEWAY HERE"})
 print("ESP8266 mode is: " .. wifi.getmode())
 print("The module MAC address is: " .. wifi.ap.getmac())
 print("Config IP is "..wifi.sta.getip())

and Send commands into ESP, if it works , you find the new IP assigned to ESP.

@nicnet
Copy link

nicnet commented May 11, 2016

@Pierrecl - did you actually try this, with the USB bridge installed?

@LabN36
Copy link

LabN36 commented Apr 28, 2017

can jonny-five run only on ESP8266 (ESP-12) without arduino ?

@relief-melone
Copy link

Hey guys. I've tried alot now but i can't seem to get io to the state ready. my esp8266 is connected to my lan, it responds to ping but it just won't ready up. Would be great if anyone had an idea how to get this to work.

@rikyoshiro
Copy link

Thank you for what you did. It is very easy now for me as a new-leaf to javascript to communicate with arduino etc.
But hey, I have a problem when I have to use temperature sensor ds18b20 that should use a configurable firmata. Is that somethingI can do about this?? It works well when I use the connector cable connected.

@Cayce2514
Copy link

Thank you for what you've done with this! I've incorporated this into a slightly re-engineered version of the simplebot from your “Building Robots with Lo-tech Materials” contribution to Make: JavaScript Robotics. (my adaptation here) We used your design and code, coached a bunch of kids through building their own bots and we sumoed with them for nodebot day 2017. Here's a video of them working

There are some occasions where the bot looses connectivity, it seems, and we have to break out of the script and re-run it to gain control of the bot. Have you experienced this? Any ideas for how best to ensure that the bot doesn't go AWOL?

Thank you again!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment