Good evening Homies,
Trying to implement MQTT with a ESP32.
running:
Mosquitto broker
Home assistant
Host system
Hostname raspberrypi
System Raspbian GNU/Linux 10 (buster)
Running client.publish("xyz", buffer, n);
it sends the data across perfectly and I can grab that
but when I try to use a callback on the other hand void callback(char* topic, byte* payload, unsigned int length)
I’m getting nothing.
I’ve tried to work it out using the MQTT Settings listen to a topic in the configuration menu and can see it being pressed:
Message 12 received on /office/light1/switch/ at 21:36:
ON
QoS: 0 - Retain: false
Message 11 received on /office/light1/switch/ at 21:36:
ON
QoS: 0 - Retain: false
My yaml is:
light:
- platform: mqtt
name: “Office light”
state_topic: “office/light1/status”
command_topic: “office/light1/switch”
payload_on: “ON”
payload_off: “OFF”
optimistic: false
switch: - platform: mqtt
name: “Example_Switch”
state_topic: “room/light”
command_topic: “room/light”
payload_on: “on”
payload_off: “off”
At this point, a little lost for what to try next. Any ideas or pointers would be great, i see from the forum this issue comes up a lot. I’ve tried including the delay(100);
after the 1client.loop()1 as suggested in a few of the other post but no luck.
feel like it’s going to be a something small as the logs show up fine with the connection and i can receive it with no error.
Cheers.
Copy of code:
#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <WiFiManager.h>
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long updateCurrentMillis;
unsigned long lastUpdateDelay;
unsigned long updateDelay = 1000;
// MQTT Network
const char* mqtt_server = "192.168.0.200";
//Node ID
const char *ID = "brainESP32"; // Name of our device, must be unique
//Topics
const char *TOPIC = "room/light"; // Topic to subcribe to
const char *STATE_TOPIC = "room/light/state"; // Topic to publish the light state to
// Home Assistant Credentials
const char *HA_USER = "********";
const char *HA_PASS = "*******";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
setup_wifi(); // Connect to network
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void callback(char* topic, byte* payload, unsigned int length) {
String topicStr = topic;
Serial.print("Topic: ");
Serial.println(topicStr);
if (topicStr == "/office/light1/switch/") {
//turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
if (payload[0] == 'ON') {
Serial.println("light on");
client.publish("/office/light1/status/", "ON");
}
//turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
else if (payload[0] == 'OFF') {
Serial.println("light off");
client.publish("/office/light1/status/", "OFF");
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
delay(100);
updateCurrentMillis = millis();
if (updateCurrentMillis - lastUpdateDelay >= updateDelay) {
lastUpdateDelay = updateCurrentMillis;
switchOut();
}
}
// Reconnect to client
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(ID, HA_USER, HA_PASS)) {
Serial.println("connected");
Serial.print("Publishing to: ");
Serial.println(ID);
Serial.println('\n');
}
else {
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup_wifi() {
Serial.print("\nConnecting to ");
//WiFiManager
WiFiManager wifiManager;
Serial.println("started");
//wifiManager.resetSettings(); // turn off after intital settingfs are done
wifiManager.setCustomHeadElement("<style>html{filter: invert(40%); -webkit-filter: invert(40%);}</style>");
WiFiManagerParameter custom_text("<p>This is just a text paragraph</p>");
wifiManager.addParameter(&custom_text);
wifiManager.setAPStaticIPConfig(IPAddress(100, 100, 100, 100), IPAddress(100, 100, 100, 100), IPAddress(255, 255, 255, 0));
wifiManager.autoConnect();
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void switchOut() {
const size_t capacity = JSON_OBJECT_SIZE(26);
DynamicJsonDocument doc1(capacity);
DynamicJsonDocument doc2(capacity);
doc1["pbrAM"] = 0;
doc1["pbrSS"] = 0;
doc1["lightAM"] = 0;
doc1["lp_1"] = 0;
doc1["lp1_1"] = 0;
doc1["lp1_2"] = 0;
doc1["lp1_3"] = 1;
doc1["lp1_4"] = 0;
doc1["lp_2"] = 0;
doc1["lp2_1"] = 1;
doc1["lp2_2"] = 0;
doc1["lp2_3"] = 0;
doc1["lp2_4"] = 0;
char buffer[256];
size_t n = serializeJson(doc1, buffer);
client.publish("brainOut1", buffer, n);
doc2["tempAM"] = 0;
doc2["tempSS"] = 1;
doc2["airAM"] = 0;
doc2["airSS"] = 0;
doc2["doseAM"] = 0;
doc2["doseSS"] = 1;
doc2["dosephU"] = 0;
doc2["dosepHD"] = 0;
doc2["doseNut"] = 0;
doc2["doseSam"] = 0;
doc2["doseFill"] = 0;
doc2["harvsetAM"] = 0;
doc2["harvestSS"] = 0;
n = serializeJson(doc2, buffer);
client.publish("brainOut2", buffer, n);
}