Skip to content

Commit

Permalink
MQTT : publish on change implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
SurfGargano committed Mar 28, 2023
1 parent c2fa46d commit 261adcf
Show file tree
Hide file tree
Showing 9 changed files with 2,375 additions and 2,094 deletions.
3 changes: 3 additions & 0 deletions software_esp32/include/VdmConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef struct {
uint8_t publishAllTemps : 1;
uint8_t publishPathAsRoot : 1 ;
uint8_t publishUpTime : 1;
uint8_t publishOnChange : 1;
} VDM_PROTOCOL_CONFIG_FLAGS;

typedef struct {
Expand All @@ -100,6 +101,7 @@ typedef struct {
char userPwd[65];
VDM_PROTOCOL_CONFIG_FLAGS protocolFlags;
uint16_t keepAliveTime;
uint32_t minBrokerDelay;
} VDM_PROTOCOL_CONFIG;

typedef struct {
Expand Down Expand Up @@ -196,6 +198,7 @@ typedef struct
#define nvsProtBrokerPwd "brokerPwd"
#define nvsProtBrokerPublishFlags "brokerPF"
#define nvsProtBrokerKeepAliveTime "brokerKAT"
#define nvsProtBrokerMinBrokerDelay "brokerMD"

#define nvsValvesCfg "valvesCfg"
#define nvsValvesControlCfg "valvesCtrlCfg"
Expand Down
2 changes: 2 additions & 0 deletions software_esp32/include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

#include <Arduino.h>

#define CHECK_BIT(var,pos) ((var>>pos) & 1)

// #define CONFIG_ASYNC_TCP_USE_WDT 0

#define ACTUATOR_COUNT 12
Expand Down
41 changes: 41 additions & 0 deletions software_esp32/include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#pragma once

#include "globals.h"

#define MAINTOPIC_LEN 50

// MQTT settings
Expand All @@ -48,6 +50,37 @@
#define DEFAULT_VALVESTOPIC "valves/"
#define DEFAULT_TEMPSTOPIC "temps/"


#define publishNothing 0
#define publishCommon 1
#define publishValves 2
#define publishTemps 4

typedef struct {
uint8_t position;
uint8_t target;
uint8_t state;
uint16_t meanCurrent;
int temp1;
int temp2;
uint32_t ts;
bool publishNow;
bool publishTimeOut;
} LASTVALVEVALUES;

typedef struct {
int16_t temperature; // temperature of assigned sensor
char id[25];
uint32_t ts;
bool publishNow;
} LASTTEMPVALUES;

typedef struct {
uint8_t heatControl;
uint8_t parkingPosition;
uint8_t systemState;
} LASTCOMMONVALUES;

class CMqtt
{
public:
Expand All @@ -60,7 +93,11 @@ class CMqtt
bool mqttReceived;
private:
void reconnect();
void publish_all (uint8_t publishFlags);
void publish_common ();
void publish_valves ();
void publish_temps ();
uint8_t checkForPublish();
bool firstPublish;
char mqtt_mainTopic[MAINTOPIC_LEN];
char mqtt_commonTopic[MAINTOPIC_LEN];
Expand All @@ -69,6 +106,10 @@ class CMqtt
char mqtt_callbackTopic[MAINTOPIC_LEN];
char stationName[MAINTOPIC_LEN];
uint32_t tsPublish;
bool forcePublish;
LASTCOMMONVALUES lastCommonValues;
LASTVALVEVALUES lastValveValues[ACTUATOR_COUNT];
LASTTEMPVALUES lastTempValues[TEMP_SENSORS_COUNT];
};

extern CMqtt Mqtt;
7 changes: 7 additions & 0 deletions software_esp32/src/VdmConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ void CVdmConfig::clearConfig()
configFlash.protConfig.brokerPort = 0;
configFlash.protConfig.brokerInterval = 1000;
configFlash.protConfig.publishInterval = 10;
configFlash.protConfig.minBrokerDelay = 5;
memset (configFlash.protConfig.userName,0,sizeof(configFlash.protConfig.userName));
memset (configFlash.protConfig.userPwd,0,sizeof(configFlash.protConfig.userPwd));
configFlash.protConfig.protocolFlags.publishTarget = false;
configFlash.protConfig.protocolFlags.publishAllTemps = false;
configFlash.protConfig.protocolFlags.publishPathAsRoot = false;
configFlash.protConfig.protocolFlags.publishUpTime = false;
configFlash.protConfig.protocolFlags.publishOnChange = false;
configFlash.protConfig.keepAliveTime = 60;


for (uint8_t i=0; i<ACTUATOR_COUNT; i++) {
configFlash.valvesConfig.valveConfig[i].active = false;
Expand Down Expand Up @@ -202,6 +205,7 @@ void CVdmConfig::readConfig()
uint8_t a=prefs.getUChar(nvsProtBrokerPublishFlags,7);
configFlash.protConfig.protocolFlags = *(VDM_PROTOCOL_CONFIG_FLAGS *)&a;
configFlash.protConfig.keepAliveTime = prefs.getUShort(nvsProtBrokerKeepAliveTime,60);
configFlash.protConfig.minBrokerDelay = prefs.getUShort(nvsProtBrokerMinBrokerDelay,5);
prefs.end();
}

Expand Down Expand Up @@ -277,6 +281,7 @@ void CVdmConfig::writeConfig(bool reboot)
uint8_t a = *(uint8_t *)&configFlash.protConfig.protocolFlags;
prefs.putUChar(nvsProtBrokerPublishFlags,a);
prefs.putUShort(nvsProtBrokerKeepAliveTime,configFlash.protConfig.keepAliveTime);
prefs.putUShort(nvsProtBrokerMinBrokerDelay,configFlash.protConfig.minBrokerDelay);
}
prefs.end();

Expand Down Expand Up @@ -360,7 +365,9 @@ void CVdmConfig::postProtCfg (JsonObject doc)
if (!doc["pubAllTemps"].isNull()) configFlash.protConfig.protocolFlags.publishAllTemps = doc["pubAllTemps"];
if (!doc["pubPathAsRoot"].isNull()) configFlash.protConfig.protocolFlags.publishPathAsRoot = doc["pubPathAsRoot"];
if (!doc["pubUpTime"].isNull()) configFlash.protConfig.protocolFlags.publishUpTime = doc["pubUpTime"];
if (!doc["pubOnChange"].isNull()) configFlash.protConfig.protocolFlags.publishOnChange = doc["pubOnChange"];
if (!doc["keepAliveTime"].isNull()) configFlash.protConfig.keepAliveTime = doc["keepAliveTime"];
if (!doc["pubMinDelay"].isNull()) configFlash.protConfig.minBrokerDelay = doc["pubMinDelay"];
}

void CVdmConfig::postValvesCfg (JsonObject doc)
Expand Down
Loading

0 comments on commit 261adcf

Please sign in to comment.