In my last two posts i covered how i setup the Eclipse Ponte Bridge locally and how my Arduino Yun with an Infineon Shield was reading from a MQTT topic from it using HTTPClient
In this post i will cover how i used Arduino Uno with an ESP8266 to publish a MQTT message with RGB values.
Arduino Uno + ESP8266
I ordered the ESP8266 during this road test because my initial plans of using an Arduino Uno with a CC3200 Simple Link Launchpad didn't work out very well. For $7 it provides bang for the buck as long as you tolerate the initial getting to know you phase. Most of the frustration is because of the different versions and the lack of official documentation. That though seem to be improving of late as the firmware seem to have stabilized somewhat and an updated SDKs seem to have been released this month.
I was lucky in that the ESP8266 i got already had the latest firmware and i had the version v0.91 board. I didn't have to update the firmware. The following is the pin definition of my ESP8266:
I tried initially to try to use the UNO as a FTDI driver by connecting the ESP8266 to Uno with level shifters. But that didn't work. No response at all from the ESP8266. Then i bought a FTDI breakout board with a 3.3v output and i was able to talk to ESP8266 using CoolTerm (Except for the Arduino, i am using all these different tools for the first time. That added to frustration levels sometimes. But it was well worth it as it helped me learn so many new things). Once i become comfortable with the AT commands, i hooked the ESP8266 back to Arduino Uno using level shifters.
Again nothing from Uno.
One of the major misconceptions out there seem to be around using level shifters. Every one (including much respected AdaFruit) keeps saying you need to use level shifter to connect the RX pin of the ESP8266 to Uno's TX pin since uno operates at 5V. But the data sheet i found here says this in page 15.
All digital IO pins are protected from over-voltage with a snap-back circuit connected between the pad and ground. The snap back voltage is typically about 6V, and the holding voltage is 5.8V. This provides protection from over-voltages and ESD. The output devices are also protected from reversed voltages with diodes.
I am not an electrical or electronics engineer. But that statement seemed to me that it might be OK to connect the ESP8266 directly to my Uno. So i just did that and it worked. (It would be interesting to hear from the engineers in this Element 14 community about that particular statement above)
Following is the wiring i did:
1. Arduino 3.3v power to bread board red power rail
2. Arduino Ground to bread board ground (black rail)
3. ESP8266 Ground to bread board ground (black rail)
4. ESP8266 CH_PD to bread board red power rail ( This is the reset pin. I was not brave enough to connect it to Uno's reset pin. So every time i loaded my sketch, i had to pull this out and push it back in. A veritable stress test i would say.)
5. ESP8266 TX to Arduino Uno Digital Pin# 2 ( i will be using software serial library to designate this pin as RX for arduino)
6. ESP8266 RX to Arduino Uno Digital Pin#3 ( designated as TX pin using software serial library)
7. ESP8266 Vcc to bread board power rail
That's all. I have been using this setup for a few days now. It has gone through couple of power surges (too much bad weather with lightning here) and still working.
The Software (ESP8266RESTHelper Library)
As i mentioned in the beginning i have already set up Ponte MQTT/HTTP/Coap Bridge locally. ESP8266 has a TCP stack that can be accessed via AT commands. So all i had to do is prepare the proper AT commands for the ESP8266 and make it send PUT requests to the Ponte HTTP bridge for publishing MQTT topics. Other clients whether they use MQTT libraries or HTTP GET (see here) were able to receive the messages.
Since i started working on the AT commands i ended up creating a helper library to do http GET and POST as well. The ESP8266RESTHelper library is available here at Github. If you are interested take a look and use it as you see fit. Please note apart from the sendMQTTMessage other functionalities are not fully tested. But it might give you a starting point.
Here is the sketch that's running on my Arduino Uno. Visit the Github library to see the API and the AT command sequence.
#include <SoftwareSerial.h> #include "ESP8266RESTHelper.h" ESP8266RESTHelper esp8266=ESP8266RESTHelper(); void setup() { esp8266.begin(); } void loop() { //Send Red -- topic name starts with "/resources/" because the Ponte http bridge expects PUTS to be sent that way. The actual topic for clients is just "FromArduinoUnoESP/RGB" esp8266.sendMQTTMessage("192.168.1.217",6000,"/resources/FromArduinoUnoESP/RGB","255,0,0"); delay(2000); ] //send Green esp8266.sendMQTTMessage("192.168.1.217",6000,"/resources/FromArduinoUnoESP/RGB","0,255,0"); delay(2000); //Send blue esp8266.sendMQTTMessage("192.168.1.217",6000,"/resources/FromArduinoUnoESP/RGB","0,0,255"); delay(2000); }
Here is a video of the Arduino Uno with ESP8266 sending MQTT messages and the Arduino Yun receiving the mqtt messages and changing the led strip using httpclient(explained in this blog entry) (note that blue led on the ESP8266 go on and off when TCP send happens)
Updates:
1. Software Serial Library: (Thanks to a question from Wes in the comments)
I am using Arduino Ide version 1.5.8 beta..This includes a SoftwareSerial Library as part of the core ide libraries..
If you need the Github location for the core libraries, its here:
https://github.com/arduino/Arduino/tree/ide-1.5.x/hardware/arduino/avr/libraries
2. My ESPRESTHelper library is using String object.. i have come to realize String is not the ideal thing to use. I am running into weird memory issues once the code becomes bigger and the dynamic memory becomes full. It is ok for simple proof of concepts. I will update the library soon to remove Strings from it.
Top Comments