-->
Page 1 of 5

Mqtt subscribe example that i cobbled together. Flashing led

PostPosted: Sun Apr 26, 2015 10:12 am
by gambituk
Hi, so i was looking for an example on another thread, i couldn't find exactly what i was looking for so i pasted something together and thought i would contribute what i managed to get working.

Please feel free to comment or correct as needed.

This example actuates a LED / Relay or whatever when the correct string is received.

Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>

//////////////////////////Added toggling led with any topic publish "1"  /////
 
const char* ssid = "............";
const char* password = "............";
 
char* topic = "#";     //  using wildcard to monitor all traffic from mqtt server
char* server = "192.168.0.7";  // Address of my server on my network, substitute yours!
 
char message_buff[100];   // initialise storage buffer (i haven't tested to this capacity.)
 
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
 
 
String macToStr(const uint8_t* mac)
{
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5)
      result += ':';
  }
  return result;
}
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
    // prepare GPIO2 *********************************************************************
  pinMode(2, OUTPUT);   // i am using gpio2 as output to toggle an LED
  digitalWrite(2, 0);  //*****************************************************************
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  //  connection to broker script.
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("#");   // i should probably just point this to the varialbe above.
  }
}
 
void loop() {

  client.loop();
}


void callback(char* topic, byte* payload, unsigned int length) {
  // Here is what i have been using to handle subscriptions. I took it as a snippet from elsewhere but i cannot credit author as i dont have reference!
    int i = 0;

  Serial.println("Message arrived:  topic: " + String(topic));
  Serial.println("Length: " + String(length,DEC));
 
  // create character buffer with ending null terminator (string)
  for(i=0; i<length; i++) {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';
 
  String msgString = String(message_buff);
 
  Serial.println("Payload: " + msgString);
  int state = digitalRead(2);  // get the current state of GPIO1 pin
  if (msgString == "1"){    // if there is a "1" published to any topic (#) on the broker then:
    digitalWrite(2, !state);     // set pin to the opposite state
    Serial.println("Switching LED");
  }
}




Have fun, let me know if you make something from it! i totally cannot take credit but i would still like to know what can be done.!

Gambit

Re: Mqtt subscribe example that i cobbled together. Flashing

PostPosted: Sun Apr 26, 2015 1:33 pm
by danbicks
Brilliant,
Been looking for a subscribe function.

A plus

Dans

Re: Mqtt subscribe example that i cobbled together. Flashing

PostPosted: Sun Apr 26, 2015 2:23 pm
by danbicks
Works a treat :)

Not sure if anyone knows how to add a callback if MQTT connection drops, also how do we make sure that a message published was actually received?

Well done again, brilliant.

Dans

Re: Mqtt subscribe example that i cobbled together. Flashing

PostPosted: Sun Apr 26, 2015 7:38 pm
by MeNoGeek
Thanks, Gambit! That example's pretty useful. (I guess the publish part was written by the library's author, Nick O’Leary: http://knolleary.net/arduino-client-for-mqtt/.)
danbicks wrote:how do we make sure that a message published was actually received?
That would require a QoS of 1 or 2, but only 0 is supported by the PubSubClient library, AFAIK :( So I guess some kind of automatic publishing would be needed from the broker or another client.
Tuan's MQTT library does support all three levels of QoS, as well as other features that PubSubClient lacks.
https://github.com/tuanpmt/esp_mqtt
I wish someone could make it available in the Arduino environment.