From d7dae7a7fa4e82a0c80e5d21fd7304b88ada0875 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 26 Jun 2021 19:38:31 +0300 Subject: [PATCH 1/4] add optional support for am2320 temperature/humidity sensor --- tasmota/my_user_config.h | 2 +- tasmota/xsns_88_am2320.ino | 225 +++++++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 tasmota/xsns_88_am2320.ino diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index f70840ce5..e731d4898 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -632,7 +632,7 @@ // #define USE_SEESAW_SOIL // [I2cDriver56] Enable Capacitice Soil Moisture & Temperature Sensor (I2C addresses 0x36 - 0x39) (+1k3 code) // #define USE_MPU_ACCEL // [I2cDriver58] Enable MPU6886/MPU9250 - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x68) (+2k code) // #define USE_BM8563 // [I2cDriver59] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code) - +// #define USE_AM2320 // [I2cDriver88] Enable AM2320 temperature and humidity Sensor (I2C address 0x5C)() // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 #define USE_DISPLAY_LCD // [DisplayModel 1] [I2cDriver3] Enable Lcd display (I2C addresses 0x27 and 0x3F) (+6k code) diff --git a/tasmota/xsns_88_am2320.ino b/tasmota/xsns_88_am2320.ino new file mode 100644 index 000000000..7388a9d91 --- /dev/null +++ b/tasmota/xsns_88_am2320.ino @@ -0,0 +1,225 @@ +/* + xsns_88_am2320.ino - Tasmota driver for I2C AM2320 Temp/Hum Sensor + + Copyright (C) 2019 Lars Wessels + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifdef USE_I2C +#ifdef USE_AM2320 + +/*********************************************************************************************\ + * AM2320 - Digital Temperature and Humidity Sensor + * https://akizukidenshi.com/download/ds/aosong/AM2320.pdf + * + * based on https://github.com/hibikiledo/AM2320 from Ratthanan Nalintasnai + * and https://github.com/adafruit/Adafruit_AM2320 from Limor Fried (Adafruit Industries) + * and https://github.com/nightphobos/tasmota-am2320-i2c-driver as adaptation code to Tamota +\*********************************************************************************************/ + +#define XSNS_92 92 +#define XI2C_92 92 // See I2CDEVICES.md + +#define AM2320_ADDR 0x5C // use 7bit address: 0xB8 >> 1 +#define INIT_MAX_RETRIES 5 + +char AM2320_types[] = "AM2320"; + +uint8_t am2320_found = 0; +struct AM2320_Readings { + uint8_t valid = 0; + float t = NAN; + float h = NAN; +} AM2320; + + +bool Am2320Init(void) +{ + // wake AM2320 up, goes to sleep to not warm up and affect the humidity sensor + Wire.beginTransmission(AM2320_ADDR); + Wire.write(0x02); + Wire.endTransmission(); + delay(1); + + Wire.beginTransmission(AM2320_ADDR); // start transmission + Wire.write(0x03); // read register data (function code) + Wire.write(0x00); // start address (0x00 = Temp, 0x02 = Humidity) + Wire.write(0x04); // read 4 bytes (2 byte temperature + 2 byte humidity) + if (Wire.endTransmission(1) != 0) { + return false; + } + return true; +} + + +unsigned int crc16(byte *ptr, byte length) +{ + unsigned int crc = 0xFFFF; + + while(length--) { + crc ^= *ptr++; + for(uint8_t i = 0; i < 8; i++) { + if ((crc & 0x01) != 0) { + crc >>= 1; + crc ^= 0xA001; + } else { + crc >>= 1; + } + } + } + return crc; +} + + +bool Am2320Read(void) +{ + byte buf[8]; + + if (AM2320.valid > 0) { AM2320.valid--; } + if (!Am2320Init()) { return false; } + + delayMicroseconds(1600); // >=1.5ms + Wire.requestFrom(AM2320_ADDR, 8); // request all data + if (Wire.available() != 8) { return false; } + + // read 8 bytes: preamble(2) + data(2) + crc(2) + memset(buf, 0, 8); + for (uint8_t i = 0; i < 8; i++) { + buf[i] = Wire.read(); + } + + if (buf[0] != 0x03) { return false; } // must be 0x03 function code reply + if (buf[1] != 4) { return false; } // must be 4 bytes reply + + // verify checksum + unsigned int receivedCrc = (buf[7] << 8) | buf[6]; // pack high and low byte together + if (receivedCrc == crc16(buf, 6)) { // preamble + data + int temperature = ((buf[4] & 0x7F) << 8) | buf[5]; + AM2320.t = temperature / 10.0; + // check for negative temp reading + AM2320.t = ((buf[4] & 0x80) >> 7) == 1 ? AM2320.t * (-1) : AM2320.t; + + int humidity = (buf[2] << 8) | buf[3]; + AM2320.h = humidity / 10.0; + + AM2320.valid = SENSOR_MAX_MISS; // reset error counter + return true; + + } else { + AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_I2C "Am2320Read() checksum failed")); + return false; + } +} + + +void Am2320Detect(void) +{ + if (Am2320Init()) { + if (!am2320_found) { + AddLog(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, AM2320_types, AM2320_ADDR); + } else { + AddLog(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, AM2320_types, AM2320_ADDR); + } + am2320_found = 3; + } else { + if (am2320_found) { am2320_found--; } + } +} + + +void Am2320EverySecond(void) +{ + // if (!(uptime%10)) { + // Am2320Detect(); // look for sensor every 10 seconds, after three misses it's set to not found + // } else if (uptime & 1 && am2320_found) { // read from sensor every 2 seconds + if (TasmotaGlobal.uptime &1) { + if (!Am2320Read()) { + AddLogMissed(AM2320_types, AM2320.valid); + } + // } + } +} + + +void Am2320Show(bool json) +{ + if (!am2320_found) { return; } // no sensor, no show :( + + float t, h, dewpoint; + + if (AM2320.valid) { + t = ConvertTemp(AM2320.t); + h = ConvertHumidity(AM2320.h); + dewpoint = CalcTempHumToDew(AM2320.t, AM2320.h); + } else { + t = -1; + h = -1; + dewpoint = -1; + } + + if (json) { + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%f,\"" D_JSON_HUMIDITY "\":%f,\"" D_JSON_DEWPOINT "\":%f}"),AM2320_types,t,h,dewpoint); + +#ifdef USE_DOMOTICZ + if (0 == Settings->tele_period) { + DomoticzTempHumPressureSensor(t, h); + } +#endif // USE_DOMOTICZ +#ifdef USE_KNX + if (0 == Settings->tele_period) { + KnxSensor(KNX_TEMPERATURE, t); + KnxSensor(KNX_HUMIDITY, h); + } +#endif // USE_KNX +#ifdef USE_WEBSERVER + } else { + WSContentSend_THD(AM2320_types, t, h); +#endif // USE_WEBSERVER + } +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +bool Xsns92(uint8_t function) +{ + if (!I2cEnabled(XI2C_92)) { return false; } + + boolean result = false; + + if (FUNC_INIT == function) { + Am2320Detect(); + } + else if (am2320_found) { + switch (function) { + case FUNC_EVERY_SECOND: + Am2320EverySecond(); + break; + case FUNC_JSON_APPEND: + Am2320Show(1); + break; +#ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Am2320Show(0); + break; +#endif // USE_WEBSERVER + } + } + return result; +} + +#endif // USE_AM2320 +#endif // USE_I2C \ No newline at end of file From c0207d04a0fc699695b8b68538150f3bc8426f9e Mon Sep 17 00:00:00 2001 From: Adrian Scillato <39969427+ascillato2@users.noreply.github.com> Date: Sat, 26 Jun 2021 15:52:12 -0300 Subject: [PATCH 2/4] AM2320 fix internal SNS driver number --- tasmota/xsns_88_am2320.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasmota/xsns_88_am2320.ino b/tasmota/xsns_88_am2320.ino index 7388a9d91..88da1181e 100644 --- a/tasmota/xsns_88_am2320.ino +++ b/tasmota/xsns_88_am2320.ino @@ -29,7 +29,7 @@ * and https://github.com/nightphobos/tasmota-am2320-i2c-driver as adaptation code to Tamota \*********************************************************************************************/ -#define XSNS_92 92 +#define XSNS_88 88 #define XI2C_92 92 // See I2CDEVICES.md #define AM2320_ADDR 0x5C // use 7bit address: 0xB8 >> 1 @@ -222,4 +222,4 @@ bool Xsns92(uint8_t function) } #endif // USE_AM2320 -#endif // USE_I2C \ No newline at end of file +#endif // USE_I2C From 7d161ad8c6ad1f8f65e6d7d2bbe172f7cff8851e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 27 Jun 2021 13:31:12 +0200 Subject: [PATCH 3/4] Update my_user_config.h --- tasmota/my_user_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index e731d4898..398f8ecaa 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -632,7 +632,7 @@ // #define USE_SEESAW_SOIL // [I2cDriver56] Enable Capacitice Soil Moisture & Temperature Sensor (I2C addresses 0x36 - 0x39) (+1k3 code) // #define USE_MPU_ACCEL // [I2cDriver58] Enable MPU6886/MPU9250 - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x68) (+2k code) // #define USE_BM8563 // [I2cDriver59] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code) -// #define USE_AM2320 // [I2cDriver88] Enable AM2320 temperature and humidity Sensor (I2C address 0x5C)() +// #define USE_AM2320 // [I2cDriver60] Enable AM2320 temperature and humidity Sensor (I2C address 0x5C)() // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 #define USE_DISPLAY_LCD // [DisplayModel 1] [I2cDriver3] Enable Lcd display (I2C addresses 0x27 and 0x3F) (+6k code) From c2026623d088dcb0dc203a878d0d813f21137a61 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 27 Jun 2021 13:32:33 +0200 Subject: [PATCH 4/4] Update xsns_88_am2320.ino --- tasmota/xsns_88_am2320.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasmota/xsns_88_am2320.ino b/tasmota/xsns_88_am2320.ino index 88da1181e..a9b28bd73 100644 --- a/tasmota/xsns_88_am2320.ino +++ b/tasmota/xsns_88_am2320.ino @@ -30,7 +30,7 @@ \*********************************************************************************************/ #define XSNS_88 88 -#define XI2C_92 92 // See I2CDEVICES.md +#define XI2C_60 60 // See I2CDEVICES.md #define AM2320_ADDR 0x5C // use 7bit address: 0xB8 >> 1 #define INIT_MAX_RETRIES 5 @@ -196,7 +196,7 @@ void Am2320Show(bool json) bool Xsns92(uint8_t function) { - if (!I2cEnabled(XI2C_92)) { return false; } + if (!I2cEnabled(XI2C_60)) { return false; } boolean result = false;