Skip to content

Instantly share code, notes, and snippets.

@ajfisher
Last active October 10, 2021 00:51
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • 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();
});
});
@rodrigoadachi
Copy link

Hello, very good job, then I change the mode to season, how can I set the ip since it does not catch from dhcp. Thank you ...

@JerksonS
Copy link

@ajfisher, While I do agree that the ESP's are cheap and therefore not a big deal if they have to be replaced, I would disagree that the TX line is the only one of concern. Actually, I would suggest the opposite to be more true; the RX line on the Arduino will be more damaging. It is held high for quite a few seconds during Arduino boot whereas the standard serial comms, as you say, hold it high for a very minimal duration.

I'll live on the wild side and just connect them directly for now and see how long my ESP-12 lasts. Thanks again.

@Shane-Lester
Copy link

Can someone help me, please. I'm trying to use esptool-ck on Windows 10 and can't move forward. Do I need to build it? I've installed MinGW and tried using the make utility but I'm having no luck.

Any help would be appreciated- the README.md just tells me it can be used for Windows.

Thanks

@raskaman
Copy link

raskaman commented Feb 4, 2016

On WIndows, you havr to use this tool https://github.com/themadinventor/esptool
Install python27, then run as explain in the post

@raskaman
Copy link

raskaman commented Feb 4, 2016

@ajfisher, I got everything working as explained, was able to control arduino over wifi, the only problem I am having is the esp8266 is not connecting to some access points with WPA/WPA2 security or any kind of password protected like WEP, it did connect initially, but after connecting to other network, it does not connect again to the first network, even after updating with the correct credentials.

@rajy4683
Copy link

rajy4683 commented Apr 9, 2016

@ajfisher/all,
I have added the images to the ESP8266. I am able to see that the ESP8266 is able to come up with the image and it also connects to my WiFi network and acquires a local IP such as 192.168.0.10.
When I connect to the WIFI AP, I don't get an IP, however, I have managed using a static IP such as 192.168.4.20. I am also able to access the webpage @192.168.4.1 after that.
However, when I run the sample js application bundled with the package, I am unable to connect to the ESP-8266. Below are my queries:

  1. What IP should I input in the host section below?
    var sp = new VirtualSerialPort({
    host: '192.168.4.1',
  2. Should I be connected to the ESP_xxxx network and have the default IP i.e 192.168.4.1 in the host part i.e
  3. Should I connect to my other wifi network and input the IP that ESP_xxxx received from my wifi network i.e 192.168.0.10

Please advise as I am kinda stuck while completing this exciting project.

@nicnet
Copy link

nicnet commented Apr 26, 2016

This is really nice - thanks
I'm trying to get it working with node-red (node-red-contrib-gpio), but configuring the node with UDP broadcast doesn't seem to work (hangs on "connecting").
Any pointers?

@nicnet
Copy link

nicnet commented May 2, 2016

The author of node-red-contrib-gpio has fixed this (configuring the node with UDP broadcast) now.
See https://gitter.im/monteslu/node-red-contrib-gpio

@nicnet
Copy link

nicnet commented May 2, 2016

I need a little help using this fantastic udp bridge.
I'm using it with node-red, bridging from firmata on arduino pro-mini.

It works great for that no problem.
The problem I do have is that I can't seem to get it to connect to my wifi AP in station mode.
In the "join setup" menu, there is place for AP and password.
However, when I put my details in there, it just causes it to crash and needs reflashing to get it to work again.

So the only way I can access it is using softAP mode, with static address always 192.168.4.1.
This is a bit of a problem.

Is it possible to get it to connect properly in station mode, either with DHCP, or static address is also fine.

@sarkarstanmoy
Copy link

sarkarstanmoy commented May 3, 2016

hi,

I am able to follow all your steps and able to browse 192.168.4.1 by connecting ESP_**** AP. After that I configure my ESP module by providing SSID and password. So now I having two ip address 192.168.2.6 assigned by my router and 192.168.4.1. Which ip address to use in nodejs ? Both the ip is not working.

Please help me out on this.

@nicnet
Copy link

nicnet commented May 4, 2016

The problem seems to be security.
Create an open AP and connect it with that - it worked for me.
So I now have an additional open network, that uses only MAC filtering for security - a pain, but not a huge one.

@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