This post is part of my entry to the Internet of Holiday Lights road test challenge. A full list of all the post is available at this link.
In the earlier blog post, i introduced Ponte and set it up locally on my windows PC.
In this post i will demonstrate how i used the HTTP Client on the Arduino Yun to read the MQTT messages and light up the LED strip using the Infineon Shield. This code does not use any of the MQTT Client libraries to subscribe to a topic. It just uses the HttpClient available on Arduino Yun's bridge library and keeps polling the HTTP bridge on the Ponte server for message availability.
The HTTP Client in Arduino Yun works by creating a process on the linux side to run "curl" program. Once the linux side receives a response, its sent over the hardware serial port to the Arduino processor. In the code below, i am setting up the base Url for the Ponte server with the MQTT topic and accessing it the loop using the HttpClient. The get method on the HTTPClient is a blocking call. You can use the getAsynchronously() method if you need a non-blocking call.
Once a response is received, I am parsing it to split the RGB values, map it to a maximum of 4095 which is the maximum value for the intensity parameter on the Infineon RGB Shield.
//Description: Arduino Yun sketch to read an MQTT Topic on Ponte bridge using HTTP Client //Code uses a custom version of Infineon RGB shield library created with samples from Infineon //Written for the Internet of Holiday Lights challange at element14.com //http://www.element14.com/community/groups/arduino/blog/2014/12/11/intro-interactive-wifi-christmas-tree //Date: December 26, 2014 //Author: Mohan Palanisamy //Please note that the code doesn't include much of error handling as its a Proof of Concept code. #include <Bridge.h> #include <HttpClient.h> #include <Wire.h> #include "InfineonRGB.h" //Using two strings to easily differentiate base url and the topic.. //Base url points to my local installation of Ponte server. /resources is where ponte adds topics. String mqttPonteBaseUrl="http://192.168.1.217:6000/resources/"; String mqttTopic="FromPonte/RGB"; String currentColor=""; HttpClient httpClient; InfineonRGB rgbShield= InfineonRGB(); void setup() { Bridge.begin(); Wire.begin(); rgbShield.begin(); delay(1000); //small delay to let bridge initialize } void loop() { httpClient.get(mqttPonteBaseUrl+mqttTopic); while (httpClient.available()) { String response=httpClient.readString(); //Ponte bridge responds with a Not Found if there is no message for the topic. if(!response.equals("Not found")) { changeColor(response); } } delay(200); //change this delay of you don't want the http get to be called so often } void changeColor(String rgbString) { //Parse RGB; int rIndex=rgbString.indexOf(','); int gIndex=rgbString.indexOf(',', rIndex+1); int r= rgbString.substring(0,rIndex).toInt(); int g= rgbString.substring(rIndex+1,gIndex).toInt(); int b= rgbString.substring(gIndex+1).toInt(); //map to the maximum intensity available on the infineon shield r=map(r,0,255,0,4095); g=map(g,0,255,0,4095); b=map(b,0,255,0,4095); rgbShield.setRGB(r,g,b); //light up }
Here is the code in action. I use the mosquitto_pub application to send mqtt messages. Please note that the messages are sent with retain flag (-r) to make sure its accessible from the http bridge whenever a client connects and asks for it.
$ mosquitto_pub -r -h 192.168.1.217 -t FromPonte/RGB -m "255,0,0"
The best way to listen for MQTT messages is by using the MQTT libraries. They provide the fastest response because the TCP connections are kept alive until they are explicitly closed. This demonstration of HTTP Client is another way of consuming MQTT messages without dependence on additional libraries when your application doesn't need the entire functionality offered by the MQTT stack and a HTTP bridge like Ponte is available.
All code is available at this github location.
Stay tuned for further use cases for the Ponte Bridge as i continue to build additional modules for my Interactive WiFi christmas tree.
Update: See here for the MQTT publisher..
Part 3.3: Arduino Uno + ESP8266 + Eclipse Ponte HTTP Bridge = MQTT Magic