Add support for MAX44009

Add support for MAX44009 Ambient Light sensor (#4907)
This commit is contained in:
Theo Arends 2019-01-24 11:41:52 +01:00
parent 669f6c86c9
commit ced019eaa5
5 changed files with 19 additions and 15 deletions

View File

@ -1,6 +1,7 @@
/* 6.4.1.10 20190121 /* 6.4.1.10 20190121
* Fix Hass discovery of MHZ19(B) sensors (#4992) * Fix Hass discovery of MHZ19(B) sensors (#4992)
* Fix Hass Software Watchdog exception during discovery (#4988) * Fix Hass Software Watchdog exception during discovery (#4988)
* Add support for MAX44009 Ambient Light sensor (#4907)
* *
* 6.4.1.9 20190115 * 6.4.1.9 20190115
* Add support for Mi LED Desk Lamp with rotary switch (#4887) * Add support for Mi LED Desk Lamp with rotary switch (#4887)

View File

@ -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_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_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_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_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) #define USE_SENSEAIR // Add support for SenseAir K30, K70 and S8 CO2 sensor (+2k3 code)
#ifndef CO2_LOW #ifndef CO2_LOW

View File

@ -379,7 +379,9 @@ void GetFeatures(void)
#ifdef USE_PN532_I2C #ifdef USE_PN532_I2C
feature_sns2 |= 0x00100000; // xsns_40_pn532_i2c.ino feature_sns2 |= 0x00100000; // xsns_40_pn532_i2c.ino
#endif #endif
// feature_sns2 |= 0x00200000; #ifdef USE_MAX44009
feature_sns2 |= 0x00200000;
#endif
// feature_sns2 |= 0x00400000; // feature_sns2 |= 0x00400000;
// feature_sns2 |= 0x00800000; // feature_sns2 |= 0x00800000;
// feature_sns2 |= 0x01000000; // feature_sns2 |= 0x01000000;

View File

@ -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 Copyright (C) 2019 Theo Arends
@ -25,7 +25,7 @@
* I2C Address: 0x4a or 0x4b * I2C Address: 0x4a or 0x4b
\*********************************************************************************************/ \*********************************************************************************************/
#define XSNS_91 91 #define XSNS_41 41
#define MAX44009_ADDR1 0x4A #define MAX44009_ADDR1 0x4A
#define MAX44009_ADDR2 0x4B #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_addresses[] = { MAX44009_ADDR1, MAX44009_ADDR2, 0 }; //0 terminated list
uint8_t max44009_found = 0; uint8_t max44009_found = 0;
uint8_t max44009_valid = 0; uint8_t max44009_valid = 0;
float max44009_illuminance = 0; float max44009_illuminance = 0;
char max44009_types[] = "MAX44009"; char max44009_types[] = "MAX44009";
bool Max4409Read_lum(void) bool Max4409Read_lum(void)
@ -53,7 +53,7 @@ bool Max4409Read_lum(void)
if (I2cValidRead16((uint16_t *)&regdata, max44009_address, REG_LUMINANCE)) { if (I2cValidRead16((uint16_t *)&regdata, max44009_address, REG_LUMINANCE)) {
int exponent = (regdata[0] & 0xF0) >> 4; int exponent = (regdata[0] & 0xF0) >> 4;
int mantissa = ((regdata[0] & 0x0F) << 4) | (regdata[1] & 0x0F); 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; max44009_valid = 1;
return true; return true;
} else { } else {
@ -84,12 +84,12 @@ void Max4409Detect(void)
//AddLog(LOG_LEVEL_DEBUG_MORE); //AddLog(LOG_LEVEL_DEBUG_MORE);
if ((0x00 == buffer1) && if ((0x00 == buffer1) &&
(0xFF == buffer2)) { (0xFF == buffer2)) {
// looks like a MAX44009, try to initialize // looks like a MAX44009, try to initialize
Wire.beginTransmission(max44009_address); Wire.beginTransmission(max44009_address);
// select configuration register and set mode // select configuration register and set mode
Wire.write(REG_CONFIG); Wire.write(REG_CONFIG);
Wire.write(MAX44009_CONTINUOUS_AUTO_MODE); Wire.write(MAX44009_CONTINUOUS_AUTO_MODE);
if (0 == Wire.endTransmission()) { if (0 == Wire.endTransmission()) {
@ -119,7 +119,7 @@ void Max4409Show(boolean json)
/* convert illuminance to string with suitable accuracy */ /* convert illuminance to string with suitable accuracy */
uint8_t prec = 0; uint8_t prec = 0;
if (10 > max44009_illuminance ) { if (10 > max44009_illuminance ) {
prec = 3; prec = 3;
} else if (100 > max44009_illuminance) { } else if (100 > max44009_illuminance) {
prec = 2; prec = 2;
@ -129,8 +129,8 @@ void Max4409Show(boolean json)
dtostrf(max44009_illuminance, sizeof(illum_str) -1, prec, illum_str); dtostrf(max44009_illuminance, sizeof(illum_str) -1, prec, illum_str);
if (json) { if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), snprintf_P(mqtt_data, sizeof(mqtt_data),
PSTR("%s,\"%s\":{\"" D_JSON_ILLUMINANCE "\":%s}"), PSTR("%s,\"%s\":{\"" D_JSON_ILLUMINANCE "\":%s}"),
mqtt_data, max44009_types, illum_str); mqtt_data, max44009_types, illum_str);
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (0 == tele_period) { if (0 == tele_period) {
@ -140,7 +140,7 @@ void Max4409Show(boolean json)
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
// show integer value for lx on web-server // 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); mqtt_data, max44009_types, (int)max44009_illuminance);
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
@ -151,7 +151,7 @@ void Max4409Show(boolean json)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
boolean Xsns91(byte function) boolean Xsns41(byte function)
{ {
boolean result = false; boolean result = false;

View File

@ -137,7 +137,7 @@ a_features = [[
"USE_MCP39F501","USE_PZEM_AC","USE_DS3231","USE_HX711", "USE_MCP39F501","USE_PZEM_AC","USE_DS3231","USE_HX711",
"USE_PZEM_DC","USE_TX20_WIND_SENSOR","USE_MGC3130","USE_RF_SENSOR", "USE_PZEM_DC","USE_TX20_WIND_SENSOR","USE_MGC3130","USE_RF_SENSOR",
"USE_THEO_V2","USE_ALECTO_V2","USE_AZ7798","USE_MAX31855", "USE_THEO_V2","USE_ALECTO_V2","USE_AZ7798","USE_MAX31855",
"USE_PN532_I2C","","","", "USE_PN532_I2C","USE_MAX44009","","",
"","","","", "","","","",
"","","",""]] "","","",""]]