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

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:

  1. 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 +++.
  2. 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.
  3. 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:

  1. Build modified AT firmware for the ESP8266.
  2. Program the new firmware onto the module.
  3. Compile the modified bootloader and flash it to the atmega using a programmer. I use the USBasp or the buspirate
  4. Connect via WiFi to the ESP8266’s AP. You should get an IP address like 192.168.4.100
  5. Run the following command specifying the the correct *.hex file
    ./program_rodi.py /tmp/build7763862112848895248.tmp/Blink.cpp.hex
    

Hope you found it useful!

Happy hacking!

About Gary

I build robots for fun! View all posts by Gary

24 responses to “Programming an Arduino via WiFi with the ESP8266

  • Programming an Arduino via WiFi with the ESP8266 - RLabs

    […] Servin has been working on a new project a method to flash an Arduino via WiFi using the ESP8266.  He wrote a post on his blog detailing the […]

  • Mehmet

    I didn’t make it work. i flashed newest firmware. it seems esp server cannot start but i dont know why. your py script says connection refused.

    • Gary

      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.

      • Mehmet

        I connected with put and it is 192.168.4.1 and tried ping ip is correct. But it doesn’t connect socket client with port 1234 when I tried. I tried on arduino UNO and pro mini.

  • marcinozog

    It works with pro mini, but I had to try several times.

  • arnieo

    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!)

  • ximran

    Reblogged this on Imran's Tech Tips and commented:
    Programming an Arduino via WiFi with the ESP8266

  • Sébastien HEUZE

    Hello,
    Nice tutorial 🙂

    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)

    Do you know where this problem comes from ?

    Thanks

    • Gary

      Hi!

      did you modify the bootloader of your Arduino?

      • Sébastien HEUZE

        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)

        NOTE : For thoses who want to compile the ESP8266 Firmware with Windows, this is a very, very simple DevKit : http://www.esp8266.com/viewtopic.php?f=9&t=820

      • Gary

        Awesome!

        Also, the Optiboot does not have the 0x00!

    • Francesco Noto

      Hello can i have more information about your idea??
      I try this but not receive the “0” to reset arduino.
      look this picture..

      what did i write on python code instead of this ?
      if wait_for(cli, “\x00”, MAX_TIMEOUT)[0]:
      -………..

      Please answer me THANKS!

  • Emir Ercan Ayar

    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?

    • Gary

      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.

  • Dan Anbinder

    Great article.
    I am just starting to use the ESP8266 and I need the transperent mode for makeing an easy arduino to android communication.

    *I have tryed to follow your steps but the proccess is too complicated for me.

    Could you please post a compiled firmware for the ESP8266?

    Thank you in advance.

  • Programming an Arduino via WiFi with the ESP8266 -Use Arduino for Projects

    […] For more detail: Programming an Arduino via WiFi with the ESP8266 […]

  • Patrik Hermansson

    Hi!
    Interesting! But can you please explain step 1 a bit more? How do I include your at-mods in my firmware?

    Regards Patrik

  • yoguiti

    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

    • Gary

      Hi! the binary files that are on the repo are not generated by me, they’re the stock firmware so the custom commands won’t work. Unfortunately you need to compile the firmware.

  • Steph

    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.

    Many thanks

  • Steph

    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.

    Many thanks

  • Tobias

    Hi, I tried to program a Arduino Mega 2560 with this ESP8266 solution. However it didn’t work (I assume because of the STK500v2 protocol).

    However:
    I got my wifi solution finally running with a different solution (ESP-LINK):

    If you are interested – there is a very easy solution:

    Get a e.g. adafruit Wifi ESP8266 board (voltages etc. already fit for the mega).

    Get it flashed with the ESP-LINK firmware (this has the advantage no manual firmware compilation needed)
    https://github.com/jeelabs/esp-link/tree/master

    Push a new bootloader to the arduino (I used another Mega as programming device):
    https://github.com/nickgammon/arduino_sketches

    Done!

    If ESP8266 is wired correctly to the Mega you can now program it via Wifi with the following command line:
    ./megaflash.sh IP_OF_ESP Hex_File
    megaflash program is included in the ESP-LINK package…

    (most useful infos I got from the following page: https://github.com/jeelabs/esp-link/issues/87)

Leave a reply to Steph Cancel reply