Updated 09 Apr 2015 – code fix: A little something here from my guest writer and friend Aidan who’s been working with me on the latest home control successes to follow after this blog!
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
void setup()
{
pinMode(RST_ESP, OUTPUT);
pinMode(SERIAL_MONITOR, INPUT);
pinMode(ESP_PROG, OUTPUT);
digitalWrite(RST_ESP, 1); // Clear reset }
void loop()
{
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
delay(200);
//digitalWrite(ESP_PROG, 1); // program mode
st = millis();
for (;;)
{
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
// TEST
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
Even though this is all over the web (including this site), I’ll reiterate here…
Test any serial/USB converter you choose to make sure it’s only putting out 3.3V (not 5) on the RX and TX pins or you might nuke your ESP8266. If that happens, you’ll probably spend a day trying to debug that.
To do this, take your converter and plug in a live USB cable then measure the voltage on the TX and RX pins and they should both be around 3.3V.
I have the ESP-201, Breadboard 3.3 Voltage Regulator and the Arduino Uno.
I’m trying to solve the following errors when loading a sketch:
espcomm_sync failed
espcomm_open failed
Applying power to the ESP-201 it is connected as follows:
GIO0 >Grnd Arduino/ Grnd Voltage Regulator
GIO15 > Grnd Arduino/ Grnd Voltage Regulator
CHIP_EN > 3.3V Voltage Regulator
Grnd > Grnd Arduino/ Grnd Voltage Regulator
TX > RX Arduino
RX > TX Arduino
3.3V > 3.3V Voltage Regulator
With this setup, you’re using the Arduino as and RS232-USB converter. Nowadays, those converters cost less than a dollar (try to find a real CP2102 based converter which might cost 1.50 USD). The FTDI based converters should also work but I’ve had one fail. Whenever I shop, I buy two of everything since they’re so cheap and will make debugging a hardware failure easier. With the dedicated USB/Serial converter you get to take the Arduino out of the picture and simplify your setup.
You should be able to bring up the ESP board out of the box and actuate w/ AT commands on serial port. Note that most of them ship with an initial baud rate setting of 77,400 which is weird. A free serial terminal program called CoolTerm allows you to set any baud rate. There may be others but I know that one works.
Now, if you want to code for it, I would start simple with the latest Arduino IDE which now allows you to select the ESP8266 directly from the droplist of platform choices and set the baud rate in code. This uses the AiThinker kernel. Once you’re comfortable with that, you’d want to start reading Mr. Scargill’s other posts which explain how to use the kernels from AiThinker and Espressif (the designers of the chip). These are steeper learning curves, but allow lower level access to interrupts and other features. But, if all you want to do is read and write I/O pins and send wifi packets, the Arduino IDE is more than sufficient.
If any of the information above is not familiar to you, I urge you to do some research online. Everything’s out there. You just might be kissing some frogs. I happen to think this blog has the most accurate information outside of vendor websites.
Hope this helps.
Thanks Pete,
I think so too and that’s what worries me. I have to dig deeper. I’m interested in the mesh kernel and the ability to run simple, user space programs to respond to sensors as needed. Maybe eventually GPS self location.
If I use the ESP8266 plug in for the Arduino IDE, will it upload a completely new f/w image or is replace the user space code w/ my sketch?
I think it replaces the lot.
My auto-reset patch for esptool-ck lets you reset the esp8266 with just a resistor and capacitor, and control gpio0 with DTR.
http://nerdralph.blogspot.ca/2015/04/zero-wire-auto-reset-for-esp8266arduino.html
Ok, so I’m not following this (I understand the reset process)… DTR line connects to GPIO-0 – yup, got that, though you still surely have to disconnect that when you are done if GPIO0 is used as an output? And how does the reset fire – you don’t mention any connection between DTR and the reset? Sorry, not following?
If you need to use gpio0, and disconnecting it after uploading is a pain, then connect DTR to the base of a PNP connected between GPIO0 and GND.
The reset is triggered when esptool transmits a break. It discharges the RC circuit on the reset pin.
Good idea Ralph. I’m trying to come up with a solution so that a 5v relay connected to GPIO0 doesn’t bounce up and down during all of this… Got some ideas – see latest blog entry…. always happy to hear other ideas (bearing in mind that most handy relays are 5v not 3v3 hence NPN transistor driven, Due to difference between 3v3 ESP and 5v relay, PNP not a good idea for THAT – I see what you are after in the case of the reset. With the terminal program active, GPIO0 would be held high permanently however.
What I’d like to do is write a stage 2 bootloader that waits for flash upload, and if it times out, run the application code.
That way gpio0&2 can stay hi at boot, and be brought low by the application to do things like control an attached relay.
I still have to learn some more about the esp8266 before I get there.
Great, I am looking for PI and esp8266
Hi Peter,
I found a way to flash the esp, using the raspberry pi (no need for other devices)
By installing wiring pi http://wiringpi.com/
connect GPIO (0 and 2 and reset) esp8266 to GPIO raspberry pi (wPi 0 – 2 – 3)
You can toggle the ports by executing e.g (gpio write 3 0 and gpio write 3 1, which resets the esp)
gpio write 0 0 sets the esp in flashmode
by using /usr/src/esptool.py –port /dev/ttyAMA0 write_flash 0×00000 firmware/0×00000.bin you can upload the binary
I wrote a script to automate the process : http://wordpress.rmatic.info/wp/?p=2475
Kind Regards,
Jan
BTW there’s is a ESP-SDK for RPI
Ok… I’m sure there’s a use for that.. over to others? Oh I guess you could do your dev on a Pi – hah !! That’s interesting..
Peter, I do not have an ATTiny to hand, is it possible to use an Arduino as the programming interface rather than the ATTiny?
Thanks
Bob
Well, clearly a 328 (which is all an Arduino is really) would work – wrong size for our board but of course – the principle is very simple as the code demonstrates.
I’m using cp2102 USB to TTL with Windows 8.1, connecting DTR to GPIO0 and RTS (not RST!) to REST in my ESP-12 and I’m flashing with the click of a button… no extra hardware, buttons, capacitors or whatever (sometimes I have to click a second time, but that’s probably just a missing pullup or capacitor somewhere).
in “C:\Espressif\utils\esptool.py” :
def connect(self):
# RTS = EXT_RSTB
# DTR = GPIO0
print ‘Entering bootloader…’
self._port.setRTS(True)
self._port.setDTR(True)
time.sleep(0.25)
self._port.setRTS(False)
self._port.setDTR(True)
time.sleep(0.25)
self._port.setRTS(False)
self._port.setDTR(False)
@sfam, Arer you also doing TTL voltage level translation esp for TTL (Tx from CP2102 to Rx of ESP8266) ? I intend to use similar strategy with CH340G
TTL voltage of CP2102 is already 3.3V, so there is no need for a level converter (not sure about CH340G).
I’m also using it to power esp8266… you can’t draw enough power from the 3.3V line, but connecting a 3.3v voltage regulator to the 5V works like a charm! something like:
http://www.instructables.com/id/Mod-a-USB-to-TTL-Serial-Adapter-CP2102-to-program–1
That’s really useful. So we have full control of both of those lines when flashing.. now, if only I know how to get that control when enabling the terminal in Eclipse because for reasons beyond me it is setting DTR low and I can’t seem to find any setup to turn that action off.
I’m not really following that bit about RTS…. so I can see that it would be great to control DTR and hence GPIO0 – but do I take it you are using RTS to turn the ESP on and off (and hence reset) ???
Yes, with both RTS and DTR lines connected to esp8266 (as in http://www.esp8266.com/viewtopic.php?f=9&t=820) , and using the Eclipse devkit for windows, when I hit the FLASH target, the makefile calls esptool which takes care of everything:
– RTS resets esp8266
– DTR makes gpio0 LOW when booting
– esp8266 enters flash mode and new firmware is uploaded
– esp8266 reboots again in normal mode
This is as easy as flashing an arduino… and combining this with Sming framework (which already has MQTT !!)… its really fast to develop and debug!
Thats’ great. If you look I’ve just put a new post up on the subject. I have a problem as the Terminal add on for Eclipse seems to want to take the DTR line LOW – it flicks up and down then stays DOWN. We are using a little SMT ATTiny to handle this – and also stop any relay flicking if you happen to be using GPIO0 as a relay output. See latest blog item… I can’t seem to find any way to control the DTR in the terminal program.
I’m impressed. Eagle has a “Mains_PSU_Cheap_Chinese” footprint nowadays. 🙂 🙂
Erm, I don’t think it has – I think we probably added “Mains_PSU_Cheap_Chinese” – referring of course to the very cheap Chinese power supplies – and no they’re not very good but we used the 5v versions and then stuck a 3v3 linear regulator on the output – works a treat.
Hi Pete, great job! Could you, please, share the eagle files of this project?
Just spoke to Aidan – he wants to hang fire until we get the new boards back and tested. Don’t really want to be responsible for others having hassles. Keep looking in.