diff --git a/gpio.c b/gpio.c index 77a6454..25d698c 100644 --- a/gpio.c +++ b/gpio.c @@ -8,7 +8,7 @@ #define LED_BLINK_SLOW 1000 // Warning blink interval (ms) #define LED_BLINK_FAST 500 // Error blink interval (ms) #define LED_BREATH_PERIOD 2000 // Breathing effect period (ms) -#define STATE_STABILITY 500 // Minimum time before state change (ms) +#define STATE_STABILITY 500 // Minimum time before state change (ms) #define LED_G (1 << 4) #define LED_B (1 << 3) diff --git a/onewire_temp.c b/onewire_temp.c index 255f1ca..0501eb7 100644 --- a/onewire_temp.c +++ b/onewire_temp.c @@ -94,7 +94,7 @@ static float convert_temperature(const uint8_t* data, uint8_t family_code) { } static bool start_conversion(onewire_sensor_t* sensor) { - if (!sensor->state != ONEWIRE_STATE_READY) { + if (!sensor->valid || !sensor->state != ONEWIRE_STATE_READY) { return false; } @@ -148,7 +148,9 @@ void onewire_temp_process(void) { sensor->error_count++; if (sensor->error_count >= ONEWIRE_MAX_RETRIES) { sensor->valid = false; - DEBUG_PRINT("sensor %d marked temporarily invalid after %d retries\n", i, ONEWIRE_MAX_RETRIES); + DEBUG_PRINT( + "sensor %d marked temporarily invalid after %d retries\n", i, + ONEWIRE_MAX_RETRIES); } } } @@ -165,7 +167,9 @@ void onewire_temp_process(void) { sensor->error_count++; if (sensor->error_count >= ONEWIRE_MAX_RETRIES) { sensor->valid = false; - DEBUG_PRINT("sensor %d marked temporarily invalid after %d retries\n", i, ONEWIRE_MAX_RETRIES); + DEBUG_PRINT( + "sensor %d marked temporarily invalid after %d retries\n", i, + ONEWIRE_MAX_RETRIES); } } else { sensor->valid = true; // re-enable @@ -214,104 +218,113 @@ void onewire_temp_set_parallel(bool enable) { ow_sys.parallel_mode = enable; } // MQTT void onewire_temp_publish_discovery(MQTTClient* client, const char* node_id) { - char topic[MAX_TOPIC_LENGTH]; - char sensor_name[32]; - uint8_t sensor_count = onewire_temp_count(); - - // append to node list - size_t current_len = strlen(nodes_list); - char* ptr = nodes_list + current_len; - size_t remaining = sizeof(nodes_list) - current_len; - - for (uint8_t i = 0; i < sensor_count && remaining > 1; i++) { - if (!onewire_temp_valid(i)) { - continue; - } + char topic[MAX_TOPIC_LENGTH]; + char sensor_name[32]; + uint8_t sensor_count = onewire_temp_count(); - // , if not 1st - if (current_len > 0 && remaining > 1) { - *ptr++ = ','; - remaining--; - current_len++; - } + // append to node list + size_t current_len = strlen(nodes_list); + char* ptr = nodes_list + current_len; + size_t remaining = sizeof(nodes_list) - current_len; - const uint8_t* addr = onewire_temp_address(i); - int written = snprintf(sensor_name, sizeof(sensor_name), - "temp_%02x%02x%02x%02x%02x%02x%02x%02x", - addr[0], addr[1], addr[2], addr[3], - addr[4], addr[5], addr[6], addr[7]); - - if (written < 0 || (size_t)written >= remaining) { - break; - } - - memcpy(ptr, sensor_name, written); - ptr += written; - remaining -= written; - current_len += written; + for (uint8_t i = 0; i < sensor_count && remaining > 1; i++) { + if (!onewire_temp_valid(i)) { + continue; } - *ptr = '\0'; - // pub node list - snprintf(topic, sizeof(topic), "homie/%s/$nodes", node_id); - publish_retained(client, topic, nodes_list); + // , if not 1st + if (current_len > 0 && remaining > 1) { + *ptr++ = ','; + remaining--; + current_len++; + } - for (uint8_t i = 0; i < sensor_count; i++) { - if (!onewire_temp_valid(i)) { - continue; - } - - const uint8_t* addr = onewire_temp_address(i); + const uint8_t* addr = onewire_temp_address(i); + int written = snprintf(sensor_name, sizeof(sensor_name), - "temp_%02x%02x%02x%02x%02x%02x%02x%02x", - addr[0], addr[1], addr[2], addr[3], - addr[4], addr[5], addr[6], addr[7]); + "temp_%02x%02x%02x%02x%02x%02x%02x%02x", addr[0], addr[1], + addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); - snprintf(topic, sizeof(topic), "homie/%s/%s/$name", node_id, sensor_name); - char display_name[48]; - snprintf(display_name, sizeof(display_name), - "Temperature Sensor %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", - addr[0], addr[1], addr[2], addr[3], - addr[4], addr[5], addr[6], addr[7]); - publish_retained(client, topic, display_name); - - snprintf(topic, sizeof(topic), "homie/%s/%s/$properties", node_id, sensor_name); - publish_retained(client, topic, "temperature,address,status,error_count"); - - // temperature properties - snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$name", node_id, sensor_name); - publish_retained(client, topic, "Temperature"); - snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$datatype", node_id, sensor_name); - publish_retained(client, topic, "float"); - snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$unit", node_id, sensor_name); - publish_retained(client, topic, "°C"); - - // address properties - snprintf(topic, sizeof(topic), "homie/%s/%s/address/$name", node_id, sensor_name); - publish_retained(client, topic, "ROM Address"); - snprintf(topic, sizeof(topic), "homie/%s/%s/address/$datatype", node_id, sensor_name); - publish_retained(client, topic, "string"); - - char addr_str[24]; - snprintf(addr_str, sizeof(addr_str), - "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", - addr[0], addr[1], addr[2], addr[3], - addr[4], addr[5], addr[6], addr[7]); - snprintf(topic, sizeof(topic), "homie/%s/%s/address", node_id, sensor_name); - publish_retained(client, topic, addr_str); - - snprintf(topic, sizeof(topic), "homie/%s/%s/status/$name", node_id, sensor_name); - publish_retained(client, topic, "Sensor Status"); - snprintf(topic, sizeof(topic), "homie/%s/%s/status/$datatype", node_id, sensor_name); - publish_retained(client, topic, "enum"); - snprintf(topic, sizeof(topic), "homie/%s/%s/status/$format", node_id, sensor_name); - publish_retained(client, topic, "valid,invalid"); - - snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$name", node_id, sensor_name); - publish_retained(client, topic, "Error Count"); - snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$datatype", node_id, sensor_name); - publish_retained(client, topic, "integer"); + if (written < 0 || (size_t)written >= remaining) { + break; } + + memcpy(ptr, sensor_name, written); + ptr += written; + remaining -= written; + current_len += written; + } + *ptr = '\0'; + + // pub node list + snprintf(topic, sizeof(topic), "homie/%s/$nodes", node_id); + publish_retained(client, topic, nodes_list); + + for (uint8_t i = 0; i < sensor_count; i++) { + if (!onewire_temp_valid(i)) { + continue; + } + + const uint8_t* addr = onewire_temp_address(i); + snprintf(sensor_name, sizeof(sensor_name), + "temp_%02x%02x%02x%02x%02x%02x%02x%02x", addr[0], addr[1], addr[2], + addr[3], addr[4], addr[5], addr[6], addr[7]); + + snprintf(topic, sizeof(topic), "homie/%s/%s/$name", node_id, sensor_name); + char display_name[48]; + snprintf(display_name, sizeof(display_name), + "Temperature Sensor %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], + addr[7]); + publish_retained(client, topic, display_name); + + snprintf(topic, sizeof(topic), "homie/%s/%s/$properties", node_id, + sensor_name); + publish_retained(client, topic, "temperature,address,status,error_count"); + + // temperature properties + snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$name", node_id, + sensor_name); + publish_retained(client, topic, "Temperature"); + snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$datatype", node_id, + sensor_name); + publish_retained(client, topic, "float"); + snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$unit", node_id, + sensor_name); + publish_retained(client, topic, "°C"); + + // address properties + snprintf(topic, sizeof(topic), "homie/%s/%s/address/$name", node_id, + sensor_name); + publish_retained(client, topic, "ROM Address"); + snprintf(topic, sizeof(topic), "homie/%s/%s/address/$datatype", node_id, + sensor_name); + publish_retained(client, topic, "string"); + + char addr_str[24]; + snprintf(addr_str, sizeof(addr_str), + "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], + addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); + snprintf(topic, sizeof(topic), "homie/%s/%s/address", node_id, sensor_name); + publish_retained(client, topic, addr_str); + + snprintf(topic, sizeof(topic), "homie/%s/%s/status/$name", node_id, + sensor_name); + publish_retained(client, topic, "Sensor Status"); + snprintf(topic, sizeof(topic), "homie/%s/%s/status/$datatype", node_id, + sensor_name); + publish_retained(client, topic, "enum"); + snprintf(topic, sizeof(topic), "homie/%s/%s/status/$format", node_id, + sensor_name); + publish_retained(client, topic, "valid,invalid"); + + snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$name", node_id, + sensor_name); + publish_retained(client, topic, "Error Count"); + snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$datatype", node_id, + sensor_name); + publish_retained(client, topic, "integer"); + } } // MQTT @@ -326,9 +339,8 @@ void onewire_temp_publish_values(MQTTClient* client, const char* node_id) { char base_topic[MAX_TOPIC_LENGTH]; snprintf(base_topic, sizeof(base_topic), - "homie/%s/temp_%02x%02x%02x%02x%02x%02x%02x%02x", - node_id, addr[0], addr[1], addr[2], addr[3], - addr[4], addr[5], addr[6], addr[7]); + "homie/%s/temp_%02x%02x%02x%02x%02x%02x%02x%02x", node_id, addr[0], + addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); // publish status snprintf(topic, sizeof(topic), "%s/status", base_topic);