Jump to content

MQTT with Node-Red subscribe problem

Recommended Posts

Hi there,


I am using a CC3200 launchpad, together with Energia 13. 


I am trying to connect the launchpad through MQTT to publish and receive messages (following Adrian Fernandez youtube on 

and blog on http://energia.nu/creating-an-iot-connected-sensor-with-energia-mqtt/).


I succeeded on publishing information through MQTT to Node-Red, twitter etc., however I did not have any luck in subscribing. Since the blog (nor the video) does not state explicitly how to subscribe, my guess is that I have something wrong in my definitions.


In the cloud side I am using Bluemix cloud, with Node-Red service. I simply define an input and output MQTT, and a debug node. The input works well, however the output (from the cloud to the launcpad) does not work. The output MQTT is defined with the same broker as the input MQTT, however with a different topic (matching the definition in the launchpad side).


On the launcpad side, I am using the PubSubClient library, with the following commands to subscribe:


 if (client.connect("LaunchPadClient")) {

    client.publish("outTopic", char_array);
    Serial.println("Connection successful!");
The callback function is never activated (it simply contains a serial message declaring input, however it never appears).
*code attached. 
Thanks a lot,
Or Peleg


Link to post
Share on other sites

What a coincidence (or isn't it?), I was just finishing up and testing the improved MQTT examples. Quick look at your Sketch shows that you are missing the client.loop() in the loop function. This call will service any outstanding messages that came in and call your callback. Thank you for providing detailed information. This helps a lot with not having to guess or ask about details!


Below is a example Sketch that I just finished up:

 Basic MQTT example 
  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"

#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

// your network name also called SSID
char ssid[] = "energia1";
// your network password
char password[] = "launch  pad";
// MQTTServer to use
char server[] = "iot.eclipse.org";

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received message for topic ");
  Serial.print("with length ");
  Serial.write(payload, length);

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup()
  // Start Ethernet with the build in MAC Address
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.

void loop()
  // Reconnect if the connection was lost
  if (!client.connected()) {
    Serial.println("Disconnected. Reconnecting....");

    if(!client.connect("energiaClient")) {
      Serial.println("Connection failed");
    } else {
      Serial.println("Connection success");
      if(client.subscribe("inTopic")) {
        Serial.println("Subscription successfull");
  if(client.publish("outTopic","hello world")) {
    Serial.println("Publish success");
  } else {
    Serial.println("Publish failed");
  // Check if any message were received
  // on the topic we subsrcived to

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(" dBm");
Link to post
Share on other sites
  • 6 months later...

Dear Fellows, 


So I got a new CC3100 Booster Pack, and out of box, decided to try the IOT-MQTT example with energia. While energia did run the basic AnalogSerialRead example perfectly with my potentiometer, I wasnt able to move past that. 


I got the MQTT code as in the example from the PubSubClient - potentiometer example, and made necessary changes like updating my ssid and pwd, also changed the corresponding pin etc. 


I compiled and uploaded the code. Upon resetting my TIVA-C TM4C123GXL launchpad, the Serial monitor printed the first message "Start Wifi", and then hung indefintely, no response after that. 


I even tried with the code shown above. Same behavior, the serial monitor prints "Attempting to connect to network named: mySSID " and then hangs. 


To debug, I put a print statement just before the while loop 

while ( WiFi.status() != WL_CONNECTED) {

like so : Serial.println(WiFi.status()), to see if the status was incorrect. But this didnt get printed. This means that the code hangs on the prev line,


WiFi.begin(ssid, password);

How can I debug this further/resolve this ? 


I am using the default firmware on my CC3100 boosterpack. I dont have the CC3100EMU, would I need to make any updates, or import any library in energia?

Link to post
Share on other sites
  • 2 years later...

client.loop() in the loop function. This call will service any outstanding messages that came in and call your callback


This polling kind of solution is somehow ruin the whole beauty of using the event handler as it slows down the response to received topics and turns it to be non deterministic as it is dependent on the program flow,

Is there other way of doing it that would be functioning like a real event handler or interrupt?

Also, @orpeleg7 nice catch make me wonder if anyone at TI is running the example programs at all, before they are being added to the released libraries?


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...