Programming an Arduino via WiFi with the ESP8266

Foreword

This is experimental and not exactly for beginners, something may be wrong. Proceed with caution or you can break something!

A (not so) brief introduction.

I’ve been working on a new robot called RoDI for a summer course at my Alma mater. RoDI is a low cost wireless robot to teach robotics and programming I started developing last year (reminder to self: I still need to post about it), but it wasn’t until a month ago that I started to work on it more often.

The first version used a HC-06 Bluetooth module to communicate with the computer. I wanted to to use WiFi, but the cost of the WiFi modules was a problem, since the idea was that the entire robot would cost less than 30 USD.

Then, I read about the ESP8266 WiFi modules on Hackaday, and started working on a new version of RoDI, this time with WiFi :D. I wanted to be able to flash the atmega328 inside the robot via WiFi because the robot doesn’t have a USB to Serial converter.

I found (again on HaD) a blog that showed how to flash an Arduino via WiFi with these modules, but it uses the ESP8266 to connect to a router, as we’re planning to have a few of this robots, and the student will take these robot and it doesn’t have another way of flashing the chip unless they have a programmer, so this method wouldn’t work for the robots. One thing that I learn from the blog was that I was going to need to use the transparent mode and, of course, use the module as an AP. Bad news, transparent mode is not available when using the module in server mode with the at_v0.20_on_SDKv0.9.3 version of the at command example. Time for hacking it!

After reading through the source code for the AT commands firmware I was able to enable transparent mode when using the module as a server. One security issue for this is that it supports up to 5 connections, so is not completely secure. I asked and got this response but wasn’t able to find that method (or variable?). I found another method that consist on configuring the AP using the wifi_softap_get_config method from the SDK, which lets us set the max_connection parameter. Yet another option is to add a password to the AP. But… we plan to use it only for robots in a summer course, so the security isn’t really important for now. 😉

Let’s get to work…

Connecting all together

CAUTION: This assumes you’re using a 3.3V Arduino. I’m not responsible for any broken Arduino or ESP8266 modules.

Simplified connection diagram

Mods

We need to do some modifications to the ESP8266 AT firmware, arduino bootloader and the programmer software in order to successfully use it to program the Arduino, here are the mods:

I’ve added a new AT command called AT+RODI that set the module as an AP, starts the transparent server mode, configures the GPIO used for resetting the Arduino and starts a TCP server on port 1234. You can find the code on my esp8266_at fork on my github account. For a guide on how to install the toolchain, you can follow this guide. And for a script to build and program the ESP8266 you can take a look here. Note: to enter AT mode, send +++.

Then I’ve modified Arduino’s bootloader to send the ” character so it can be picked up by the programmer script. You can find the modified code here. (I think this is not entirely necessary but made things easier for me)Update: After some testing, I found out that there is not necessary to modify the bootloader, you can omit this step.

Lastly I’ve modified the python script programmer from sistemasorp to add support so it can work as a client connecting to the ESP8266 for programming. You can find the modified code here.

So, the steps needed are:

Build modified AT firmware for the ESP8266.

Program the new firmware onto the module.

Compile the modified bootloader and flash it to the atmega using a programmer. I use the USBasp or the buspirate

Connect via WiFi to the ESP8266’s AP. You should get an IP address like 192.168.4.100

Hi, can you try the following?
* connecting the esp module to a usb-serial cable
* open a terminal emulator (putty or minicom)
* connect to the module and check you have the correct IP address (try to ping 192.168.4.1)
* If you have ping, try running: nc 192.168.4.1 1234 in another terminal
* try sending some data, you should see data in minicom/putty

Also, what type of arduino are you using? Make sure the bootloader works at 57600 or it will not work.

Great work, guys! I spent hours yesterday working on implementing the work done by sistemasorp in my system, but so far the python code stops with an error message when the Arduino is reset.
Updating the ESO8266 firmware with a new AT command sounds like an interesting solution – Would it be possible to have the binary for that firmware update?
(I’m trying to avoid setting up the toolchain and then understand how to use it… so thank you in advance!)

I’m trying to do it with Arduino UNO R3, my ESP8266 is now under SDK0.95 with your mod.
But the python programmer does not work it say cannot connect to bootloader. (the led L is blinking to the reset “work”)
I have tried to send the bootloader command manually the led L blink but I don’t have any answer back from arduino (the expected x00)

Hi ! 🙂
No, my Bootloader is the default Bootloader and my Arduino the Original (UNO R3)
The Arduino is 2-3 years old maybe they changed the bootloader, I can’t find any information about it.

I posted another message one day after this one because it works fine now when I remove the 0x00 in the python script to replace it with a 500ms timer (Since the WatchDogTimeOut is 1sec)
Maybe the message is still on the moderation waiting line or maybe I forgot to click on “Post comment”

So this is weird, don’t know why my Arduino does not send 0x00 after reset and I can’t find it in the bootloader source (Optiboot)

Thanks for the post, You said, “One thing that I learn from the blog was that I was going to need to use the transparent mode…” My question is, what is exact function of transparent mode? Why should we use it?

Hi! The default AT firmware that comes with the module works by using the AT commands to send (AT+SEND,;) and to you get a “header” (+IPD) when you recive data. When you enter transparent mode, everything you write to the serial port will be send (no AT command or header needed). In order program the Arduino via the bootloader, and without modifying the bootloader firmware, you need the transparent mode.

Hi !
I followed your tutorial. However I haven’t compiled the ESP8266 firmware but I just flashed the *bin files you provided in github. It works fine and I can use AT commands. However, AT+RODI is not recognised … which is problematic… Could you help me ?
Thanks a lot,
Yoguiti

Hi, first, many thanks for the work you did (you and sistemasorp). Can anybody provide the compiled bin of the at bootloader with the AT+RODI command ? Ok, I know I’m lazy but I don’t want to install a full toolchain stack – and I guess I’m not the only one -. Would be cool if someone who wants to give it a try (me for instance 🙂 ) could simply download the bootloader and test it.

Hi, first, many thanks for the work you did (you and sistemasorp). Can anybody provide the compiled bin of the AT firmware with the AT+RODI command ? Ok, I know I’m lazy but I don’t want to install a full toolchain stack – and I guess I’m not the only one -. Would be nice if someone who wants to give it a try (me for instance 🙂 ) could simply download the firmware and test it.