chore: housekeeping

This commit is contained in:
2024-11-12 00:55:52 +06:00
parent 5f9c966602
commit 48943ba71f
32 changed files with 715 additions and 854 deletions

View File

@@ -6,13 +6,13 @@
#include "debug.h"
#include "gpio.h"
#include "modbus_handler.h"
#include "modbus.h"
#include "onewire_temp.h"
#include "systick.h"
// MQTT
#define MQTT_YIELD_INTERVAL 100 // 100ms between yields in main loop
#define MQTT_MAX_PACKET_WAIT 20 // Only wait up to 20ms for packet processing
#define MQTT_MAX_PACKET_WAIT 20 // Only wait up to 20ms for packet processing
#define MQTT_RECONNECT_INTERVAL 5000 // 5 seconds between reconnection attempts
// Homie convention constants
@@ -158,41 +158,9 @@ void publish_retained(MQTTClient* client, const char* topic,
}
}
// TODO: this is a modbus value only
void publish_value(MQTTClient* client, const char* device_name,
const char* property, uint16_t value) {
DEBUG_PRINT("publish_value(device_name=%s, property=%s, value=%u)\n",
device_name, property, value);
char topic[MAX_TOPIC_LENGTH];
char payload[17]; // 16 bits + null terminator
snprintf(topic, sizeof(topic), "homie/%s/%s/%s", NODE_CONFIG.id, device_name,
property);
// formta based on property type
if (strcmp(property, "state") == 0) {
for (int i = 15; i >= 0; i--) {
payload[15 - i] = '0' + ((value >> i) & 1);
}
payload[16] = '\0';
} else {
// todo:
return;
}
MQTTMessage message = {.qos = QOS1,
.retained = 1,
.payload = payload,
.payloadlen = strlen(payload)};
if (MQTTPublish(client, topic, &message) != 0) {
DEBUG_PRINT("Failed to publish to %s\n", topic);
}
}
static void publish_device_attributes(MQTTClient* client) {
char topic[MAX_TOPIC_LENGTH];
char mac_str[18];
char* ptr = nodes_list;
size_t remaining = sizeof(nodes_list);
@@ -206,6 +174,14 @@ static void publish_device_attributes(MQTTClient* client) {
snprintf(topic, sizeof(topic), "homie/%s/$state", NODE_CONFIG.id);
publish_retained(client, topic, HOMIE_STATE_READY);
// Format MAC address as XX:XX:XX:XX:XX:XX
snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
NODE_CONFIG.mac[0], NODE_CONFIG.mac[1], NODE_CONFIG.mac[2],
NODE_CONFIG.mac[3], NODE_CONFIG.mac[4], NODE_CONFIG.mac[5]);
snprintf(topic, sizeof(topic), "homie/%s/$mac", NODE_CONFIG.id);
publish_retained(client, topic, mac_str);
ptr = nodes_list;
*ptr = '\0';
// add rs485 devices