MQTT ESP8266 pubsubclient example data source

I am running the code example in knolleary/pubsubclient/examples/mqtt_esp8266. The code is running great but I have a question: Where is the data for controlling the LED coming from??? Approximately every second, the LED is changing state, but nowhere in the code can I find what is sending this data. Thank you for any help.


// https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_esp8266/mqtt_esp8266.ino

/*
  Basic ESP8266 MQTT example
  This sketch demonstrates the capabilities of the pubsub library in combination
  with the ESP8266 board/library.
  It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every xx seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off
  It will reconnect to the server if the connection is lost using a blocking
  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  achieve the same result without blocking the main loop.
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

WiFiClient mqttClient;
PubSubClient client(mqttClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int msgNumber = 0;
const char* ssid = "xxx";
const char* password = "xxx";
//const char* mqtt_server = "broker.hivemq.com";
const char* mqtt_server = "broker.mqtt-dashboard.com";

void setup_wifi() {
  int i = 0;
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");// We start by connecting to a WiFi network
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    i++;
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("Time=");
  Serial.println(i / 2);
  Serial.println("IP address: "); // 192.168.1.8
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) { // length = 1 always?
    Serial.print((char)payload[i]);
  }
  Serial.println();
  if ((char)payload[0] == '1') {    // on LED if 1 first character
    digitalWrite(BUILTIN_LED, LOW); // LED on LOW is the voltage level
  }
  else digitalWrite(BUILTIN_LED, HIGH);// Turn the LED off
}

void reconnect() {
  while (!client.connected()) {  // Loop until we're reconnected
    Serial.print("MQTT connection...");
    String clientId = "stevensarns";
    //    clientId += String(random(0xffff), HEX);   // Create a random client ID

    if (client.connect(clientId.c_str())) {    // Attempt to connect
      Serial.println("connected");
      client.publish("outTopic", "xxx"); // Once connected, publish
      client.subscribe("inTopic");      // ... and resubscribe
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);      // Wait 5 seconds before retrying
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT); // led output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) reconnect(); // always executes on first loop
  client.loop();                // returns false of discon, true of connect
  unsigned long now = millis();
  if (now - lastMsg > 10000) {
    lastMsg = now;
    ++msgNumber;
    snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", msgNumber);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
}

You are not the only person using the topics on the public broker

Make the topic names unique to you so that nobody else will use the same names

Thank you - looking into it...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.