From 37e1b31937cc8dbb3971453e2bfb2a29a22a2b27 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 10 Mar 2019 13:33:32 +0100 Subject: [PATCH] Add support for sensor SCD30 * Add support for sensor SCD30 (#5434) * Add support for commands in sensor drivers --- API.md | 2 ++ sonoff/_changelog.ino | 2 ++ sonoff/my_user_config.h | 1 + sonoff/sonoff.h | 2 +- sonoff/sonoff.ino | 18 ++++++++++--- sonoff/sonoff_post.h | 4 +-- sonoff/xdrv_interface.ino | 15 +---------- sonoff/xsns_13_ina219.ino | 2 +- sonoff/xsns_15_mhz19.ino | 2 +- sonoff/xsns_27_apds9960.ino | 2 +- sonoff/xsns_29_mcp230xx.ino | 2 +- sonoff/xsns_34_hx711.ino | 2 +- sonoff/xsns_36_mgc3130.ino | 2 +- sonoff/xsns_40_pn532.ino | 2 +- .../{xdrv_92_scd30.ino => xsns_42_scd30.ino} | 27 ++++++------------- sonoff/xsns_interface.ino | 4 ++- 16 files changed, 41 insertions(+), 48 deletions(-) rename sonoff/{xdrv_92_scd30.ino => xsns_42_scd30.ino} (98%) diff --git a/API.md b/API.md index 23e45369d..caec908c6 100644 --- a/API.md +++ b/API.md @@ -23,6 +23,8 @@ FUNC_JSON_APPEND | | | | x | | Extend tele FUNC_WEB_APPEND | | | | x | | Extend webgui ajax info FUNC_SAVE_BEFORE_RESTART | | | | x | | Just before a planned restart FUNC_COMMAND | x | | x | x | | When a command is not recognized +FUNC_COMMAND_DRIVER | x | 6.4.1.21 | x | | | When command Driver\ is executed +FUNC_COMMAND_SENSOR | x | 6.4.1.21 | | x | | When command Sensor\ is executed FUNC_MQTT_SUBSCRIBE | | 5.12.0k | x | | | At end of MQTT subscriptions FUNC_MQTT_INIT | | 5.12.0k | x | | | Once at end of MQTT connection FUNC_MQTT_DATA | x | 5.12.0k | x | | | Before decoding command diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 7fcc80b8d..f0112b908 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,5 +1,7 @@ /* 6.4.1.21 20190309 * Fix exception on GUI Configure Logging and Configure Other (#5424) + * Add support for sensor SCD30 (#5434) + * Add support for commands in sensor drivers * * 6.4.1.20 20190304 * Changed webserver content handling from single String to small Chunks increasing RAM diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index b5c52ff81..3030654ce 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -331,6 +331,7 @@ // #define USE_RTC_ADDR 0x68 // Default I2C address 0x68 // #define USE_MGC3130 // Enable MGC3130 Electric Field Effect Sensor (I2C address 0x42) (+2k7 code, 0k3 mem) // #define USE_MAX44009 // Enable MAX44009 Ambient Light sensor (I2C addresses 0x4A and 0x4B) (+0k8 code) +// #define USE_SCD30 // Enable Sensiron SCd30 CO2 sensor (I2C address 0x61) (+3k3 code) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index 951c7f6d2..6c44f54fd 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -252,7 +252,7 @@ enum LightSchemes {LS_POWER, LS_WAKEUP, LS_CYCLEUP, LS_CYCLEDN, LS_RANDOM, LS_MA enum XsnsFunctions {FUNC_SETTINGS_OVERRIDE, FUNC_MODULE_INIT, FUNC_PRE_INIT, FUNC_INIT, FUNC_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_100_MSECOND, FUNC_EVERY_200_MSECOND, FUNC_EVERY_250_MSECOND, FUNC_EVERY_SECOND, - FUNC_PREP_BEFORE_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_APPEND, FUNC_SAVE_BEFORE_RESTART, FUNC_COMMAND, + FUNC_PREP_BEFORE_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_APPEND, FUNC_SAVE_BEFORE_RESTART, FUNC_COMMAND, FUNC_COMMAND_SENSOR, FUNC_COMMAND_DRIVER, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, FUNC_SET_POWER, FUNC_SET_DEVICE_POWER, FUNC_SHOW_SENSOR, FUNC_RULES_PROCESS, FUNC_SERIAL, FUNC_FREE_MEM, FUNC_BUTTON_PRESSED, diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 0f403312e..57e4f62df 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -527,8 +527,18 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) int command_code = GetCommandCode(command, sizeof(command), type, kTasmotaCommands); if (-1 == command_code) { - if (!XdrvCommand(grpflg, type, index, dataBuf, data_len, payload, payload16)) { - type = NULL; // Unknown command +// XdrvMailbox.valid = 1; + XdrvMailbox.index = index; + XdrvMailbox.data_len = data_len; + XdrvMailbox.payload16 = payload16; + XdrvMailbox.payload = payload; + XdrvMailbox.grpflg = grpflg; + XdrvMailbox.topic = type; + XdrvMailbox.data = dataBuf; + if (!XdrvCall(FUNC_COMMAND)) { + if (!XsnsCall(FUNC_COMMAND)) { + type = NULL; // Unknown command + } } } else if (CMND_BACKLOG == command_code) { @@ -711,9 +721,9 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) XdrvMailbox.topic = command; XdrvMailbox.data = dataBuf; if (CMND_SENSOR == command_code) { - XsnsCall(FUNC_COMMAND); + XsnsCall(FUNC_COMMAND_SENSOR); } else { - XdrvCall(FUNC_COMMAND); + XdrvCall(FUNC_COMMAND_DRIVER); } } else if ((CMND_SETOPTION == command_code) && (index < 82)) { diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h index b114c9c3d..fe5c5c08d 100644 --- a/sonoff/sonoff_post.h +++ b/sonoff/sonoff_post.h @@ -92,10 +92,10 @@ void KNX_CB_Action(message_t const &msg, void *arg); //#define USE_MPU6050 // Enable MPU6050 sensor (I2C address 0x68 AD0 low or 0x69 AD0 high) (+2k6 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_MAX44009 // Enable MAX44009 Ambient Light sensor (I2C addresses 0x4A and 0x4B) (+0k8 code) +//#define USE_MAX44009 // Enable MAX44009 Ambient Light sensor (I2C addresses 0x4A and 0x4B) (+0k8 code) +#define USE_SCD30 // Enable Sensiron SCd30 CO2 sensor (I2C address 0x61) (+3k3 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_SCD30 // Add support for Sensiron SCd30 CO2 sensor (+3k6 code) #ifndef CO2_LOW #define CO2_LOW 800 // Below this CO2 value show green light (needs PWM or WS2812 RG(B) led and enable with SetOption18 1) #endif diff --git a/sonoff/xdrv_interface.ino b/sonoff/xdrv_interface.ino index 7c3a31c25..7afea8531 100644 --- a/sonoff/xdrv_interface.ino +++ b/sonoff/xdrv_interface.ino @@ -192,20 +192,6 @@ bool (* const xdrv_func_ptr[])(uint8_t) = { // Driver Function Pointers const uint8_t xdrv_present = sizeof(xdrv_func_ptr) / sizeof(xdrv_func_ptr[0]); // Number of drivers found -bool XdrvCommand(bool grpflg, char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload, uint16_t payload16) -{ -// XdrvMailbox.valid = 1; - XdrvMailbox.index = index; - XdrvMailbox.data_len = data_len; - XdrvMailbox.payload16 = payload16; - XdrvMailbox.payload = payload; - XdrvMailbox.grpflg = grpflg; - XdrvMailbox.topic = type; - XdrvMailbox.data = dataBuf; - - return XdrvCall(FUNC_COMMAND); -} - bool XdrvMqttData(char *topicBuf, uint16_t stopicBuf, char *dataBuf, uint16_t sdataBuf) { XdrvMailbox.index = stopicBuf; @@ -242,6 +228,7 @@ bool XdrvCall(uint8_t Function) result = xdrv_func_ptr[x](Function); if (result && ((FUNC_COMMAND == Function) || + (FUNC_COMMAND_DRIVER == Function) || (FUNC_MQTT_DATA == Function) || (FUNC_RULES_PROCESS == Function) || (FUNC_BUTTON_PRESSED == Function) || diff --git a/sonoff/xsns_13_ina219.ino b/sonoff/xsns_13_ina219.ino index dcf93867b..88f6ce240 100644 --- a/sonoff/xsns_13_ina219.ino +++ b/sonoff/xsns_13_ina219.ino @@ -266,7 +266,7 @@ bool Xsns13(uint8_t function) if (i2c_flg) { switch (function) { - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if ((XSNS_13 == XdrvMailbox.index) && (ina219_type)) { result = Ina219CommandSensor(); } diff --git a/sonoff/xsns_15_mhz19.ino b/sonoff/xsns_15_mhz19.ino index 019a4dc6f..1e21aa6fb 100644 --- a/sonoff/xsns_15_mhz19.ino +++ b/sonoff/xsns_15_mhz19.ino @@ -373,7 +373,7 @@ bool Xsns15(uint8_t function) case FUNC_EVERY_SECOND: MhzEverySecond(); break; - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if (XSNS_15 == XdrvMailbox.index) { result = MhzCommandSensor(); } diff --git a/sonoff/xsns_27_apds9960.ino b/sonoff/xsns_27_apds9960.ino index ac1b7a947..765c89263 100644 --- a/sonoff/xsns_27_apds9960.ino +++ b/sonoff/xsns_27_apds9960.ino @@ -2046,7 +2046,7 @@ bool Xsns27(uint8_t function) case FUNC_EVERY_50_MSECOND: APDS9960_loop(); break; - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if (XSNS_27 == XdrvMailbox.index) { result = APDS9960CommandSensor(); } diff --git a/sonoff/xsns_29_mcp230xx.ino b/sonoff/xsns_29_mcp230xx.ino index b72afb66a..d7aaaf2ef 100644 --- a/sonoff/xsns_29_mcp230xx.ino +++ b/sonoff/xsns_29_mcp230xx.ino @@ -814,7 +814,7 @@ bool Xsns29(uint8_t function) case FUNC_JSON_APPEND: MCP230xx_Show(1); break; - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if (XSNS_29 == XdrvMailbox.index) { result = MCP230xx_Command(); } diff --git a/sonoff/xsns_34_hx711.ino b/sonoff/xsns_34_hx711.ino index 15f9f37db..349dcee63 100644 --- a/sonoff/xsns_34_hx711.ino +++ b/sonoff/xsns_34_hx711.ino @@ -476,7 +476,7 @@ bool Xsns34(uint8_t function) case FUNC_EVERY_100_MSECOND: HxEvery100mSecond(); break; - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if (XSNS_34 == XdrvMailbox.index) { result = HxCommand(); } diff --git a/sonoff/xsns_36_mgc3130.ino b/sonoff/xsns_36_mgc3130.ino index 20df0a223..5c1d2df48 100644 --- a/sonoff/xsns_36_mgc3130.ino +++ b/sonoff/xsns_36_mgc3130.ino @@ -613,7 +613,7 @@ bool Xsns36(uint8_t function) case FUNC_EVERY_50_MSECOND: MGC3130_loop(); break; - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if (XSNS_36 == XdrvMailbox.index) { result = MGC3130CommandSensor(); } diff --git a/sonoff/xsns_40_pn532.ino b/sonoff/xsns_40_pn532.ino index ea88143e3..0bfa8bda1 100644 --- a/sonoff/xsns_40_pn532.ino +++ b/sonoff/xsns_40_pn532.ino @@ -593,7 +593,7 @@ bool Xsns40(uint8_t function) case FUNC_EVERY_SECOND: break; #ifdef USE_PN532_DATA_FUNCTION - case FUNC_COMMAND: + case FUNC_COMMAND_SENSOR: if (XSNS_40 == XdrvMailbox.index) { result = PN532_Command(); } diff --git a/sonoff/xdrv_92_scd30.ino b/sonoff/xsns_42_scd30.ino similarity index 98% rename from sonoff/xdrv_92_scd30.ino rename to sonoff/xsns_42_scd30.ino index 80743ebb0..c62df4d25 100644 --- a/sonoff/xdrv_92_scd30.ino +++ b/sonoff/xsns_42_scd30.ino @@ -1,5 +1,5 @@ /* - xdrv_92_scd30.ino - SC30 CO2 sensor support for Sonoff-Tasmota + xsns_42_scd30.ino - SC30 CO2 sensor support for Sonoff-Tasmota Copyright (C) 2019 Frogmore42 @@ -20,8 +20,8 @@ #ifdef USE_I2C #ifdef USE_SCD30 -#define XDRV_92 92 -#define XSNS_92 92 +#define XSNS_42 42 + #define SCD30_MAX_MISSED_READS 3 #define SONOFF_SCD30_STATE_NO_ERROR 0 #define SONOFF_SCD30_STATE_ERROR_DATA_CRC 1 @@ -224,7 +224,7 @@ int Scd30Update() #endif scd30Reset_count++; error = scd30.softReset(); - if (error) + if (error) { #ifdef SCD30_DEBUG snprintf_P(log_data, sizeof(log_data), "SCD30: resetting got error: 0x%lX", error); @@ -465,21 +465,7 @@ void Scd30Show(bool json) * Interface \*********************************************************************************************/ -bool Xdrv92(byte function) -{ - bool result = false; - - if (i2c_flg) { - switch (function) { - case FUNC_COMMAND: - result = Scd30CommandSensor(); - break; - } - } - return result; -} - -bool Xsns92(byte function) +bool Xsns42(byte function) { bool result = false; @@ -488,6 +474,9 @@ bool Xsns92(byte function) case FUNC_EVERY_SECOND: Scd30Update(); break; + case FUNC_COMMAND: + result = Scd30CommandSensor(); + break; case FUNC_JSON_APPEND: Scd30Show(1); break; diff --git a/sonoff/xsns_interface.ino b/sonoff/xsns_interface.ino index 5e77b0d46..fdb947bfb 100644 --- a/sonoff/xsns_interface.ino +++ b/sonoff/xsns_interface.ino @@ -310,7 +310,9 @@ bool XsnsCall(uint8_t Function) } #endif // PROFILE_XSNS_SENSOR_EVERY_SECOND - if (result && (FUNC_COMMAND == Function)) { + if (result && ((FUNC_COMMAND == Function) || + (FUNC_COMMAND_SENSOR == Function) + )) { break; } #ifdef USE_DEBUG_DRIVER