fix: yet more wiznet bugs, reconnects, led status indicators
This commit is contained in:
@@ -5,13 +5,14 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "gpio.h"
|
||||
#include "modbus_handler.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
|
||||
@@ -26,9 +27,9 @@ char nodes_list[MAX_PAYLOAD_LENGTH];
|
||||
|
||||
// Parse Homie topic format: homie/node-id/device-name/property/[set|get]
|
||||
static bool parse_homie_topic(const char* topic, size_t topic_len,
|
||||
char* device_name, size_t name_max,
|
||||
char* property, size_t prop_max,
|
||||
uint8_t* is_set) {
|
||||
char* device_name, size_t name_max,
|
||||
char* property, size_t prop_max,
|
||||
uint8_t* is_set) {
|
||||
const char* segment_start = topic;
|
||||
const char* topic_end = topic + topic_len;
|
||||
uint8_t segment = 0;
|
||||
@@ -359,16 +360,15 @@ void message_arrived(MessageData* md) {
|
||||
void mqtt_process(mqtt_state_t* state) {
|
||||
uint32_t now = millis();
|
||||
int rc;
|
||||
static uint8_t discovery_published = 0;
|
||||
|
||||
if (!state->is_connected) {
|
||||
if (now - state->last_reconnect >= MQTT_RECONNECT_INTERVAL) {
|
||||
rc = setup_mqtt_client(&state->network, &state->opts, &state->client);
|
||||
|
||||
if (rc == 0) {
|
||||
if (rc == SUCCESS) {
|
||||
state->is_connected = true;
|
||||
|
||||
if (!discovery_published) {
|
||||
if (!state->discovery_published) {
|
||||
publish_device_attributes(&state->client);
|
||||
|
||||
for (int i = 0; i < RS485_DEVICE_COUNT; i++) {
|
||||
@@ -379,7 +379,7 @@ void mqtt_process(mqtt_state_t* state) {
|
||||
// is it worth implementing?
|
||||
onewire_temp_publish_discovery(&state->client, NODE_CONFIG.id);
|
||||
|
||||
discovery_published = 1;
|
||||
state->discovery_published = 1;
|
||||
}
|
||||
|
||||
char sub_topic[MAX_TOPIC_LENGTH];
|
||||
@@ -388,17 +388,23 @@ void mqtt_process(mqtt_state_t* state) {
|
||||
|
||||
rc = subscribe_to_topic(&state->client, sub_topic, QOS1,
|
||||
message_arrived);
|
||||
if (rc != 0) {
|
||||
if (rc != SUCCESS) {
|
||||
state->is_connected = false;
|
||||
}
|
||||
}
|
||||
state->last_reconnect = now;
|
||||
}
|
||||
led_status_set(LED_STATE_BUSY);
|
||||
|
||||
} else if (now - state->last_yield >= MQTT_YIELD_INTERVAL) {
|
||||
rc = MQTTYield(&state->client, MQTT_MAX_PACKET_WAIT);
|
||||
if (rc != 0) {
|
||||
if (rc != SUCCESS) {
|
||||
DEBUG_PRINT("MQTT Yield failed with rc=%d, ping_outstanding=%d\n", rc,
|
||||
state->client.ping_outstanding);
|
||||
state->is_connected = false;
|
||||
state->discovery_published = false;
|
||||
}
|
||||
state->last_yield = now;
|
||||
led_status_set(LED_STATE_ON);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user