Uploading code wirelessly to Arduino Mega
Given the same thing worked ok using an Arduino UNO form-factor board with both an AVR 328p and an ESP8266, I was expecting not much trouble getting the same thing to work for a similar board in the Arduino Mega format.
Oh boy, I was wrong!
The hardware modification part was easy: Find the diode whose cathode is connected to the RESET pin and remove it from the board. (Board schematic)
I used GPIO12 as the ESP8266 pin to be used to reset the AVR processor (key for a remote upload).
But once the hardware was working, it became obvious the software side of things was just not working. I could see no communication was ever successful between the ATMega2560 and avrdude program.
Reset generation was not a problem as it was clear remote reset was working ok. Perhaps timing? Let us read about possible issues on github. It turns out this is old news, as ESP-Link is playing a trick to detect the attempt of a firmware upload so it can reset the AVR chip then. That detection was limited to the m328p and not the atmega2560. But even if you press RESET button manually that is not enough to get it all running.
I was using latest stable version of ESP-link 3.0.14 and that is known no to support uploads of code to the Mega, but the new alpha 3.2.42 is, so I upgraded it. Still no dice.
A bit more reading shown that older bootloaders may not work as only stk500v2 is supported, so it was time to do so ISP programming with the help of an Arduino UNO so I could upload the bootleader on the Mega2560. In case you need to do that the connections are as follows:
Oh boy, I was wrong!
The hardware modification part was easy: Find the diode whose cathode is connected to the RESET pin and remove it from the board. (Board schematic)
I used GPIO12 as the ESP8266 pin to be used to reset the AVR processor (key for a remote upload).
But once the hardware was working, it became obvious the software side of things was just not working. I could see no communication was ever successful between the ATMega2560 and avrdude program.
Reset generation was not a problem as it was clear remote reset was working ok. Perhaps timing? Let us read about possible issues on github. It turns out this is old news, as ESP-Link is playing a trick to detect the attempt of a firmware upload so it can reset the AVR chip then. That detection was limited to the m328p and not the atmega2560. But even if you press RESET button manually that is not enough to get it all running.
I was using latest stable version of ESP-link 3.0.14 and that is known no to support uploads of code to the Mega, but the new alpha 3.2.42 is, so I upgraded it. Still no dice.
A bit more reading shown that older bootloaders may not work as only stk500v2 is supported, so it was time to do so ISP programming with the help of an Arduino UNO so I could upload the bootleader on the Mega2560. In case you need to do that the connections are as follows:
- D10 on the UNO --> RESET on the Mega
- D11 --> D51
- D12 --> D50
- D13 --> D52
Do not forget that GNDs need to be connected (no need if both boards are powered by the same computer). The Arduino UNO needs to be running the Arduino ISP example and the IDE needs to be configured with the bootloader programmer to be "Arduino as ISP". Next, make sure you have selected the board to be Arduino Mega 2560 and go to the tools menu and select Burn bootloader.
Once a new bootloader was in place I expected all to work, but it did not. And yes, I placed back the proper DIP switches on the board (1&2=ON). With a Blink example running on the Mega I realized now the AVR was not being reset when a programming attempt was made using:
avrdude -DV -patmega2560 -Pnet:192.168.4.1:23 -cstk500v2 -b 115200 -U flash:w:Marlin.hex:i
avrdude -DV -patmega2560 -Pnet:192.168.4.1:23 -cstk500v2 -b 115200 -U flash:w:Marlin.hex:i
I realized that I could force a reset by connecting to port 2323 instead of 23, but while that did cause a reset, the communication was still not working. Finally, I learned that some people used curl to cause a remote reset with the next command-line: curl --data '' http://192.168.4.1/console/reset
so I tried to prepend that to my previous command. It finally worked!!
so I tried to prepend that to my previous command. It finally worked!!
So the complete command-line to remotely program an Arduino Mega with a given hex file (assuming you are using the ESP in AP Mode, thus the IP address I am using below):
curl --data '' http://192.168.4.1/console/reset ; avrdude -DV -p atmega2560 -P net:192.168.4.1:23 -c stk500v2 -b 115200 -U flash:w:Marlin.hex:i
Comments