Add DS1820 to JSON sensor output (#1809)

* first try for DS1820 sensors

* fix windows build

* Fix wrong comment format

* Fix code

* fix missing includes

* fix missing defines (only check if driver is included)

* Add DS1820 to sensors in "DRV_IsSensor
Fix string might be not empty

* fix output for long string in case of many DS1820 sensors
This commit is contained in:
MaxineMuster 2025-09-27 09:15:20 +02:00 committed by GitHub
parent 744d5bc40c
commit c1d258fb60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 73 additions and 4 deletions

View File

@ -58,7 +58,7 @@ jobs:
build2:
name: Build Simulator
needs: refs
runs-on: windows-latest
runs-on: windows-2022
steps:
- name: Checkout repository
@ -620,4 +620,4 @@ jobs:
echo ${{ steps.semantic.outputs.new_release_version }}
echo ${{ steps.semantic.outputs.new_release_major_version }}
echo ${{ steps.semantic.outputs.new_release_minor_version }}
echo ${{ steps.semantic.outputs.new_release_patch_version }}
echo ${{ steps.semantic.outputs.new_release_patch_version }}

View File

@ -736,6 +736,41 @@ void DS1820_full_driver_Init()
};
/*
printer(request, "\"DS1820\":");
// following check will clear NaN values
printer(request, "{");
printer(request, "\"Temperature\": %.1f,", chan_val1);
// close ENERGY block
printer(request, "},");
*/
static char *jsonSensor_reststr = NULL;
char *DS1820_full_jsonSensors(){
if (ds18_count <= 0 ) return NULL;
if (jsonSensor_reststr!=NULL) free(jsonSensor_reststr);
// {"DS1820_<name>":{"Temperature": <temp>},
// {"DS1820_<name - DS18B20namel>":{"Temperature": <temp -127,00>},
// 123456789 123456789012345678 1234567890
// length of str: 10 + DS18B20namel + 18 + 10 --> 40 + DS18B20namel
int size = (40 + DS18B20namel) * ds18_count;
char *str = (char *)malloc(size * sizeof(char));
if (str == NULL) {
return NULL; // string allocation failed
}
str[0] = 0;
for (int i=0; i < ds18_count; i++) {
char tmp[50 + DS18B20namel];
sprintf(tmp, "\"DS1820_%s\":{\"Temperature\": %.1f},",ds18b20devices.name[i],ds18b20devices.lasttemp[i]);
strncat(str, tmp, size - strlen(str) - 1); // Concatenate to the main string
}
jsonSensor_reststr = str;
return jsonSensor_reststr;
}
void DS1820_full_AppendInformationToHTTPIndexPage(http_request_t* request, int bPreState)
{
if (bPreState){

View File

@ -24,4 +24,4 @@ float ds18b20_getTempC(const uint8_t *deviceAddress);
bool isConversionComplete();
void reset_search();
bool search(uint8_t *newAddr, bool search_mode, int Pin);
char *DS1820_full_jsonSensors();

View File

@ -852,7 +852,7 @@ bool DRV_IsMeasuringBattery() {
bool DRV_IsSensor() {
#ifndef OBK_DISABLE_ALL_DRIVERS
return DRV_IsRunning("SHT3X") || DRV_IsRunning("CHT83XX") || DRV_IsRunning("SGP") || DRV_IsRunning("AHT2X");
return DRV_IsRunning("SHT3X") || DRV_IsRunning("CHT83XX") || DRV_IsRunning("SGP") || DRV_IsRunning("AHT2X") || DRV_IsRunning("DS1820") || DRV_IsRunning("DS1820_full");
#else
return false;
#endif

View File

@ -20,6 +20,9 @@
#include "../driver/drv_ntp.h"
#include "../driver/drv_local.h"
#include "../driver/drv_bl_shared.h"
#include "../driver/drv_ds1820_simple.h"
#include "../driver/drv_ds1820_full.h"
#if ENABLE_TASMOTA_JSON
@ -278,6 +281,36 @@ static int http_tasmota_json_SENSOR(void* request, jsonCb_t printer) {
// close ENERGY block
printer(request, "},");
}
#if (ENABLE_DRIVER_DS1820)
if (DRV_IsRunning("DS1820")) { //DS1820_simple.c with one sensor
g_pin_1 = PIN_FindPinIndexForRole(IOR_DS1820_IO, g_pin_1);
channel_1 = g_cfg.pins.channels[g_pin_1];
chan_val1 = CHANNEL_GetFloat(channel_1) / 100.0f;
// writer header
printer(request, "\"DS1820\":");
// following check will clear NaN values
printer(request, "{");
printer(request, "\"Temperature\": %.1f", chan_val1);
// close ENERGY block
printer(request, "},");
}
#endif
#if (ENABLE_DRIVER_DS1820_FULL)
if (DRV_IsRunning("DS1820_full")) { //DS1820_full.c with possibly multiple sensors
char *str = DS1820_full_jsonSensors();
int toprint = strlen(str);
while (*str && toprint > 250) { // string can be long, longer than request, this would break output if not split
char t = str[250];
str[250]=0;
printer(request, str);
str[250]=t;
str+=250;
toprint -= 250;
}
printer(request, str);
}
#endif
if (DRV_IsRunning("CHT83XX")) {
g_pin_1 = PIN_FindPinIndexForRole(IOR_CHT83XX_DAT, g_pin_1);
channel_1 = g_cfg.pins.channels[g_pin_1];
@ -295,6 +328,7 @@ static int http_tasmota_json_SENSOR(void* request, jsonCb_t printer) {
// close ENERGY block
printer(request, "},");
}
for (int i = 0; i < PLATFORM_GPIO_MAX; i++) {
int role = PIN_GetPinRoleForPinIndex(i);
if (role != IOR_DHT11 && role != IOR_DHT12 && role != IOR_DHT21 && role != IOR_DHT22)