From 61aeea9132cf6f6a1dadab1f2c750231b1405de5 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:01:19 +0200 Subject: [PATCH] Add ESP8266 support for one-wire M1601 temperature sensor on DS18x20 GPIO (#21376) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + .../tasmota_xsns_sensor/xsns_05_ds18x20.ino | 82 ++++++++++++++----- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 947a491af..b726578e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ## [14.2.0.5] ### Added - ESP8266 support for I2C CLK on GPIO16 (#22199) +- ESP8266 support for one-wire M1601 temperature sensor on DS18x20 GPIO (#21376) ### Breaking Changed diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 4ba96530a..62f9a8fa3 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -123,6 +123,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm ### Added - Command ``SetOption69 1`` to enable Serial Bridge inverted Receive [#22000](https://github.com/arendst/Tasmota/issues/22000) - HX711 optional calibration precision option on command ``Sensor34 2 `` where `` is 1 to 20 [#13983](https://github.com/arendst/Tasmota/issues/13983) +- ESP8266 support for one-wire M1601 temperature sensor on DS18x20 GPIO [#21376](https://github.com/arendst/Tasmota/issues/21376) - ESP8266 support for I2C CLK on GPIO16 [#22199](https://github.com/arendst/Tasmota/issues/22199) - Support for I2C M5Unit (Mini)Scales using HX711 driver - Support for RX8010 RTC as used in IOTTIMER [#21376](https://github.com/arendst/Tasmota/issues/21376) diff --git a/tasmota/tasmota_xsns_sensor/xsns_05_ds18x20.ino b/tasmota/tasmota_xsns_sensor/xsns_05_ds18x20.ino index ccc489a62..36fb15dea 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_05_ds18x20.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_05_ds18x20.ino @@ -35,10 +35,11 @@ * Result in JSON: "Outside1":{"Id":"000003287CD8","Temperature":26.3} (example with N=Outside1) */ -#define DS18S20_CHIPID 0x10 // +/-0.5C 9-bit -#define DS1822_CHIPID 0x22 // +/-2C 12-bit -#define DS18B20_CHIPID 0x28 // +/-0.5C 12-bit -#define MAX31850_CHIPID 0x3B // +/-0.25C 14-bit +#define DS18S20_CHIPID 0x10 // +/-0.5C 9-bit +#define DS1822_CHIPID 0x22 // +/-2C 12-bit +#define DS18B20_CHIPID 0x28 // +/-0.5C 12-bit +#define M1601_CHIPID 0x28 +1 // +/-0.1C 16-bit (M1601B = +/-0.5C 16-bit) +#define MAX31850_CHIPID 0x3B // +/-0.25C 14-bit #define W1_SKIP_ROM 0xCC #define W1_CONVERT_TEMP 0x44 @@ -46,21 +47,24 @@ #define W1_WRITE_SCRATCHPAD 0x4E #define W1_READ_SCRATCHPAD 0xBE -#ifndef DS18X20_MAX_SENSORS // DS18X20_MAX_SENSORS fallback to 8 if not defined in user_config_override.h +#ifndef DS18X20_MAX_SENSORS // DS18X20_MAX_SENSORS fallback to 8 if not defined in user_config_override.h #define DS18X20_MAX_SENSORS 8 #endif #define DS18X20_ALIAS_LEN 17 -const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850"; +//#define DS18X20_DEBUG -uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID }; +const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850|M1601"; + +uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID, M1601_CHIPID }; struct { float temperature; float temp_sum; uint16_t numread; uint8_t address[8]; + uint8_t chip_id; uint8_t index; uint8_t valid; int8_t pins_id; @@ -228,6 +232,9 @@ uint8_t OneWireSearch(uint8_t *newAddr) { onewire_last_discrepancy = 0; onewire_last_device_flag = false; onewire_last_family_discrepancy = 0; +#ifdef DS18X20_DEBUG + AddLog(LOG_LEVEL_DEBUG, PSTR("DSB: OneWireReset fail")); +#endif // DS18X20_DEBUG return false; } OneWireWrite(W1_SEARCH_ROM); @@ -278,6 +285,9 @@ uint8_t OneWireSearch(uint8_t *newAddr) { } search_result = true; } +#ifdef DS18X20_DEBUG + AddLog(LOG_LEVEL_DEBUG, PSTR("DSB: OneWireSearch result %d, bits %d, %8_H"), search_result, id_bit_number, onewire_rom_id); +#endif // DS18X20_DEBUG } if (!search_result || !onewire_rom_id[0]) { onewire_last_discrepancy = 0; @@ -291,12 +301,11 @@ uint8_t OneWireSearch(uint8_t *newAddr) { return search_result; } -bool OneWireCrc8(uint8_t *addr) { +bool OneWireCrc8(uint8_t *addr, uint8_t len) { uint8_t crc = 0; - uint8_t len = 8; while (len--) { - uint8_t inbyte = *addr++; // from 0 to 7 + uint8_t inbyte = *addr++; for (uint32_t i = 8; i; i--) { uint8_t mix = (crc ^ inbyte) & 0x01; crc >>= 1; @@ -306,7 +315,7 @@ bool OneWireCrc8(uint8_t *addr) { inbyte >>= 1; } } - return (crc == *addr); // addr 8 + return (crc == *addr); } /********************************************************************************************/ @@ -347,24 +356,47 @@ void Ds18x20Init(void) { while (DS18X20Data.sensors < DS18X20_MAX_SENSORS) { if (!OneWireSearch(ds18x20_sensor[DS18X20Data.sensors].address)) { +#ifdef DS18X20_DEBUG + AddLog(LOG_LEVEL_DEBUG, PSTR("DSB: OneWireSearch fail")); +#endif // DS18X20_DEBUG break; } - if (OneWireCrc8(ds18x20_sensor[DS18X20Data.sensors].address) && - ((ds18x20_sensor[DS18X20Data.sensors].address[0] == DS18S20_CHIPID) || - (ds18x20_sensor[DS18X20Data.sensors].address[0] == DS1822_CHIPID) || - (ds18x20_sensor[DS18X20Data.sensors].address[0] == DS18B20_CHIPID) || - (ds18x20_sensor[DS18X20Data.sensors].address[0] == MAX31850_CHIPID))) { + + uint32_t chip_id = ds18x20_sensor[DS18X20Data.sensors].address[0]; + bool crc = OneWireCrc8(ds18x20_sensor[DS18X20Data.sensors].address, 7); + if (!crc) { + // Look for M1601 which has same chip_id as DS18B20 but has wrong CRC over 64-bit ROM code + // DS18B20 address 284CC48E04000079 + // M1601 address 2894020000000000 + if ((ds18x20_sensor[DS18X20Data.sensors].address[0] == DS18B20_CHIPID) && + (ds18x20_sensor[DS18X20Data.sensors].address[7] == 0)) { + chip_id = M1601_CHIPID; // Need different chip_id as different temperature calculation + crc = true; + } + } + if (crc && + ((chip_id == DS18S20_CHIPID) || + (chip_id == DS1822_CHIPID) || + (chip_id == DS18B20_CHIPID) || + (chip_id == M1601_CHIPID) || + (chip_id == MAX31850_CHIPID))) { ds18x20_sensor[DS18X20Data.sensors].index = DS18X20Data.sensors; - ids[DS18X20Data.sensors] = ds18x20_sensor[DS18X20Data.sensors].address[0]; // Chip id + ids[DS18X20Data.sensors] = chip_id; // Chip id for (uint32_t j = 6; j > 0; j--) { ids[DS18X20Data.sensors] = ids[DS18X20Data.sensors] << 8 | ds18x20_sensor[DS18X20Data.sensors].address[j]; } #ifdef DS18x20_USE_ID_ALIAS ds18x20_sensor[DS18X20Data.sensors].alias[0] = '0'; #endif + ds18x20_sensor[DS18X20Data.sensors].chip_id = chip_id; ds18x20_sensor[DS18X20Data.sensors].pins_id = pins; DS18X20Data.sensors++; } +#ifdef DS18X20_DEBUG + else { + AddLog(LOG_LEVEL_DEBUG, PSTR("DSB: Ds18x20Init CRC fail")); + } +#endif // DS18X20_DEBUG } } @@ -415,8 +447,11 @@ bool Ds18x20Read(uint8_t sensor) { for (uint32_t i = 0; i < 9; i++) { data[i] = OneWireRead(); } - if (OneWireCrc8(data)) { - switch(ds18x20_sensor[index].address[0]) { +#ifdef DS18X20_DEBUG + AddLog(LOG_LEVEL_DEBUG, PSTR("DSB: OneWireRead ChipId 0x%02X, %9_H"), ds18x20_sensor[index].chip_id, data); +#endif // DS18X20_DEBUG + if (OneWireCrc8(data, 8)) { + switch(ds18x20_sensor[index].chip_id) { case DS18S20_CHIPID: { int16_t tempS = (((data[1] << 8) | (data[0] & 0xFE)) << 3) | ((0x10 - data[6]) & 0x0F); temperature = ConvertTemp(tempS * 0.0625f - 0.250f); @@ -424,6 +459,7 @@ bool Ds18x20Read(uint8_t sensor) { } case DS1822_CHIPID: case DS18B20_CHIPID: { + // 71 01 4B 46 7F FF 0F 10 56 if (data[4] != 0x7F) { data[4] = 0x7F; // Set resolution to 12-bit OneWireReset(); @@ -446,6 +482,12 @@ bool Ds18x20Read(uint8_t sensor) { temperature = ConvertTemp(sign * temp12 * 0.0625f); // Divide by 16 break; } + case M1601_CHIPID: { + // 96 F1 00 80 55 05 02 09 86 + float temp = (int16_t)(data[1] << 8) + data[0]; + temperature = ConvertTemp(40 + (temp / 256)); + break; + } case MAX31850_CHIPID: { int16_t temp14 = (data[1] << 8) + (data[0] & 0xFC); temperature = ConvertTemp(temp14 * 0.0625f); // Divide by 16 @@ -472,7 +514,7 @@ void Ds18x20Name(uint8_t sensor) { uint32_t index = sizeof(ds18x20_chipids); while (--index) { - if (ds18x20_sensor[sensor_index].address[0] == ds18x20_chipids[index]) { + if (ds18x20_sensor[sensor_index].chip_id == ds18x20_chipids[index]) { break; } }