Loop function added, generic timer added, message buffer increased (you never know)

This commit is contained in:
Cabooman 2024-01-30 07:36:43 +01:00
parent bf37f655df
commit 957a91e55e
6 changed files with 41 additions and 4 deletions

View file

@ -127,6 +127,9 @@ void loop() {
#ifdef WEBSERVER #ifdef WEBSERVER
// Over-the-air updates by ElegantOTA // Over-the-air updates by ElegantOTA
ElegantOTA.loop(); ElegantOTA.loop();
#ifdef MQTT
mqtt_loop();
#endif
#endif #endif
// Input // Input

View file

@ -4,6 +4,7 @@
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include "../../../USER_SETTINGS.h" #include "../../../USER_SETTINGS.h"
#include "../../lib/knolleary-pubsubclient/PubSubClient.h" #include "../../lib/knolleary-pubsubclient/PubSubClient.h"
#include "../utils/timer.h"
const char* mqtt_subscriptions[] = MQTT_SUBSCRIPTIONS; const char* mqtt_subscriptions[] = MQTT_SUBSCRIPTIONS;
const size_t mqtt_nof_subscriptions = sizeof(mqtt_subscriptions) / sizeof(mqtt_subscriptions[0]); const size_t mqtt_nof_subscriptions = sizeof(mqtt_subscriptions) / sizeof(mqtt_subscriptions[0]);
@ -13,6 +14,7 @@ PubSubClient client(espClient);
char msg[MSG_BUFFER_SIZE]; char msg[MSG_BUFFER_SIZE];
int value = 0; int value = 0;
static unsigned long previousMillisUpdateVal; static unsigned long previousMillisUpdateVal;
MyTimer publish_global_timer(5000);
/** Publish global values and call callbacks for specific modules */ /** Publish global values and call callbacks for specific modules */
static void publish_values(void) { static void publish_values(void) {
@ -28,7 +30,7 @@ static void publish_values(void) {
"}\n", "}\n",
((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0, ((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0,
((float)((int16_t)temperature_max)) / 10.0, cell_max_voltage, cell_min_voltage); ((float)((int16_t)temperature_max)) / 10.0, cell_max_voltage, cell_min_voltage);
bool result = client.publish("battery/info", msg, true); bool result = client.publish("battery_testing/info", msg, true);
Serial.println(msg); // Uncomment to print the payload on serial Serial.println(msg); // Uncomment to print the payload on serial
} }
@ -79,9 +81,8 @@ void init_mqtt(void) {
void mqtt_loop(void) { void mqtt_loop(void) {
if (client.connected()) { if (client.connected()) {
client.loop(); client.loop();
if (millis() - previousMillisUpdateVal >= 5000) // Every 5s if (publish_global_timer.elapsed() == true) // Every 5s
{ {
previousMillisUpdateVal = millis();
publish_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus. publish_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
} }
} else { } else {
@ -92,3 +93,7 @@ void mqtt_loop(void) {
} }
} }
} }
bool mqtt_publish(const String& topic, const String& payload) {
return client.publish(topic.c_str(), payload.c_str());
}

View file

@ -55,5 +55,6 @@ extern const char* mqtt_password;
void init_mqtt(void); void init_mqtt(void);
void mqtt_loop(void); void mqtt_loop(void);
bool mqtt_publish(const String& topic, const String& payload);
#endif #endif

View file

@ -0,0 +1,12 @@
#include "timer.h"
MyTimer::MyTimer(unsigned long interval) : interval(interval), previousMillis(0) {}
bool MyTimer::elapsed() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
return true;
}
return false;
}

View file

@ -0,0 +1,16 @@
#ifndef __MYTIMER_H__
#define __MYTIMER_H__
#include <Arduino.h>
class MyTimer {
public:
MyTimer(unsigned long interval);
bool elapsed();
private:
unsigned long interval;
unsigned long previousMillis;
};
#endif // __MYTIMER_H__

View file

@ -23,7 +23,7 @@
// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize(). // MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
#ifndef MQTT_MAX_PACKET_SIZE #ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 256 #define MQTT_MAX_PACKET_SIZE 512
#endif #endif
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive() // MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()