Add configurable DS18B20 temperature probe support

This commit is contained in:
2026-06-04 14:25:34 -06:00
parent f46e72fc7f
commit 0afd78284e
6 changed files with 490 additions and 48 deletions
@@ -474,9 +474,9 @@ void buildStatusDocument(JsonDocument& doc) {
temp["id"] = appConfig.tempSensors[i].id;
temp["name"] = appConfig.tempSensors[i].name;
temp["enabled"] = appConfig.tempSensors[i].enabled;
temp["online"] = tempOnline[i];
if (tempOnline[i]) {
temp["temperature_f"] = tempValues[i];
temp["online"] = sensors.online[i];
if (sensors.online[i]) {
temp["temperature_f"] = sensors.tempsF[i];
} else {
temp["temperature_f"] = nullptr;
}
@@ -912,6 +912,82 @@ void handleUartMessage(const String& line) {
return;
}
if (strcmp(type, "scan_temps") == 0) {
scanTempSensors();
DynamicJsonDocument response(2048);
response["type"] = "temp_scan_response";
response["ok"] = true;
JsonArray devices = response.createNestedArray("devices");
int count = getTempScanResultCount();
for (int i = 0; i < count; i++) {
JsonObject device = devices.createNestedObject();
device["index"] = i + 1;
device["address"] = getTempScanResultAddress(i);
}
serializeJson(response, DashboardSerial);
DashboardSerial.println();
return;
}
if (strcmp(type, "assign_temp") == 0) {
String id = doc["id"] | "";
int slot = doc["slot"] | 0;
int index = doc["index"] | 0;
int configIndex = -1;
if (id.length() > 0) {
configIndex = findTempConfigIndexForUart(id);
} else if (slot >= 1 && slot <= MAX_TEMP_SENSORS) {
configIndex = slot - 1;
}
if (configIndex < 0) {
sendError(DashboardSerial, "unknown_temp_sensor");
return;
}
if (!assignTempSensorByIndex(configIndex, index - 1)) {
sendError(DashboardSerial, "invalid_temp_selection");
return;
}
saveConfig();
sendConfigResponse(DashboardSerial);
return;
}
if (strcmp(type, "clear_temp") == 0) {
String id = doc["id"] | "";
int slot = doc["slot"] | 0;
int configIndex = -1;
if (id.length() > 0) {
configIndex = findTempConfigIndexForUart(id);
} else if (slot >= 1 && slot <= MAX_TEMP_SENSORS) {
configIndex = slot - 1;
}
if (configIndex < 0) {
sendError(DashboardSerial, "unknown_temp_sensor");
return;
}
if (!clearTempSensor(configIndex)) {
sendError(DashboardSerial, "unknown_temp_sensor");
return;
}
saveConfig();
sendConfigResponse(DashboardSerial);
return;
}
if (strcmp(type, "save_config") == 0) {
saveConfig();
sendConfigResponse(DashboardSerial);
@@ -1191,6 +1267,47 @@ void handleDebugSerial() {
}
}
if (command == "scan temps") {
scanTempSensors();
printSensorAddresses();
return;
}
if (command.startsWith("assign temp ")) {
String rest = command.substring(12);
int space = rest.indexOf(' ');
if (space < 0) {
Serial.println("Invalid command. Use: assign temp <slot> <number>");
return;
}
int slot = rest.substring(0, space).toInt();
int index = rest.substring(space + 1).toInt();
if (!assignTempSensorByIndex(slot - 1, index - 1)) {
Serial.println("Invalid temp slot or sensor number.");
return;
}
saveConfig();
Serial.println("OK temp sensor assigned");
return;
}
if (command.startsWith("clear temp ")) {
int slot = command.substring(11).toInt();
if (!clearTempSensor(slot - 1)) {
Serial.println("Invalid temp slot.");
return;
}
saveConfig();
Serial.println("OK temp sensor cleared");
return;
}
if (command.startsWith("tempname ")) {
int firstSpace = command.indexOf(' ');
int secondSpace = command.indexOf(' ', firstSpace + 1);
@@ -1524,6 +1641,102 @@ void handleWifiClear() {
sendWifiConfigJson();
}
void sendTempScanJson() {
DynamicJsonDocument doc(2048);
doc["type"] = "temp_scan_response";
doc["ok"] = true;
JsonArray devices = doc.createNestedArray("devices");
int count = getTempScanResultCount();
for (int i = 0; i < count; i++) {
JsonObject device = devices.createNestedObject();
device["index"] = i + 1;
device["address"] = getTempScanResultAddress(i);
}
String output;
serializeJson(doc, output);
server.send(200, "application/json", output);
}
void handleTempScan() {
scanTempSensors();
sendTempScanJson();
}
void handleTempAssign() {
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, server.arg("plain"));
if (error) {
server.send(400, "application/json", "{\"ok\":false,\"error\":\"invalid_json\"}");
return;
}
String id = doc["id"] | "";
int slot = doc["slot"] | 0;
int index = doc["index"] | 0;
int configIndex = -1;
if (id.length() > 0) {
configIndex = findTempConfigIndexForUart(id);
} else if (slot >= 1 && slot <= MAX_TEMP_SENSORS) {
configIndex = slot - 1;
}
if (configIndex < 0) {
server.send(400, "application/json", "{\"ok\":false,\"error\":\"unknown_temp_sensor\"}");
return;
}
if (!assignTempSensorByIndex(configIndex, index - 1)) {
server.send(400, "application/json", "{\"ok\":false,\"error\":\"invalid_temp_selection\"}");
return;
}
saveConfig();
sendConfigJson();
}
void handleTempClear() {
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, server.arg("plain"));
if (error) {
server.send(400, "application/json", "{\"ok\":false,\"error\":\"invalid_json\"}");
return;
}
String id = doc["id"] | "";
int slot = doc["slot"] | 0;
int configIndex = -1;
if (id.length() > 0) {
configIndex = findTempConfigIndexForUart(id);
} else if (slot >= 1 && slot <= MAX_TEMP_SENSORS) {
configIndex = slot - 1;
}
if (configIndex < 0) {
server.send(400, "application/json", "{\"ok\":false,\"error\":\"unknown_temp_sensor\"}");
return;
}
if (!clearTempSensor(configIndex)) {
server.send(400, "application/json", "{\"ok\":false,\"error\":\"unknown_temp_sensor\"}");
return;
}
saveConfig();
sendConfigJson();
}
void handleSaveConfig() {
saveConfig();
sendConfigJson();