element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Achievement Levels
    • Benefits of Membership
    • Feedback and Support
    • Members Area
    • Personal Blogs
    • What's New on element14
  • Learn
    Learn
    • eBooks
    • Learning Center
    • Learning Groups
    • STEM Academy
    • Webinars, Training and Events
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Arduino Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Project Groups
    • Raspberry Pi Projects
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Or choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Arduino
  • Products
  • More
Arduino
Blog Part 3.2: Reading a MQTT topic with HttpClient On Arduino Yun
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: pmohan
  • Date Created: 27 Dec 2014 4:51 AM Date Created
  • Views 601 views
  • Likes 3 likes
  • Comments 0 comments
  • RoadTest
  • eclipse_iot
  • mqtt
  • yun
  • wifi_christmas_tree
  • ponte
  • iot_holidaylights
  • httpclient
  • eclipse_paho
  • iot
  • arduino
Related
Recommended

Part 3.2: Reading a MQTT topic with HttpClient On Arduino Yun

pmohan
pmohan
27 Dec 2014

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"

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

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

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube