From ee93b64800b888d19f171bf9f8eaf56640aee1f9 Mon Sep 17 00:00:00 2001 From: arendst Date: Sat, 4 Nov 2017 16:36:51 +0100 Subject: [PATCH] v5.9.0a - Streamline External Sensor Support --- sonoff/sonoff.ino | 10 ++- sonoff/support.ino | 32 ++++------ sonoff/webserver.ino | 22 +++---- sonoff/xsns_01_counter.ino | 56 +++++++---------- sonoff/xsns_03_hlw8012.ino | 87 ++++++++++----------------- sonoff/xsns_04_snfsc.ino | 66 ++++++++------------ sonoff/xsns_05_ds18b20.ino | 49 +++++++-------- sonoff/xsns_05_ds18x20.ino | 98 ++++++++++++++---------------- sonoff/xsns_06_dht.ino | 57 +++++++----------- sonoff/xsns_07_sht1x.ino | 90 ++++++++++++--------------- sonoff/xsns_08_htu21.ino | 84 +++++++++++--------------- sonoff/xsns_09_bmp.ino | 117 ++++++++++++++++-------------------- sonoff/xsns_10_bh1750.ino | 63 +++++++++---------- sonoff/xsns_11_veml6070.ino | 63 +++++++++---------- sonoff/xsns_interface.ino | 66 +++++++++----------- 15 files changed, 402 insertions(+), 558 deletions(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index c616f9e14..3bcee078e 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -183,6 +183,7 @@ uint8_t light_type = 0; // Light types boolean mdns_begun = false; +uint8_t xsns_present = 0; // Number of External Sensors found boolean (*xsns_func_ptr[XSNS_MAX])(byte); // External Sensor Function Pointers for simple implementation of sensors char version[16]; // Version string from VERSION define char my_hostname[33]; // Composed Wifi hostname @@ -1791,19 +1792,16 @@ void MqttShowState() boolean MqttShowSensor() { - boolean json_data_available = false; - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_TIME "\":\"%s\""), mqtt_data, GetDateAndTime().c_str()); + int json_data_start = strlen(mqtt_data); for (byte i = 0; i < MAX_SWITCHES; i++) { if (pin[GPIO_SWT1 +i] < 99) { boolean swm = ((FOLLOW_INV == Settings.switchmode[i]) || (PUSHBUTTON_INV == Settings.switchmode[i]) || (PUSHBUTTONHOLD_INV == Settings.switchmode[i])); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_SWITCH "%d\":\"%s\""), mqtt_data, i +1, GetStateText(swm ^ lastwallswitch[i])); - json_data_available = true; } } - if (XsnsCall(FUNC_XSNS_JSON)) { - json_data_available = true; - } + XsnsCall(FUNC_XSNS_JSON); + boolean json_data_available = (strlen(mqtt_data) - json_data_start); if (strstr_P(mqtt_data, PSTR(D_TEMPERATURE))) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_TEMPERATURE_UNIT "\":\"%c\""), mqtt_data, TempUnit()); } diff --git a/sonoff/support.ino b/sonoff/support.ino index c0b99a6e5..30a4683aa 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1228,28 +1228,23 @@ int GetCommandCode(char* destination, size_t destination_size, const char* needl * ADC support \*********************************************************************************************/ -uint16_t GetAdc0() +void AdcShow(boolean json) { - uint16_t alr = 0; + uint16_t analog = 0; for (byte i = 0; i < 32; i++) { - alr += analogRead(A0); + analog += analogRead(A0); delay(1); } - return alr >> 5; -} - -boolean MqttShowAdc() -{ - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_ANALOG_INPUT0 "\":%d"), mqtt_data, GetAdc0()); - return true; -} + analog >>= 5; + if (json) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_ANALOG_INPUT0 "\":%d"), mqtt_data, analog); #ifdef USE_WEBSERVER -void WebShowAdc() -{ - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{s}" D_ANALOG_INPUT0 "{m}%d{e}"), mqtt_data, GetAdc0()); -} + } else { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{s}" D_ANALOG_INPUT0 "{m}%d{e}"), mqtt_data, analog); #endif // USE_WEBSERVER + } +} /*********************************************************************************************\ * Interface @@ -1268,23 +1263,22 @@ boolean Xsns02(byte function) // case FUNC_XSNS_PREP: // break; case FUNC_XSNS_JSON: - result = MqttShowAdc(); + AdcShow(1); break; #ifdef USE_WEBSERVER case FUNC_XSNS_WEB: - WebShowAdc(); + AdcShow(0); break; #endif // USE_WEBSERVER } } return result; } - #endif // USE_ADC_VCC /*********************************************************************************************\ * Syslog - * + * * Example: * snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_LOG "Any value %d"), value); * AddLog(LOG_LEVEL_DEBUG); diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino index df75b53dd..4e8394c7f 100644 --- a/sonoff/webserver.ino +++ b/sonoff/webserver.ino @@ -60,7 +60,8 @@ const char HTTP_HEAD[] PROGMEM = "x=new XMLHttpRequest();" "x.onreadystatechange=function(){" "if(x.readyState==4&&x.status==200){" - "document.getElementById('l1').innerHTML=x.responseText;" + "var s=x.responseText.replace(/{s}/g,\"\").replace(/{m}/g,\"\").replace(/{e}/g,\"\").replace(/{t}/g,\"%'>
"; -const char HTTP_ROW_START[] PROGMEM = ""; // Replaces {s} -const char HTTP_ROW_MIDDLE[] PROGMEM = ""; // Replases {m} -const char HTTP_ROW_END[] PROGMEM = ""; // Replaces {e} const char HTTP_SNS_TEMP[] PROGMEM = "%s{s}%s " D_TEMPERATURE "{m}%s°%c{e}"; // {s} = , {m} = , {e} = const char HTTP_SNS_HUM[] PROGMEM = "%s{s}%s " D_HUMIDITY "{m}%s%{e}"; // {s} = , {m} = , {e} = const char HTTP_SNS_PRESSURE[] PROGMEM = "%s{s}%s " D_PRESSURE "{m}%s " D_UNIT_PRESSURE "{e}"; // {s} = , {m} = , {e} = @@ -514,27 +512,23 @@ void HandleAjaxStatusRefresh() ExecuteCommand(svalue); } + String page = ""; mqtt_data[0] = '\0'; XsnsCall(FUNC_XSNS_WEB); - String tpage = mqtt_data; - tpage.replace(F("{s}"), FPSTR(HTTP_ROW_START)); // = - tpage.replace(F("{m}"), FPSTR(HTTP_ROW_MIDDLE)); // = - tpage.replace(F("{e}"), FPSTR(HTTP_ROW_END)); // = - - String page = ""; - if (tpage.length() > 0) { + if (strlen(mqtt_data)) { page += FPSTR(HTTP_TABLE100); - page += tpage; + page += mqtt_data; page += F(""); } - char line[160]; + char line[80]; if (devices_present) { page += FPSTR(HTTP_TABLE100); page += F(""); uint8_t fsize = (devices_present < 5) ? 70 - (devices_present * 8) : 32; for (byte idx = 1; idx <= devices_present; idx++) { snprintf_P(svalue, sizeof(svalue), PSTR("%d"), bitRead(power, idx -1)); - snprintf_P(line, sizeof(line), PSTR("
%s
"), +// snprintf_P(line, sizeof(line), PSTR("
%s
"), + snprintf_P(line, sizeof(line), PSTR("%s
"), // {t} = %'>
, {m} = , {e} = +#endif // USE_WEBSERVER -void WebShowBh1750() +void Bh1750Show(boolean json) { if (bh1750_type) { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, mqtt_data, Bh1750ReadLux()); + uint16_t illuminance = Bh1750ReadLux(); + + if (json) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"%s\":{\"" D_ILLUMINANCE "\":%d}"), mqtt_data, bh1750_types, illuminance); +#ifdef USE_DOMOTICZ + DomoticzSensor(DZ_ILLUMINANCE, illuminance); +#endif // USE_DOMOTICZ +#ifdef USE_WEBSERVER + } else { + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, mqtt_data, illuminance); +#endif // USE_WEBSERVER + } } } -#endif // USE_WEBSERVER /*********************************************************************************************\ * Interface @@ -114,20 +109,22 @@ boolean Xsns10(byte function) { boolean result = false; - switch (function) { -// case FUNC_XSNS_INIT: -// break; - case FUNC_XSNS_PREP: - Bh1750Detect(); - break; - case FUNC_XSNS_JSON: - result = MqttShowBh1750(); - break; + if (i2c_flg) { + switch (function) { +// case FUNC_XSNS_INIT: +// break; + case FUNC_XSNS_PREP: + Bh1750Detect(); + break; + case FUNC_XSNS_JSON: + Bh1750Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_XSNS_WEB: - WebShowBh1750(); - break; + case FUNC_XSNS_WEB: + Bh1750Show(0); + break; #endif // USE_WEBSERVER + } } return result; } diff --git a/sonoff/xsns_11_veml6070.ino b/sonoff/xsns_11_veml6070.ino index c66cf0b27..82e0f594b 100644 --- a/sonoff/xsns_11_veml6070.ino +++ b/sonoff/xsns_11_veml6070.ino @@ -47,6 +47,8 @@ uint16_t Veml6070ReadUv() return uvi; } +/********************************************************************************************/ + boolean Veml6070Detect() { if (veml6070_type) { @@ -75,35 +77,28 @@ boolean Veml6070Detect() return success; } -/*********************************************************************************************\ - * Presentation -\*********************************************************************************************/ - -boolean MqttShowVeml6070() -{ - if (!veml6070_type) { - return false; - } - - uint16_t uv = Veml6070ReadUv(); - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"%s\":{\"" D_UV_LEVEL "\":%d}"), mqtt_data, veml6070_types, uv); -#ifdef USE_DOMOTICZ - DomoticzSensor(DZ_ILLUMINANCE, uv); -#endif // USE_DOMOTICZ - return true; -} - #ifdef USE_WEBSERVER const char HTTP_SNS_ULTRAVIOLET[] PROGMEM = "%s{s}VEML6070 " D_UV_LEVEL "{m}%d{e}"; // {s} = , {m} = , {e} = +#endif // USE_WEBSERVER -void WebShowVeml6070() +void Veml6070Show(boolean json) { if (veml6070_type) { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ULTRAVIOLET, mqtt_data, Veml6070ReadUv()); + uint16_t uvlevel = Veml6070ReadUv(); + + if (json) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"%s\":{\"" D_UV_LEVEL "\":%d}"), mqtt_data, veml6070_types, uvlevel); +#ifdef USE_DOMOTICZ + DomoticzSensor(DZ_ILLUMINANCE, uvlevel); +#endif // USE_DOMOTICZ +#ifdef USE_WEBSERVER + } else { + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ULTRAVIOLET, mqtt_data, uvlevel); +#endif // USE_WEBSERVER + } } } -#endif // USE_WEBSERVER /*********************************************************************************************\ * Interface @@ -115,20 +110,22 @@ boolean Xsns11(byte function) { boolean result = false; - switch (function) { -// case FUNC_XSNS_INIT: -// break; - case FUNC_XSNS_PREP: - Veml6070Detect(); - break; - case FUNC_XSNS_JSON: - result = MqttShowVeml6070(); - break; + if (i2c_flg) { + switch (function) { +// case FUNC_XSNS_INIT: +// break; + case FUNC_XSNS_PREP: + Veml6070Detect(); + break; + case FUNC_XSNS_JSON: + Veml6070Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_XSNS_WEB: - WebShowVeml6070(); - break; + case FUNC_XSNS_WEB: + Veml6070Show(0); + break; #endif // USE_WEBSERVER + } } return result; } diff --git a/sonoff/xsns_interface.ino b/sonoff/xsns_interface.ino index a17d70a5f..53b8018ff 100644 --- a/sonoff/xsns_interface.ino +++ b/sonoff/xsns_interface.ino @@ -19,91 +19,89 @@ void XSnsInit() { - byte x; - - for (x = 0; x < XSNS_MAX; x++) { - xsns_func_ptr[x] = NULL; + for (byte i = 0; i < XSNS_MAX; i++) { + xsns_func_ptr[i] = NULL; } - x = 0; + xsns_present = 0; #ifdef XSNS_01 - xsns_func_ptr[x++] = &Xsns01; + xsns_func_ptr[xsns_present++] = &Xsns01; #endif #ifdef XSNS_02 - xsns_func_ptr[x++] = &Xsns02; + xsns_func_ptr[xsns_present++] = &Xsns02; #endif #ifdef XSNS_03 - xsns_func_ptr[x++] = &Xsns03; + xsns_func_ptr[xsns_present++] = &Xsns03; #endif #ifdef XSNS_04 - xsns_func_ptr[x++] = &Xsns04; + xsns_func_ptr[xsns_present++] = &Xsns04; #endif #ifdef XSNS_05 - xsns_func_ptr[x++] = &Xsns05; + xsns_func_ptr[xsns_present++] = &Xsns05; #endif #ifdef XSNS_06 - xsns_func_ptr[x++] = &Xsns06; + xsns_func_ptr[xsns_present++] = &Xsns06; #endif #ifdef XSNS_07 - xsns_func_ptr[x++] = &Xsns07; + xsns_func_ptr[xsns_present++] = &Xsns07; #endif #ifdef XSNS_08 - xsns_func_ptr[x++] = &Xsns08; + xsns_func_ptr[xsns_present++] = &Xsns08; #endif #ifdef XSNS_09 - xsns_func_ptr[x++] = &Xsns09; + xsns_func_ptr[xsns_present++] = &Xsns09; #endif #ifdef XSNS_10 - xsns_func_ptr[x++] = &Xsns10; + xsns_func_ptr[xsns_present++] = &Xsns10; #endif #ifdef XSNS_11 - xsns_func_ptr[x++] = &Xsns11; + xsns_func_ptr[xsns_present++] = &Xsns11; #endif #ifdef XSNS_12 - xsns_func_ptr[x++] = &Xsns12; + xsns_func_ptr[xsns_present++] = &Xsns12; #endif #ifdef XSNS_13 - xsns_func_ptr[x++] = &Xsns13; + xsns_func_ptr[xsns_present++] = &Xsns13; #endif #ifdef XSNS_14 - xsns_func_ptr[x++] = &Xsns14; + xsns_func_ptr[xsns_present++] = &Xsns14; #endif #ifdef XSNS_15 - xsns_func_ptr[x++] = &Xsns15; + xsns_func_ptr[xsns_present++] = &Xsns15; #endif #ifdef XSNS_16 - xsns_func_ptr[x++] = &Xsns16; + xsns_func_ptr[xsns_present++] = &Xsns16; #endif #ifdef XSNS_17 - xsns_func_ptr[x++] = &Xsns17; + xsns_func_ptr[xsns_present++] = &Xsns17; #endif #ifdef XSNS_18 - xsns_func_ptr[x++] = &Xsns18; + xsns_func_ptr[xsns_present++] = &Xsns18; #endif #ifdef XSNS_19 - xsns_func_ptr[x++] = &Xsns19; + xsns_func_ptr[xsns_present++] = &Xsns19; #endif #ifdef XSNS_20 - xsns_func_ptr[x++] = &Xsns20; + xsns_func_ptr[xsns_present++] = &Xsns20; #endif XsnsCall(FUNC_XSNS_INIT); @@ -120,20 +118,12 @@ boolean XsnsCall(byte Function) switch (Function) { case FUNC_XSNS_INIT: case FUNC_XSNS_PREP: - case FUNC_XSNS_WEB: - for (byte x = 0; x < XSNS_MAX; x++) { - if (xsns_func_ptr[x]) { - xsns_func_ptr[x](Function); - } - } - break; case FUNC_XSNS_JSON: - for (byte x = 0; x < XSNS_MAX; x++) { - if (xsns_func_ptr[x]) { - if (xsns_func_ptr[x](Function)) { - result = true; - } - } + case FUNC_XSNS_WEB: + for (byte x = 0; x < xsns_present; x++) { +// if (xsns_func_ptr[x]) { + xsns_func_ptr[x](Function); +// } } break; }