But then I discovered two things:
- You can use the TX and RX pins as GPIO pins! This means you can youse 4 GPIO pins on the cheapest ESP8266-1 module, not 2. That makes it even more useful (you can control up to 4 things over the internet now, not just 2, using a single ESP board).
- The ESP is incredibly picky. It doesn't like when stuff is connected to its GPIO pins when its booting. If you have 4 LEDs connected to its GPIO pins the chance is it won't boot. And, it depends on the type of the LEDs. Red ones tend to cause most trouble. I am not joking.
So, my cute little breakout is now a bit obsolete - it is still useful, but not ideal.
I was presented a new challenge - to find a convenient and reliable way to use all 4 pins on the smallest ESP.
- When booting, all 4 GPIO pins should be floating
- When in "programming mode" GPIO0 should be low, RX and TX should be connected to USB2TTL
- When in "running mode", all 4 pins should be connected to whatever I want to connect them to (LEDs, motors, relays, sensors, etc.)
- As soon as uploading a new program is finished, the ESP goes to "running mode" and starts executing that program. This is while its pins are still in "programming mode" configuration.
- I need a "development setup" setup where I can repeatedly toggle between boot/programming/running mode (so I can upload and test new code) and a "final setup" for the completed projects (it still has to boot with all pins floating and later toggle them to "running mode").
Long story short, my "development setup" looks like this:
After buying the proper switches (called "2P3T switches) it was fairly easy. Each switch controls 2 independent circuits and has 3 different positions. The only problem was that these switches were not breadboard/protoboard friendly, so I had to make little "breakouts" for them. Special thanks to a friend of mine who cut the PCBs for me, to make it possible.
Now I have the usual reset button, power on/off switch and two 3-state switches. These 2 toggle 2 pins each between programming/boot/running mode. The left one is for GPIO0 and GPIO2 and the right one is for TX/RX (a.k.a. GPIO1 and GPIO3). I used 2 separate switches so I can control TX/RX separately. So, I can still use them as TX/RX (e.g. for debug output to PC during development).
The problem that the program starts running as soon as the upload is completed turned out to be not a real problem. At first I was afraid outputting HIGH through RX/TX pins could cause some damage (to the USB2TTL board or to my laptop's USB). There was one way to find out - I put on protective glasses, helmet, had a fire extinguisher ready, crossed my fingers and just tried it. Nothing bad happened. You can even see the USB2TTL's lights blinking when my program sends high/low through TX/RX. I only had to use a pull down resistor between GPIO0 and ground instead of simple wire, because outputting high signal to GPIO0 (which is connected to ground in programming mode) resulted in a short circuit. Fortunately, it didn't blow the ESP, just made it reset.
So, now developing for the ESP is easy - i put the 2 switches to "neutral", turn the power on, then switch to "programming mode" (both switches to the left), upload a new program, flip one or both switches to the right when the program is running if needed. For corrections, I just flip the 2 switches back to the left, hit the reset button and upload the new program. It's not as easy as with the Arduino, but it's easy enough.
Now, the "final setup" is a bit more tricky. I could probably go with a few 2-state switches - an on/off switch and 1-2 switches to toggle the GPIO pins between "boot mode" and "running mode". But that would be kind of lame. Final products only need to have an on/off switch and that's it. And, when you turn it on, it would need to boot the ESP with all GPIO pins floating and then reorganize its pins to "running mode". After some fiddling and with the help of a 555 timer chip and 2x relay chips I came up with this prototype:
What happens now is when I turn the power on the ESP boots and after a second or so the timer kicks in, turns the relays on and they switch the 4 pins into "running mode". Smooooth :)
Btw, these 555 timer chips are awesome little toys, they can do so many things. And they cost like $0.75. For example, I plan to use one to make a horn for my electric scooter and another one to make blinkers for it. If you're into electronics and don't know what a 555 chip is (as I didn't until few days ago) - look it up, there is plenty of info and plenty of different usages.
Oh, and the current test makes the ESP create a WIFI hotspot, you connect to it (from a phone/laptop/tablet), and you can control the 4 LEDs from a web browser. I also tried connecting the ESP to my router instead of creating a hotspot - no problems.
So, now I can control 4 pins on a single ESP82266-1 from anywhere in the world :)
This means I could turn 4 lights/appliances on/off, drive an RC car (two motors in both directions) and whatnot ... over the internet. Using components that cost like $5 total. Nice, huh?
The next step is to turn the above prototype into a much more compact breakout, but I am not in the mood for tricky soldering today ... and I'd like to wait a little longer before I do that, just in case I get some other ideas in the meantime.