By now, 2016, there are over 10 different version of the ESP8266 available. There's plenty of information on the web, a lively community forum and even multiple software development kids SDK's.
Recently Emanuele P, a DIoTY.co user, contacted me with some questions on connecting the ESP8266 to the DIoTY cloud. Later, when he got everything working fine, he shared his code... a .ino file! As I hadn't tried out ESP8266 Arduino before, I thought I'd give it a go and found out it was by far the easiest and quickest way to get started with ESP8266, at least for those people who have some Arduino experience.
So, here's how to get started.
- First download and install the latest stable version of the Arduino software: https://www.arduino.cc/en/main/software. If you already have the Arduino software, make sure you have a least version 1.6.5.
- Start Arduino, open Preferences window and enter
http://arduino.esp8266.com/stable/package_esp8266com_index.jsoninto the " field
- Next go to the Tools > Board > Board Manager and install ESP8266
- Next go to the Tools > Board and select ESP8266
In order to be able to connect to DIoTY's cloud you'll need some MQTT libraries. You can find many different ones on the net, but I went for the latest version of Nick O'Leary (@knolleary) PubSubClient. This is an updated version of the one I used in my earlier blog post which now includes support for ESP8266. You can still download it from GitHub and install it as explained in Arduino's guide.
The good thing about Nick's PubSubClient is that it comes with some sample code. This is a great starting point, so I've stripped it further down and configured it to connect to DIoTY. The result can be found here on GitHub. Download it and import it into your Arduino workspace. Next change the userId's and passwords for your WiFi network and DIoTY account and check it compiles successfully.
In this simple example, we will connect to the DIoTY Cloud MQTT service. When we are successfull, we will publish to say after how many attempts we were successful.
Next we will subscribe to a topic "/<yourDIoTYuserId>/ESP8266/inTopic". This is the same topic we will configure in the mobile app as a switch. Toggling the switch on the mobile app will cause the app to publish 'true' and 'false' to the topic. This will be picked up by the ESP8266 who will toggle a led on and off.
To be able to transfer the program onto the ESP8266 you'll need some kind of TTL serial converter. I bought the CH340G, but you'll find other similar products when you search around a bit. You'll also need some 3.3V power source able to deliver 1A or more.
Here's the pin layout of the ESP-01 and how you should connect it to upload your software:
- GND: ground of your 3.3V power supply and GND of your TTL serial converter.
- UTXD: TTL serial converter RX
- GPIO02: do not connect
- CH_PD: plus side of your 3.3V power supply
- GPIO0: ground of your 3.3V power supply
- RST: do not connect
- URXD: TTL serial converter TX
- VCC: plus side of your 3.3V power supply
Once you got the program loaded, have a look at the serial monitor output and you should see your ESP connecting to your WiFi and after that also to DIoTY. Once connected successfully to DIoTY the program will post it was successful to the topic /<yourDIoTYid>/ESP8266/connected. You can confirm this by login in onto DIoTY's website and navigating to "My DIoTY".
Now that we got this far, it's time to power off the ESP8266 and rewire it for business. Unplug it from the TTL serial converter as we don't need this one anymore. Wire GPIO0 to the minus side of a LED (short leg) and wire the plus side of the LED (long leg) to the plus side of your 3.3V power supply. Most LED's can handle 3.3V, but if yours can't don't forget to put a resister in between.
Finally we need to configure the DIoTY mobile app with a switch to control the LED. If you haven't already installed the mobile app have a look at my previous post: it shows you where to get it from, how to log in and add new things. So, now that we know how that works, add a new thing of type "switch". Give it a name of your liking and configure it with topic /<yourDIoTYid>/ESP8266/inTopic.
And that's it... Switch on your ESP8266, wait for it to connect to your WiFi and the DIoTY cloud and switch the led on and off using your mobile... from anywhere in the world... or for right next to it like in this short video below ;-)
By connecting GPIO0 to the signal input of a relay, you can now switch on and off just about anything rather than just a little LED.