From ced019eaa53de2dc853d5789f20609d8e6879c06 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 24 Jan 2019 11:41:52 +0100 Subject: [PATCH] Add support for MAX44009 Add support for MAX44009 Ambient Light sensor (#4907) --- sonoff/_changelog.ino | 1 + sonoff/sonoff_post.h | 1 + sonoff/support_features.ino | 4 ++- ...s_91_max44009.ino => xsns_41_max44009.ino} | 26 +++++++++---------- tools/decode-status.py | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) rename sonoff/{xsns_91_max44009.ino => xsns_41_max44009.ino} (92%) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index a32d7f7f2..47f2132a1 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,6 +1,7 @@ /* 6.4.1.10 20190121 * Fix Hass discovery of MHZ19(B) sensors (#4992) * Fix Hass Software Watchdog exception during discovery (#4988) + * Add support for MAX44009 Ambient Light sensor (#4907) * * 6.4.1.9 20190115 * Add support for Mi LED Desk Lamp with rotary switch (#4887) diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h index 5defb494d..726ec6cba 100644 --- a/sonoff/sonoff_post.h +++ b/sonoff/sonoff_post.h @@ -93,6 +93,7 @@ void KNX_CB_Action(message_t const &msg, void *arg); //#define USE_DS3231 // Enable DS3231 external RTC in case no Wifi is avaliable. See docs in the source file (+1k2 code) //#define USE_MGC3130 // Enable MGC3130 Electric Field Effect Sensor (I2C address 0x42) (+2k7 code, 0k3 mem) //#define USE_PN532_I2C // Enable PN532 - Near Field Communication (NFC) controller (+1k6 code) +//#define USE_MAX44009 // Enable MAX44009 Ambient Light sensor (I2C addresses 0x4A and 0x4B) (+0k8 code) #define USE_MHZ19 // Add support for MH-Z19 CO2 sensor (+2k code) #define USE_SENSEAIR // Add support for SenseAir K30, K70 and S8 CO2 sensor (+2k3 code) #ifndef CO2_LOW diff --git a/sonoff/support_features.ino b/sonoff/support_features.ino index 21d67178b..6c1abf97a 100644 --- a/sonoff/support_features.ino +++ b/sonoff/support_features.ino @@ -379,7 +379,9 @@ void GetFeatures(void) #ifdef USE_PN532_I2C feature_sns2 |= 0x00100000; // xsns_40_pn532_i2c.ino #endif -// feature_sns2 |= 0x00200000; +#ifdef USE_MAX44009 + feature_sns2 |= 0x00200000; +#endif // feature_sns2 |= 0x00400000; // feature_sns2 |= 0x00800000; // feature_sns2 |= 0x01000000; diff --git a/sonoff/xsns_91_max44009.ino b/sonoff/xsns_41_max44009.ino similarity index 92% rename from sonoff/xsns_91_max44009.ino rename to sonoff/xsns_41_max44009.ino index a0aa51a47..92bd7d8a7 100644 --- a/sonoff/xsns_91_max44009.ino +++ b/sonoff/xsns_41_max44009.ino @@ -1,5 +1,5 @@ /* - xsns_91_max44009.ino - MAX44009 ambient light sensor support for Sonoff-Tasmota + xsns_41_max44009.ino - MAX44009 ambient light sensor support for Sonoff-Tasmota Copyright (C) 2019 Theo Arends @@ -25,7 +25,7 @@ * I2C Address: 0x4a or 0x4b \*********************************************************************************************/ -#define XSNS_91 91 +#define XSNS_41 41 #define MAX44009_ADDR1 0x4A #define MAX44009_ADDR2 0x4B @@ -41,7 +41,7 @@ uint8_t max44009_address; uint8_t max44009_addresses[] = { MAX44009_ADDR1, MAX44009_ADDR2, 0 }; //0 terminated list uint8_t max44009_found = 0; uint8_t max44009_valid = 0; -float max44009_illuminance = 0; +float max44009_illuminance = 0; char max44009_types[] = "MAX44009"; bool Max4409Read_lum(void) @@ -53,7 +53,7 @@ bool Max4409Read_lum(void) if (I2cValidRead16((uint16_t *)®data, max44009_address, REG_LUMINANCE)) { int exponent = (regdata[0] & 0xF0) >> 4; int mantissa = ((regdata[0] & 0x0F) << 4) | (regdata[1] & 0x0F); - max44009_illuminance = (float)(((0x00000001 << exponent) * (float)mantissa) * 0.045); + max44009_illuminance = (float)(((0x00000001 << exponent) * (float)mantissa) * 0.045); max44009_valid = 1; return true; } else { @@ -84,12 +84,12 @@ void Max4409Detect(void) //AddLog(LOG_LEVEL_DEBUG_MORE); if ((0x00 == buffer1) && (0xFF == buffer2)) { - - // looks like a MAX44009, try to initialize - + + // looks like a MAX44009, try to initialize + Wire.beginTransmission(max44009_address); - // select configuration register and set mode + // select configuration register and set mode Wire.write(REG_CONFIG); Wire.write(MAX44009_CONTINUOUS_AUTO_MODE); if (0 == Wire.endTransmission()) { @@ -119,7 +119,7 @@ void Max4409Show(boolean json) /* convert illuminance to string with suitable accuracy */ uint8_t prec = 0; - if (10 > max44009_illuminance ) { + if (10 > max44009_illuminance ) { prec = 3; } else if (100 > max44009_illuminance) { prec = 2; @@ -129,8 +129,8 @@ void Max4409Show(boolean json) dtostrf(max44009_illuminance, sizeof(illum_str) -1, prec, illum_str); if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), - PSTR("%s,\"%s\":{\"" D_JSON_ILLUMINANCE "\":%s}"), + snprintf_P(mqtt_data, sizeof(mqtt_data), + PSTR("%s,\"%s\":{\"" D_JSON_ILLUMINANCE "\":%s}"), mqtt_data, max44009_types, illum_str); #ifdef USE_DOMOTICZ if (0 == tele_period) { @@ -140,7 +140,7 @@ void Max4409Show(boolean json) #ifdef USE_WEBSERVER } else { // show integer value for lx on web-server - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, mqtt_data, max44009_types, (int)max44009_illuminance); #endif // USE_WEBSERVER } @@ -151,7 +151,7 @@ void Max4409Show(boolean json) * Interface \*********************************************************************************************/ -boolean Xsns91(byte function) +boolean Xsns41(byte function) { boolean result = false; diff --git a/tools/decode-status.py b/tools/decode-status.py index 04b549465..1b9478a8e 100644 --- a/tools/decode-status.py +++ b/tools/decode-status.py @@ -137,7 +137,7 @@ a_features = [[ "USE_MCP39F501","USE_PZEM_AC","USE_DS3231","USE_HX711", "USE_PZEM_DC","USE_TX20_WIND_SENSOR","USE_MGC3130","USE_RF_SENSOR", "USE_THEO_V2","USE_ALECTO_V2","USE_AZ7798","USE_MAX31855", - "USE_PN532_I2C","","","", + "USE_PN532_I2C","USE_MAX44009","","", "","","","", "","","",""]]