Uploading code to your Arduino board remotely

A while ago I bought some Arduino UNO and Mega cards that featured, besides the usual processor, an on-board ESP8266 processor to bring Wi-Fi connectivity to them. These boards, have DIP switches to be able to program either the AVR (Arduino) processor or the ESP8266 using the USB port (think of them as a sort of serial port multiplexer).

The main use for me was to be able to use them for different applications without using a USB cable. A [Wi-Fi]  TCP connection to port 23 would be used instead. As I am using ESP-Link firmware for the ESP8266.

Do you want to print wirelessly on your 3D printer? You can do that with one of these Arduino Mega boards that can use Marlin. Using Pronterface software you can print to a TCP socket the same as if you print to a serial port.

However, what intrigued me is that no mention was made in the boards I use about using the ESP8266 to upload new programs to the Arduino processor. And that was odd as ESP-Link software allows you to do that. However, in order for this to work the ESP8266 needs to be able to cause a RESET to the AVR processor. Here is where these boards do not perform, as the schematic shows a single RESET button that resets both processors at once and no additional ESP8266 GPIO pins that could do the job.

There are, however, several free GPIO pins that could be used to the job, however, there is a diode that joins the two RESET circuits, that though they work at the same time, they have the steady state as a high level, which is 5V for the AVR but 3.3V for the ESP (thus the diode, that will allow the ESP to be reset when the Arduino RESET pushbutton is pressed).

But for my goal of being able to reprogram the Arduino flash memory, I do not need nor want the ESP to be reset during that process.


I can select one of the spare GPIO pins on the ESP-link configuration page, that will be the one that will cause the RESET (will go low for a short period of time). But for that not to cause a RESET to the ESP a diode has to be removed from the circuit (D3 on the UNO).

But, it turns out, the manufacturer did some savings and failed to populate capacitor C11. That was not a problem until you remove D3 (marked with the red X), then the ESP stops having proper reset. I added a 47nF (marked with the red circle) to fix the problem.


Finally, I need to connect the GPIO pin of the ESP to the RESET pin of the AVR but ... they use different logic levels. Some pins of the ESP8266 can support 5V but outputs will die if shorted. I opted for an unconventional solution that works for me: just a diode, connecting the anode to the RESET pin, and its cathode to the selected GPIO pin. When the GPIO goes to zero, it will reset the AVR. It is true that more than 3.3V can be presented to that GPIO pin but that does not really happen as the resistor to +5V limits the current and the RESET pin only reaches 3.75V, thus keeping the GPIO pin at 3.3V. You may recycle the same diode D3 you are removing from the board. 


Unfortunately, as you can see in the image above, I placed the diode the wrong way, and I destroyed GPIO012 once I pressed the RESET pushbutton. Luckily some other GPIO was available, so I fixed the diode polarity and used GPIO04 instead. 

The last past was to use avrdude to test the new acquired wireless upload capability. I connected my computer to the nextwork ESP-link was created acting as an access point. The magic words here were: 

$ avrdude -DV -pm328p   -Pnet:192.168.4.1:23 -carduino -b115200  -U flash:w:grblUpload.hex:i  -C avrdude.conf 

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "grblUpload.hex"
avrdude: writing flash (29832 bytes):

Writing | ################################################## | 100% 5.45s

avrdude: 29832 bytes of flash written

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

It works most of the time, but when it fails it gives an error message quickly (perhaps reset lasts a bit more or less than it should). And the wireless upload speed is on par with the USB connection, at least with a good Wi-Fi connection.

Comments

Popular posts from this blog

VFD control with Arduino using RS485 link

How to get sinusoidal s-curve for a stepper motor

Importing OpenSCAD designs into Onshape