knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.84k stars 1.47k forks source link

More then one Topic #471

Open pietbierbuik opened 6 years ago

pietbierbuik commented 6 years ago

How do i subscribe to two or more topics ?

knolleary commented 6 years ago

By calling the subscribe function multiple times.

pietbierbuik commented 6 years ago

Yes, thats what i did. But the callback function dont care what the topic is. so i changed the callback like

if(strcmp(topic ,"inTopic") == 0){

    if ((char)payload[0] == '0') {
      digitalWrite(12, LOW);   
    }
    if ((char)payload[0] == '1') {
      digitalWrite(12, HIGH);   
    }  

  }

  if(strcmp(topic ,"inTopic2") == 0){

    if ((char)payload[0] == '0') {
      digitalWrite(13, LOW);   
    }
    if ((char)payload[0] == '1') {
      digitalWrite(13, HIGH);   
    }  

  }

}

I think it works now as i want, im new with the arduino so i am still learning. Thanks for your script !

pietbierbuik commented 6 years ago

Sorry i am a real noob. I want to use ON and OFF

if ((char)payload[0] == 'ON') {
      digitalWrite(12, LOW);   
    }

But this does not work

rezahili commented 6 years ago

use this

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  if(topic=="room/lamp"){
      Serial.print("Changing Room lamp to ");
      if(messageTemp == "on1"){
        digitalWrite(14, HIGH);
        Serial.print("On 1");
                char* lamp1 = "light 1 is on";

      client.publish("light/status", lamp1);

      }
      if(messageTemp == "on2"){
        digitalWrite(12, HIGH);
        Serial.print("On 2");
                char* lamp2 = "light 2 is on"; `

      `client.publish("light/status",` lamp2);

      }
      if(messageTemp == "on3"){
        digitalWrite(16, HIGH);
        Serial.print("On 3");
                char* lamp3 = "light 3 is on";

      client.publish("light/status", lamp3);

      }
      else if(messageTemp == "off1"){
        digitalWrite(14, LOW);
        Serial.print("Off 1");
        char* light1 = "light 1 is off";
        client.publish("light/status", light1);

      }
      else if(messageTemp == "off2"){
        digitalWrite(12, LOW);
        Serial.print("Off 2");
        char* light2 = "light 2 is off";
        client.publish("light/status", light2);

      }
      else if(messageTemp == "off3"){
        digitalWrite(16, LOW);
        Serial.print("Off 3");
        char* light3 = "light 3 is off";
        client.publish("light/status", light3);

      }
  }
  Serial.println();
}
ebivan commented 6 years ago

Hey there, I got the same question, have been trying for half a day now. Could you please post a whole sketch that reads/subcribles multiple topics and then acts to the content/payload.?

I imagine it to be something like that:

subscribe to topic1 if payload of topic1 is x do y

subscribe to topic2 if payload of topic1 is z do q

and so on.

I would like to build a device that controls a couple of relays and switches them on and of acording to the states of the different topics...

rezahili commented 6 years ago

hi here tell u how to do what u want

ebivan commented 6 years ago

Thanks, here is my complete solution for subscribing to multiple topics and doing stuff depending on the topic and payload:

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

const char* ssid = "your_ssid";
const char* password = "your_password";
const char* mqtt_server = "your_mqtt_broker";
String clientId = "enter_unique_id_for_this_device_here";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {
  delay(10);
  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());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.println("] ");

  // Reading the pointers and creating strings to work with
  String strTopic = topic;
  String strPayload="";
  char c;
  for (int i = 0; i < length; i++) {
    c=(char)payload[i];
    strPayload += c;
  } 

  // Doing something with the values from topic and payload
  if ((strTopic == "Wohnzimmer/Schalter/state") && (strPayload == "on")) {
    digitalWrite(D1, HIGH);
  } else if ((strTopic == "Wohnzimmer/Schalter/state") && (strPayload == "off")) {
    digitalWrite(D1, LOW);
  }

    else if ((strTopic == "Wohnzimmer/Schalter2/state") && (strPayload == "on")) {
    digitalWrite(D2, HIGH);
  } else if ((strTopic == "Wohnzimmer/Schalter2/state") && (strPayload == "off")) {
    digitalWrite(D2, LOW);
  }

}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
     client.subscribe("Wohnzimmer/Schalter/state");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.subscribe("Wohnzimmer/Schalter/state");
  client.loop();
  delay(10);

  client.subscribe("Wohnzimmer/Schalter2/state");
  client.loop();
  delay(10);

  // Add more topics here by copying the three lines above and putting in the desired topic
}