/** * MQTT client interface for Power PWM (PID) Regler for StromLog * Version 0.62 * 31.03.2024 P. Rebesky * author Creator P.Rebesky * Copyright (©): 2024-2028 by Peter Rebesky * This code can use in private cases only. Every business or companies using of this codes or codes parts is required an approval of us (me) * Every private using can exchange some parts of code or modify some code-lines. This code is allowed change for private use only. * This software is basicly owned by Peter Rebesky and any comercial using is forbidden without approval of us (me). **/ #ifndef MQTT_CLIENT_H_ #define MQTT_CLIENT_H_ #include #include #include "global.h" #include "configPWR.h" WiFiClient espClient; PubSubClient client(espClient); extern controlRelais relais; extern configPWR cPWR; //*********** struct and variables for mqtt client ******************************************/ struct mqttVariables{ unsigned int MQTTWaitTime2Connect=120; unsigned int MQTTBrokerOK=0; unsigned int MQTTInterval=0; unsigned int MQTTtcpPort=1883; bool MQTTsetupOK=false; String MQTTtarget = "192.168.10.47"; String MQTTuser = "User"; String MQTTpassword = "user"; }; mqttVariables mqtt; //*********** parts for send data over mqtt message to broker *******************************/ String getStatusData(){ String message="{\"Watt\":"; message += _actualConsumption; #ifdef PMW_4_X4 message += ",\"r1\":"; message += digitalRead(RELAIS1); message += ",\"r2\":"; message += digitalRead(RELAIS2); #endif message += ",\"r3\":"; message += digitalRead(SecureRELAIS); message += ",\"rp\":"; message += digitalRead(CirculationPump); message += ",\"pwm\":"; message += 255-relais.PWMvalue; message += ",\"pro\":"; message += relais.PWM_Power; message += ",\"kwh\":"; message += _sumConsumption/(float)cPWR.FactorKWH; message += ",\"tm0\":"; message += _temperature0; message += ",\"tm1\":"; message += _temperature1; message += ",\"RpOf\":"; message += relais.getPumpOffTime(); message += ",\"atm\":"; message += cPWR.getAutomatic(); message += ",\"er\":"; message += _errorCount; message += ",\"Debug\":"; message += _actualDebugValue; message += "}"; return message; } String getMQTTpath(){ String path="PWRegler/"; path += cPWR.getPName(); return path; } bool sendStatusData2mqtt(){ String path=getMQTTpath(); path += "/status"; String message=getStatusData(); client.publish(path.c_str(),message.c_str()); } //*** check and reconnect if it nesseccary *****// void reconnect() { // Loop until we're reconnected or timeout #define maxLoop 5 uint maximalLoop=0; while (!client.connected() && maximalLoop <= maxLoop) { String clientId = "PWRegler-"; clientId += cPWR.getPName(); // Create client ID if (client.connect(clientId.c_str(),cPWR.getBrokerUser().c_str(),cPWR.getBrokerPW().c_str())) { // Attempt to connect // client.subscribe("PWRegler"); // Once connected, publish an announcement... yield(); } delay(100); _mqttWaitTime2Connect = 0; maximalLoop++; } if(maximalLoop >= maxLoop){ _mqttBrokerOK=0; // turn off, because broker is not available _mqttWaitTime2Connect = 120; } else _mqttBrokerOK=1; } //*********************** set mqtt server address ************************ void setMQTTserver(){ IPAddress broker; client.disconnect(); broker.fromString(cPWR.getIoBroker()); if(broker)client.setServer(broker, 1883); else cPWR.SendMQTT=0; client.setSocketTimeout(2); //socket timeout interval in Seconds // Serial.println(broker.toString()); } void handleBrokerConnect(int timeSecond){ if(timeSecond == 0 && _mqttBrokerOK==1){ if (!client.connected()){ reconnect(); } } if(_mqttBrokerOK==0){ _mqttWaitTime2Connect--; if(_mqttWaitTime2Connect==0) reconnect(); } } //*********** broker main from loop *************************************// void handleMQTTclient(){ if(!mqtt.MQTTsetupOK){ setMQTTserver(); mqtt.MQTTsetupOK=true; } else if(_mqttInterval <= 0 && _mqttBrokerOK){ sendStatusData2mqtt(); _mqttInterval = cPWR.MQTT_INTERVAL; } handleBrokerConnect(_second); _mqttInterval--; } //*********** mqtt parts end ****************************************************/ #endif //*** MQTT_CLIENT_H_