fix: onewire
This commit is contained in:
@@ -94,7 +94,7 @@ static float convert_temperature(const uint8_t* data, uint8_t family_code) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool start_conversion(onewire_sensor_t* sensor) {
|
static bool start_conversion(onewire_sensor_t* sensor) {
|
||||||
if (!sensor->state != ONEWIRE_STATE_READY) {
|
if (!sensor->valid || !sensor->state != ONEWIRE_STATE_READY) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +148,9 @@ void onewire_temp_process(void) {
|
|||||||
sensor->error_count++;
|
sensor->error_count++;
|
||||||
if (sensor->error_count >= ONEWIRE_MAX_RETRIES) {
|
if (sensor->error_count >= ONEWIRE_MAX_RETRIES) {
|
||||||
sensor->valid = false;
|
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++;
|
sensor->error_count++;
|
||||||
if (sensor->error_count >= ONEWIRE_MAX_RETRIES) {
|
if (sensor->error_count >= ONEWIRE_MAX_RETRIES) {
|
||||||
sensor->valid = false;
|
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 {
|
} else {
|
||||||
sensor->valid = true; // re-enable
|
sensor->valid = true; // re-enable
|
||||||
@@ -236,10 +240,10 @@ void onewire_temp_publish_discovery(MQTTClient* client, const char* node_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t* addr = onewire_temp_address(i);
|
const uint8_t* addr = onewire_temp_address(i);
|
||||||
int written = snprintf(sensor_name, sizeof(sensor_name),
|
int written =
|
||||||
"temp_%02x%02x%02x%02x%02x%02x%02x%02x",
|
snprintf(sensor_name, sizeof(sensor_name),
|
||||||
addr[0], addr[1], addr[2], addr[3],
|
"temp_%02x%02x%02x%02x%02x%02x%02x%02x", addr[0], addr[1],
|
||||||
addr[4], addr[5], addr[6], addr[7]);
|
addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
|
||||||
|
|
||||||
if (written < 0 || (size_t)written >= remaining) {
|
if (written < 0 || (size_t)written >= remaining) {
|
||||||
break;
|
break;
|
||||||
@@ -263,53 +267,62 @@ void onewire_temp_publish_discovery(MQTTClient* client, const char* node_id) {
|
|||||||
|
|
||||||
const uint8_t* addr = onewire_temp_address(i);
|
const uint8_t* addr = onewire_temp_address(i);
|
||||||
snprintf(sensor_name, sizeof(sensor_name),
|
snprintf(sensor_name, sizeof(sensor_name),
|
||||||
"temp_%02x%02x%02x%02x%02x%02x%02x%02x",
|
"temp_%02x%02x%02x%02x%02x%02x%02x%02x", addr[0], addr[1], addr[2],
|
||||||
addr[0], addr[1], addr[2], addr[3],
|
addr[3], addr[4], addr[5], addr[6], addr[7]);
|
||||||
addr[4], addr[5], addr[6], addr[7]);
|
|
||||||
|
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/$name", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/$name", node_id, sensor_name);
|
||||||
char display_name[48];
|
char display_name[48];
|
||||||
snprintf(display_name, sizeof(display_name),
|
snprintf(display_name, sizeof(display_name),
|
||||||
"Temperature Sensor %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
|
"Temperature Sensor %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
|
||||||
addr[0], addr[1], addr[2], addr[3],
|
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6],
|
||||||
addr[4], addr[5], addr[6], addr[7]);
|
addr[7]);
|
||||||
publish_retained(client, topic, display_name);
|
publish_retained(client, topic, display_name);
|
||||||
|
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/$properties", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/$properties", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "temperature,address,status,error_count");
|
publish_retained(client, topic, "temperature,address,status,error_count");
|
||||||
|
|
||||||
// temperature properties
|
// temperature properties
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$name", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$name", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "Temperature");
|
publish_retained(client, topic, "Temperature");
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$datatype", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$datatype", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "float");
|
publish_retained(client, topic, "float");
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$unit", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$unit", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "°C");
|
publish_retained(client, topic, "°C");
|
||||||
|
|
||||||
// address properties
|
// address properties
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/address/$name", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/address/$name", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "ROM Address");
|
publish_retained(client, topic, "ROM Address");
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/address/$datatype", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/address/$datatype", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "string");
|
publish_retained(client, topic, "string");
|
||||||
|
|
||||||
char addr_str[24];
|
char addr_str[24];
|
||||||
snprintf(addr_str, sizeof(addr_str),
|
snprintf(addr_str, sizeof(addr_str),
|
||||||
"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
|
"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1],
|
||||||
addr[0], addr[1], addr[2], addr[3],
|
addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
|
||||||
addr[4], addr[5], addr[6], addr[7]);
|
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/address", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/address", node_id, sensor_name);
|
||||||
publish_retained(client, topic, addr_str);
|
publish_retained(client, topic, addr_str);
|
||||||
|
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/status/$name", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/status/$name", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "Sensor Status");
|
publish_retained(client, topic, "Sensor Status");
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/status/$datatype", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/status/$datatype", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "enum");
|
publish_retained(client, topic, "enum");
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/status/$format", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/status/$format", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "valid,invalid");
|
publish_retained(client, topic, "valid,invalid");
|
||||||
|
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$name", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$name", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "Error Count");
|
publish_retained(client, topic, "Error Count");
|
||||||
snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$datatype", node_id, sensor_name);
|
snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$datatype", node_id,
|
||||||
|
sensor_name);
|
||||||
publish_retained(client, topic, "integer");
|
publish_retained(client, topic, "integer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -326,9 +339,8 @@ void onewire_temp_publish_values(MQTTClient* client, const char* node_id) {
|
|||||||
|
|
||||||
char base_topic[MAX_TOPIC_LENGTH];
|
char base_topic[MAX_TOPIC_LENGTH];
|
||||||
snprintf(base_topic, sizeof(base_topic),
|
snprintf(base_topic, sizeof(base_topic),
|
||||||
"homie/%s/temp_%02x%02x%02x%02x%02x%02x%02x%02x",
|
"homie/%s/temp_%02x%02x%02x%02x%02x%02x%02x%02x", node_id, addr[0],
|
||||||
node_id, addr[0], addr[1], addr[2], addr[3],
|
addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
|
||||||
addr[4], addr[5], addr[6], addr[7]);
|
|
||||||
|
|
||||||
// publish status
|
// publish status
|
||||||
snprintf(topic, sizeof(topic), "%s/status", base_topic);
|
snprintf(topic, sizeof(topic), "%s/status", base_topic);
|
||||||
|
|||||||
Reference in New Issue
Block a user