I need help with MQTT callback function for more than 1 subscribed topic.
Can you breakdown the void callback line?
This is what I understand:
If I publish mosquitto_pub -t 'Chart/PS' -m '0'
the my "chartsPin" should should go HIGH and the led come on
and
If I publish mosquitto_pub -t 'Busy/Office_A' -m '0'
the my "officeaPin" should should go HIGH and the led come on
// ????????????????? VOID CALLBACK ???????????????????????
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
//TOPIC 1
if (topic == "Charts/PS") {
Serial.print("Reception LED is ");
if (Charts/PS == "1") {
digitalWrite(chartsPin, HIGH);
Serial.print("On");
}
else if (Charts/PS == "0") {
digitalWrite(chartsPin, LOW);
Serial.print("Off");
}
}
// TOPIC 2
if (topic == "Busy/Office_A") {
Serial.print("Office A LED is ");
if (PSA == "1") {
digitalWrite(psaPin, HIGH);
Serial.print("On");
}
else if (PSA == "0") {
digitalWrite(psaPin, LOW);
Serial.print("Off");
}
Serial.println();
}
Entire Sketch:
/*
* Reception
*/
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
WiFiClient espClient;
PubSubClient client(espClient);
// Connect to the WiFi
const char* ssid = "MYSSID";
const char* password = "MYPASSWORD";
const char* mqtt_server = "Mosquitto_broker";
// Constants won't change: Name pins. It's easier to understand when reading through code
const int buttonPin = D2; // the pin that the pushbutton is attached to
const int chartsPin = D1; // LED pin for "Charts""
const byte officeaPin = D5; // LED pin for "Office A"
const byte officebPin = D6; // LED pin for "Office B"
const byte officecPin = D7; // LED pin for "Office C"
const byte officedPin = D8; // LED pin for "Office D"
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// ????????????????? VOID CALLBACK ???????????????????????
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++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
//TOPIC 1
if (topic == "Charts/PS") {
Serial.print("Reception LED is ");
if ("Charts/PS" == "1") {
digitalWrite(chartsPin, HIGH);
Serial.print("On");
}
else if ("Charts/PS" == "0") {
digitalWrite(chartsPin, LOW);
Serial.print("Off");
}
}
// TOPIC 2
if (topic == "Busy/Office_A") {
Serial.print("Office A LED is ");
if ("Office_A" == "1") {
digitalWrite(officeaPin, HIGH);
Serial.print("On");
}
else if ("Office_A" == "0") {
digitalWrite(officeaPin, LOW);
Serial.print("Off");
}
Serial.println();
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("Reception Desk")) {
Serial.println("Connected");
// ... and subscribe to topics
client.subscribe("Charts/PS");
client.subscribe("Busy/Office_A");
client.subscribe("Busy/Office_B");
client.subscribe("Busy/Office_C");
client.subscribe("Busy/Office_D");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
{
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
//DEFINE I/O
pinMode(buttonPin, INPUT); // initialize the button pin as a input:
pinMode(chartsPin, OUTPUT); // initialize the LED as an output:
pinMode(officeaPin, OUTPUT); // initialize the LED as an output:
// pinMode(officebPin, OUTPUT); // initialize the LED as an output:
// pinMode(officecPin, OUTPUT); // initialize the LED as an output:
// pinMode(officedPin, OUTPUT); // initialize the LED as an output:
Serial.begin(9600);// initialize serial communication:
}
// Connect to WiFinetwork
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.begin(9600);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (!client.connected()) {
reconnect();
}
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
client.publish("Charts/PS", "0");
Serial.println("LED is On");
// if the current state is HIGH then the button
// went from off to on:
} else {
// if the current state is LOW then the button
// went from on to off:
client.publish("Charts/PS", "1");
Serial.println("LED is off");
}
// Do I need a delay for debounce with Adafruit Capacitive Touch Toggle Switch?
delay(50);//if not I can delete this line.
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
client.loop();
}