For switching the lights in my house I use Domoticz with SonOff tasmota switches and doing this makes me a happy person. The next stap is using Domoticz with een ESP32 Wroom board witch I want to setup as an alarm system.How easy can it be you would think, Just let the call back function listen to the topic "domoticz/out" and handle the payload when the massage arrives. But nothing happens. So I stated up MQTTLens and setup the connection. Here with the same topic "domoticz/out" it shows the whole payload received from Domoticz. But still nothing shows up on the ESP32. When I now copy the payload received by MQTTLens en publish the content of it with the same topic as before "domoticz/out" the ESP 32 call back function works beautifully nice and shows the topic as well as the content of the payload. The problem now is that everything works fine with the interference of MQTTLens and not as it also should be, directly between Domoticz and the ESP32.
#include <ArduinoJson.h>
#include <DallasTemperature.h>
#include <OneWire.h>
const int temperature_pin = 21; // connection pin DS18B20
OneWire oneWire(temperature_pin);
DallasTemperature sensors(&oneWire);
/*
This skech works on a 32ESP dev wroom board to
*/
#include <PubSubClient.h>
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
const uint8_t mqtt_server[] = {192, 168, 2, 150};
const char* WiFiUser = "xxxxxxxxxxxx";
const char* WiFiPassword = "xxxxxxxxxxxxxxxx";
const char* mqttUser = "xxxxxxxxxxxxx";
const char* mqttPassword = "xxxxxxxxxxxx";
const int DomoticzInx = 6;
//int readerHumidity = 0;
//const int reader_1 = 35; // connection pin sensor humidity
//const int reader_1_power = 32;
char msg[50];
int value = 0;
float temperatureOld ;
const int ledPin = 15; // connection pin relais
// ////////////////call back////////////////////////////////////
void callback(char* topic, byte* payload, unsigned int length) {
char str[length + 1];
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]\n ");
for ( int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
str[i] = (char)payload[i];
}
// Serial.println();
StaticJsonDocument <512> doc;
deserializeJson(doc, payload); // deserializeJson(doc,str); can use string instead of payload
int idx = doc["idx"];
int nvalue = doc["nvalue"];
Serial.println("idx =");
Serial.println(idx);
Serial.println("nvalue =");
Serial.println(nvalue);
}
// ///////////////reconnect////////////////////////////
void reconnect() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
if (mqttClient.connect("esp32temp666666666666", mqttUser, mqttPassword)) {
Serial.println("connected");
boolean r = mqttClient.subscribe("domoticz/out");
Serial.print("subscribe ");
Serial.println(r);
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
// ////////////////////////////setup/////////////////////
void setup()
{
Serial.begin(115200);
delay(100);
sensors.begin(); // Start the DS18B20 sensor
WiFiMulti.addAP(WiFiUser, WiFiPassword); // We start by connecting to a WiFi network
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);
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(callback);
pinMode(ledPin, OUTPUT);
}
// //////////////////////////loop////////////////////////
void loop()
{
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
toDomoticzTemp();
}
// ////////////////SendToDomotczTemperature//////////////////////////////
void toDomoticzTemp() {
sensors.requestTemperatures(); //prepair and send
delay(20);
float temperatureC = sensors.getTempCByIndex(0);
if (temperatureC < -100) {
temperatureC = temperatureOld;
}
if ((temperatureC < (temperatureOld - 0.3)) or (temperatureC > (temperatureOld + 0.3)) ) {
temperatureOld = temperatureC;
char tempString[8];
dtostrf(temperatureC, 1, 1, tempString);
Serial.print("Temperatue: ");
Serial.println(tempString);
// toDomoticzTemp(tempString);
StaticJsonDocument<256> doc;
doc["idx"] = DomoticzInx ;
//data["nvalue"] = 22;
doc["svalue"] = tempString ;
char out[128];
int b = serializeJson(doc, out);
Serial.print("publishing bytes = ");
Serial.println(b, DEC);
mqttClient.publish("domoticz/in", out);
// “domoticz/in” , “{ \”idx\”:1,\”nvalue\”:0,\”svalue\”:\”18.4\”}// tempeature
// “domoticz/in” , “{ \”idx\”:2,\”nvalue\”:0,\”svalue\”:\”1.2 ; 33 ; 1\”}// first temp, then hum, then ?
}
}