After having used Openhabian and Home Assistant I have made a begin with Domoticz. Until now I'm impressed by the way it works. But I do have a problem getting my ESP32 board working with the MQTT build-in in Domoticz.
Domoticz MQTT is working fine. With the topic domoticz/out put in MQTTLens I get the massage compleet in Jason.
The ESP32 has contact with the MQTT Broker as the test message (Hello from ESP8266) shows itself in the serial monitor. This with the same topic "domoticz/out". Now the problem explained. MQTT works well on Domoticz as well as on Arduino using the same topic. Arduino goes true the selftest remarkebly good but when I tumble a switch in Domoticz what shows up in MQTTLens I do not get any reaction from my callback routine on the ESP32. I hope that someone will be able to help me in the right direction with this.
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
WiFiClient espClient;
PubSubClient client(espClient);
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "raspberrypi";
const uint8_t mqtt_server[] = {192, 168, 2, 150};
const char* mqttUser = "XXX";
const char* mqttPassword = "XXX";
long lastMsg = 0;
// char msg[50];
// int value = 0;
// float temperature = 0;
// float humidity = 0;
const int ledPin = 15;
#define LED D4
#define MYIDX 1528
// ////////////////////////////setup/////////////////////
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
WiFiMulti.addAP(mqttUser ,mqttPassword);
Serial.println();
Serial.println();
Serial.print("Waiting for WiFi... ");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin, OUTPUT);
// temperature = 33;
}
// //////////////////////////loop////////////////////////
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
/*
// Temperature in Celsius
temperature = temperature + 1;
if (temperature > 100) {
temperature = 0;
}
humidity = temperature - 5;
// Uncomment the next line to set temperature in Fahrenheit
// (and comment the previous temperature line)
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
// Convert the value to a char array
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
// Serial.print("Temperature: ");
// Serial.println(tempString);
client.publish("ESP32/temperature", tempString);
// humidity = 44;
// Convert the value to a char array
char humString[8];
dtostrf(humidity, 1, 2, humString);
// Serial.print("Humidity: ");
// Serial.println(humString);
client.publish("ESP32/humidity", humString);
*/
}
}
// ----- START CALLBACK ROUTINE -----
// This function runs when the client.loop() function detects receipt of a new message
void callback(char* topic, byte* payload, unsigned int length) //Get topic, payload, and length
{
Serial.print("\n\rMessage arrived in topic: "); //Message arrived, so print topic
Serial.println(topic);
Serial.print("Payload: "); //Print "Payload: "
Serial.println((char*)payload);
Serial.println("-----------------------");
StaticJsonDocument<256> doc;
deserializeJson(doc, payload, length);
int index = doc["idx"];
int value = doc["nvalue"];
Serial.println(index);
Serial.println(value);
if (index == 3){
if (value == 1){
Serial.print("ON");
}
if (value == 0){
Serial.print("OFF");
}
}
// use the JsonDocument as usual...
}
// ----- END CALLBACK ROUTINE -----
// ///////////////reconnect////////////////////////////
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("esp32temp", mqttUser, mqttPassword)) {
Serial.println("connected");
// Subscribe
client.subscribe("domoticz/out"); //subscribe the topic you want to act on!!!!!!!!!!!!!
client.publish("domoticz/out", "Hello from ESP8266"); //testing the subsciption
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//--