first commit

This commit is contained in:
2021-03-06 14:34:47 +01:00
commit 866af0700e
2 changed files with 212 additions and 0 deletions

View File

@@ -0,0 +1,209 @@
/*
SimpleMQTTClient.ino
The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
It will also send a message delayed 5 seconds later.
*/
//Define DEBUG to get the Output from DEBUG_PRINTLN
#define DEBUG 1
#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println (x)
#else
#define DEBUG_PRINTLN(x)
#endif
#include "EspMQTTClient.h"
#include <esp_task_wdt.h>
#define WDT_TIMEOUT 900 //Seconds timeout, 15min
static const String HOSTNAME = "hs2";
static const String HEARTBEATTOPIC = "stat/hs1/hk2/temperature";
static const int ResetPin = 35;
static const int HKPin[4] = {32,33,25,26};
int HKTarget[4] = {0,0,0,0};
int HKStatus[4] = {1,1,1,1};
float HKCurrent[4] = {35.0,35.0,35.0,35.0};
String HKDebugMessage;
String HKstatusTopic[4]; // HK1 1/0 status information
String HKtargetTopic[4]; // target from APP/Web
String HKtemperatureTopic[4]; // temperature from sensor
String Output;
int HK;
int CountHeartBeats;
bool resetWDT;
EspMQTTClient client(
"EasyBox-368239",
"inginf95",
"192.168.178.36", // MQTT Broker server ip
"", // Can be omitted if not needed
"", // Can be omitted if not needed
"HS2", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
void setup()
{
Serial.begin(115200);
DEBUG_PRINTLN("**** STARTING SETUP ****");
//Set up Relais Pins
for (int i=0;i<=3;i++){
pinMode(HKPin[i],OUTPUT);
digitalWrite(HKPin[i], HIGH);
}
for (int i=0;i<=3;i++){
HK=i+1;
HKstatusTopic[i] = "stat/"+HOSTNAME+"/hk"+HK+"/status";
HKtargetTopic[i] = "stat/"+HOSTNAME+"/hk"+HK+"/target_temperature";
HKtemperatureTopic[i] = "stat/"+HOSTNAME+"/hk"+HK+"/temperature";
}
HKDebugMessage = "stat/"+HOSTNAME+"/debug";
// Optionnal functionnalities of EspMQTTClient :
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableHTTPWebUpdater("nils","inginf"); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
//client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
}
//This function transfers the state of the sensor. That includes the door status, battery status and level
void transmitStatus() {
DEBUG_PRINTLN(__func__);
String Output = "";
bool publish = false;
for (int i=0;i<=3;i++){
HK=i+1;
if (HKCurrent[i] <= HKTarget[i]) {
Output += "HK"+String(HK);
Output += "=1 ";
if (HKStatus[i] == 0) {
publish = true;
digitalWrite(HKPin[i], LOW);
//Transfer the current state of the sensor to the MQTT broker
client.publish(HKstatusTopic[i], "1" );
HKStatus[i] = 1;
}
} else {
Output += "HK"+String(HK);
Output += "=0 ";
if (HKStatus[i] == 1) {
publish = true;
digitalWrite(HKPin[i], HIGH);
//Transfer the current state of the sensor to the MQTT broker
client.publish(HKstatusTopic[i], "0" );
HKStatus[i] = 0;
}
}
Output += String(HKCurrent[i]);
Output += " ";
Output += String(HKTarget[i]);
Output += "\n";
}
if (publish==true) {
client.publish(HKDebugMessage.c_str(), Output.c_str() );
DEBUG_PRINTLN(Output.c_str());
}
}
// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
client.subscribe(HEARTBEATTOPIC, [](const String & payload) {
resetWDT = true;
CountHeartBeats++;
Output = "Heart-Beat count: "+String(CountHeartBeats);
client.publish(HKDebugMessage.c_str(), Output.c_str() );
DEBUG_PRINTLN(Output.c_str());
transmitStatus();
DEBUG_PRINTLN("received heartbeat topic");
});
client.subscribe("stat/"+HOSTNAME+"/#", [](const String & topic, const String & payload) {
bool publish = false;
//Check all other messages
for (int i=0;i<=3;i++){
HK=i+1;
if (topic.equals(HKtargetTopic[i])) {
resetWDT = true;
publish = true;
Output = "Neue Target Message (Pumpe"+String(HK);
Output += "): ";
//Update HKiTarget...
if (payload.equals("ON")) {
HKTarget[i] = 1;
} else {
HKTarget[i] = 0;
}
Output += String(HKTarget[i]);
Output += " payload: ";
Output += payload;
client.publish(HKDebugMessage.c_str(), Output.c_str() );
DEBUG_PRINTLN(Output.c_str());
}
}
for (int i=0;i<=3;i++){
HK=i+1;
if (topic.equals(HKtargetTopic[i])) {
Output = "Neue Ziel-Raumtemperatur (HK"+String(HK);
Output += "): ";
//Update HKiTarget...
HKTarget[i] = payload.toInt();
Output += String(HKTarget[i]);
client.publish(HKDebugMessage.c_str(), Output.c_str() );
DEBUG_PRINTLN(Output.c_str());
} else {
if (topic.equals(HKtemperatureTopic[i])) {
Output = "Neue Raumtemperatur-Messung (HK"+String(HK);
Output += "): ";
//Update HKiCurrent...
HKCurrent[i] = payload.toFloat();
//Work around as the toFloat() seems to return a wrong decimal point sometimes...
if (HKCurrent[i]>100) { HKCurrent[i]=HKCurrent[i]/10; };
Output += String(HKCurrent[i]);
client.publish(HKDebugMessage.c_str(), Output.c_str() );
DEBUG_PRINTLN(Output.c_str());
}
}
}
if (publish){
transmitStatus();
CountHeartBeats = 0; //reset heartbeatcounter
Output = "Heart-Beat count: "+String(CountHeartBeats);
DEBUG_PRINTLN(Output.c_str());
}
});
DEBUG_PRINTLN("**** ENDING SETUP ****");
}
void loop()
{
if (resetWDT){
esp_task_wdt_reset();
resetWDT = false;
}
client.loop();
}

3
readme.md.txt Normal file
View File

@@ -0,0 +1,3 @@
# Fussboden Steuerung
Small ESP32 project to control the heating vents based on temperture sensors.