If you’ve successfully written and uploaded a piece of C code for the ESP8266 in any of its’ guises such as the ESP-01, ESP-03 or ESP-12 modules, then you must also be familiar with the process of holding GPIO0 low then applying a reset to get it into upload mode… it’s a PAIN IN THE REAR!!
If you’ve attempted anything more than a simple flashing light program for the ESP8266, then you have probably become frustrated or bored to tears with this process. This goes especially for anyone who is used to programming with the Arduino IDE, where programming the board is a as simple as pressing the upload sketch button. Well, weep no more, salvation is at hand in the shape of another Atmel processor, the TINY85. This chip is a few tens of pennies from Mouser, RS, Farnell or Ebay and we have used it to handle the programming pins for you.
The process is very simple. When you attempt to upload your complied C program using the Espressif SDK in Windows, or I guess Linux (not yet tried), then the DTR line on most FTDI USB to serial units will be pulsed low then high. This is normally used to reset an Arduino which goes into sketch upload mode for a second or two after reset, although you may not be aware of this once you’ve uploaded a sketch.
In our case, the DTR line is used to reset the TINY85, not the ESP module. When the DTR line goes high again, the TINY85 starts up and monitors the serial line coming from the FTDI. If you have started the program upload process using the Espressif SDKs Upload Tool, then it will be attempting to negotiate a connection with the ESP8266 bootloader and so there will be serial data being sent out.
As soon as the TINY85 sees this attempt to negotiate, it drops the GPIO0 line to zero and issues a reset to the ESP8266. Of course, as soon as the ESP8266 comes out of reset, it sees attempts by the ESPTOOL to communicate and the programming process starts.
The TINY85 continues to monitor the serial data stream and holds the rest line high and the GPIO0 line low until the data stream stops. At this point, GPIO0 is brought high and another reset is issued to the ESP8266. This then triggers the normal start-up process in the ESP8266 and your newly uploaded program runs.
Just to keep things tidy, we then turn the reset control and GPIO0 lines into inputs so that you can do whatever you want with them until the next power up or reset events occur.
The code for the TINY85 is shown below and the diagram of the ESP-DEV3 board is here. So, you can see how the lines are connected across between the TINY85 and the ESP8266 if you want to breadboard this. However, the ESP-DEV3 board has all of this already in place, and we’ve put a few other useful items on as well:-
- Choice of a 10A relay or an optically isolated mains switch with switch terminals
- DS18B20 or DHT22 temperature/humidity detector
- LED Indicator
- Footprints for three of the most popular mains to 5V power supplies to drop into the PCB – we regulate to 3.3V after this as these supplies can be a little bit noisy electrically.
- A PCB button which we use for triggering a web setup process. If you hold the button down for about 10 seconds after power up, the ESP module generates a WiFi access point (AP) which you can then connect to using a phone, tablet or WIFI-enabled device capable of viewing a web page. Using this you can also toggle the relay on and off, read the temperature, set the LED etc. We’ll be developing this theme more in the coming months. A really neat addition is the ability to setup a serial, WS2812 LED output.
- All of the GPIO lines broken out to a 0.1” connector.
MUCH more on that in the NEXT blog entry. We have been mounting the ESP-12 module onto pins and putting the matching 2mm header sockets onto the ESP-DEV3 PCB so that you can just plug a module in and program it. Then it can be removed and added to another project.
If you want to program the TINY85 chip and you don’t have a programmer, then there are plenty of instructions out there on how to do it using an Arduino board.
So, no excuses then…
#define F_CPU 1000000
#define RST_ESP 3
#define ESP_PROG 1
#define SERIAL_MONITOR 4
digitalWrite(RST_ESP, 1); // Clear reset }
unsigned long st = millis();
digitalWrite(ESP_PROG, 0); // program mode digitalWrite(RST_ESP, 0); // Reset ESP module delay(200); digitalWrite(RST_ESP, 1); // Clear reset
//digitalWrite(ESP_PROG, 1); // program mode
st = millis();
if (!digitalRead(SERIAL_MONITOR)) // Got any serial comms?
st = millis();
if (millis() > (st + 3000L))
break; // Comms finished so exit
digitalWrite(ESP_PROG, 1); // Set ESP program pin high – not program mode digitalWrite(RST_ESP, 0); // Reset ESP module delay(200); digitalWrite(RST_ESP, 1); // Clear reset delay(200); // Slight delay to allow ESP module to see GPIO high at power up
pinMode(ESP_PROG, INPUT); // Allow the ESP pin to be used for other things by making the TINY85 pin an input pinMode(RST_ESP, INPUT); // Allow the ESP pin to be used for other things by making the TINY85 pin an input for (;;); // loop forever
For updates don’t forget if you’re a Facebook fan to follow updates on Facebook https://www.facebook.com/esp8266wifi