Saturday 20 February 2016

Using the ESP8266 with DIoTY.co

The ESP8266 has come a long way since it first became available in 2014.  In the beginning, little was know about the $5 microcontroller that includes WiFi capability.  And, the little information that could be found, was all in Chinese.

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.json into the "Additional Board Manager URLs" 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
Now you're ready to upload your program much like you would upload a program to your Arduino.  If you get an error message, switch of the power for a few seconds before retrying.

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.

3 comments:

  1. Thanks for the overview Ward! Having come into this world specifically looking for the libraries/easy solutions for the esp8266, I'd already stumbled through that particular toolset, but having all the info in one place would have been much nicer. :-)

    Couple of suggestions ...
    As far as hardware goes, you can get buy with the $5 modules on ebay/amazon (and I have, like, 4 of them) BUT the extra $5 for the Adafruit HUZZAH breakout board is TOTALLY worth it. https://www.adafruit.com/products/2471

    I found them after ordering 4 of the others, and it makes it so much easier ... breadboard friendly, onboard voltage regulator to supply 3.3v from up to 12, appropriate caps and resistors, an ftdi header already wired in, AND the appropriate reset/bootloader buttons that make uploading from the arduino environment much nicer. If I'd already built a breakout for the other boards, it'd been more of a "meh", but they make that "getting started" hardware challenge much easier.

    The example works well. However (and this isn't unique to yours) I'd really like to see an example with the appropriate arduino "C-ish" syntax around building statement to sort incoming messages. (I had some trouble translating byte* to a workable value)

    Something like :

    if (topic == "led1") {
    if (payloadValue == "true") {
    //switch led1 on
    } else {
    //switch led1 off
    }
    } else if (topic == "led2") {
    if (payloadValue == "true") {
    //switch led2 on
    } else {
    //switch led2 off
    }
    }

    Only somewhat related ... I know you published an updated iOS app...and you've probably been deluged with "it'd be nice if..." requests, but ... it'd be *really* useful if the app could somehow throw an OS-level notification.

    For example, the ios device in the basement detects water.. you don't want to have to check the app or your email, you want a notification to pop up. Or the cat's water dish is empty ... or someone opened the garage door ... or your super nifty custom home alarm system just went of ... You get the idea. Perhaps something to consider.

    (oh, and just to note, your font changes size in the middle of the post in case you didn't know :-) )

    Take those suggestions with a grain of salt ... and thanks again for your work!
    -aaron

    ReplyDelete
    Replies
    1. Hi Aaron,

      thanks for your comment and useful suggestions. Got me an Adafruit version ordered to have a closer look...

      Will try to produce a more C-like version in a follow up blog on the ESP8266.

      As for your suggestion to throw an OS-level notification I have to admit that although I'd love to have the time to implement this, it's currently not on the top of my priority list. I'm planning to rewrite the app in version 2 of Ionic once it comes out of beta before doing any big changes like this...

      Also, my current priority is making things easier for the end user to build connected things. I see still too many users trying out DioTY but giving up before getting it working properly... (hence also this blog post)
      If you have idea's on how I can make things easier, feel free to share them (either here or in an email to admin at dioty.co)

      Delete
  2. Hats off to your presence of mind..I really enjoyed reading your blog. I really appreciate your information which you shared with us.
    ionic training in chennai

    ReplyDelete