From 7457d0b03cb75a545fa7371f066bd792038c4005 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 12 Apr 2021 16:21:33 +0200 Subject: [PATCH] Refactor ESP32 Temperature and Hall Effect sensor --- tasmota/my_user_config.h | 1 - tasmota/support_features.ino | 4 +- tasmota/support_tasmota.ino | 5 -- tasmota/tasmota_configurations_ESP32.h | 1 - ...lleffect.ino => xsns_87_esp32_sensors.ino} | 78 +++++++++++++------ 5 files changed, 54 insertions(+), 35 deletions(-) rename tasmota/{xsns_87_esp32_halleffect.ino => xsns_87_esp32_sensors.ino} (64%) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 1a6da9982..f30354df0 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -900,7 +900,6 @@ // #define ETH_CLKMODE 0 // [EthClockMode] 0 = ETH_CLOCK_GPIO0_IN, 1 = ETH_CLOCK_GPIO0_OUT, 2 = ETH_CLOCK_GPIO16_OUT, 3 = ETH_CLOCK_GPIO17_OUT #define USE_ADC // Add support for ADC on GPIO32 to GPIO39 -#define USE_HALLEFFECT // Add support for internal Hall Effcet sensor connected to GPIO36 and GPIO39 //#define USE_SPI // Add support for hardware SPI //#define USE_MI_ESP32 // Add support for ESP32 as a BLE-bridge (+9k2 mem, +292k flash) diff --git a/tasmota/support_features.ino b/tasmota/support_features.ino index 3cd84d00f..8051e4eb8 100644 --- a/tasmota/support_features.ino +++ b/tasmota/support_features.ino @@ -737,9 +737,7 @@ void ResponseAppendFeatures(void) #ifdef USE_BERRY feature8 |= 0x00000008; // xdrv_52_9_berry.ino #endif -#ifdef USE_HALLEFFECT - feature8 |= 0x00000010; // xsns_87_esp32_halleffect.ino -#endif +// feature8 |= 0x00000010; #if defined(USE_ENERGY_SENSOR) && defined(USE_ENERGY_DUMMY) feature8 |= 0x00000020; #endif diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 17fc84a3f..eb852ca77 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -821,11 +821,6 @@ bool MqttShowSensor(void) { ResponseAppendTime(); -#ifdef ESP32 - float t = CpuTemperature(); - ResponseAppend_P(PSTR(",\"Cpu" D_JSON_TEMPERATURE "\":%*_f"), Settings.flag2.temperature_resolution, &t); -#endif - int json_data_start = strlen(TasmotaGlobal.mqtt_data); for (uint32_t i = 0; i < MAX_SWITCHES; i++) { #ifdef USE_TM1638 diff --git a/tasmota/tasmota_configurations_ESP32.h b/tasmota/tasmota_configurations_ESP32.h index 8d7295ce4..ef568017d 100644 --- a/tasmota/tasmota_configurations_ESP32.h +++ b/tasmota/tasmota_configurations_ESP32.h @@ -187,7 +187,6 @@ #define USE_LIGHT_PALETTE // Add support for color palette (+0k9 code) -#define USE_HALLEFFECT // Add support for internal Hall Effcet sensor connected to GPIO36 and GPIO39 #define USE_DS18x20 // Add support for DS18x20 sensors with id sort, single scan and read retry (+1k3 code) #define USE_I2C // I2C using library wire (+10k code, 0k2 mem, 124 iram) diff --git a/tasmota/xsns_87_esp32_halleffect.ino b/tasmota/xsns_87_esp32_sensors.ino similarity index 64% rename from tasmota/xsns_87_esp32_halleffect.ino rename to tasmota/xsns_87_esp32_sensors.ino index 07fdad4bb..6ab5b9820 100644 --- a/tasmota/xsns_87_esp32_halleffect.ino +++ b/tasmota/xsns_87_esp32_sensors.ino @@ -1,5 +1,5 @@ /* - xsns_87_esp32_halleffect.ino - ESP32 Hall Effect sensor for Tasmota + xsns_87_esp32_sensors.ino - ESP32 Temperature and Hall Effect sensor for Tasmota Copyright (C) 2021 Theo Arends @@ -18,9 +18,8 @@ */ #ifdef ESP32 -#if CONFIG_IDF_TARGET_ESP32 -#ifdef USE_HALLEFFECT /*********************************************************************************************\ + * ESP32 CPU Temperature * ESP32 internal Hall Effect sensor connected to both GPIO36 and GPIO39 * * To enable set @@ -30,13 +29,15 @@ #define XSNS_87 87 +#if CONFIG_IDF_TARGET_ESP32 + #define HALLEFFECT_SAMPLE_COUNT 32 // 32 takes about 12 mS at 80MHz CPU frequency struct { bool present = false; } HEData; -void HallEffectInit(void) { +void Esp32SensorInit(void) { if (PinUsed(GPIO_HALLEFFECT) && PinUsed(GPIO_HALLEFFECT, 1)) { if (((36 == Pin(GPIO_HALLEFFECT)) && (39 == Pin(GPIO_HALLEFFECT, 1))) || ((39 == Pin(GPIO_HALLEFFECT)) && (36 == Pin(GPIO_HALLEFFECT, 1)))) { @@ -51,22 +52,52 @@ void HallEffectInit(void) { const char HTTP_SNS_HALL_EFFECT[] PROGMEM = "{s}" D_HALL_EFFECT "{m}%d{e}"; #endif // USE_WEBSERVER -void HallEffectShow(bool json) { +#endif // CONFIG_IDF_TARGET_ESP32 + +void Esp32SensorShow(bool json) { + +#if CONFIG_IDF_TARGET_ESP32 int value = 0; - for (uint32_t i = 0; i < HALLEFFECT_SAMPLE_COUNT; i++) { - value += hallRead(); + if (HEData.present) { + for (uint32_t i = 0; i < HALLEFFECT_SAMPLE_COUNT; i++) { + value += hallRead(); + } + value /= HALLEFFECT_SAMPLE_COUNT; } - value /= HALLEFFECT_SAMPLE_COUNT; +#endif // CONFIG_IDF_TARGET_ESP32 + if (json) { - ResponseAppend_P(PSTR(",\"" D_JSON_HALLEFFECT "\":%d"), value); + float t = CpuTemperature(); + ResponseAppend_P(PSTR(",\"ESP32\":{\"" D_JSON_TEMPERATURE "\":%*_f"), Settings.flag2.temperature_resolution, &t); + +#if CONFIG_IDF_TARGET_ESP32 + if (HEData.present) { + ResponseAppend_P(PSTR(",\"" D_JSON_HALLEFFECT "\":%d"), value); + } +#endif // CONFIG_IDF_TARGET_ESP32 + + ResponseJsonEnd(); + #ifdef USE_DOMOTICZ if (0 == TasmotaGlobal.tele_period) { - DomoticzSensor(DZ_COUNT, value); + +#if CONFIG_IDF_TARGET_ESP32 + if (HEData.present) { + DomoticzSensor(DZ_COUNT, value); + } +#endif // CONFIG_IDF_TARGET_ESP32 + } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - WSContentSend_P(HTTP_SNS_HALL_EFFECT, value); + +#if CONFIG_IDF_TARGET_ESP32 + if (HEData.present) { + WSContentSend_P(HTTP_SNS_HALL_EFFECT, value); + } +#endif // CONFIG_IDF_TARGET_ESP32 + #endif // USE_WEBSERVER } } @@ -78,24 +109,21 @@ void HallEffectShow(bool json) { bool Xsns87(uint8_t function) { bool result = false; - if (FUNC_INIT == function) { - HallEffectInit(); - } - else if (HEData.present) { - switch (function) { - case FUNC_JSON_APPEND: - HallEffectShow(1); - break; + switch (function) { + case FUNC_JSON_APPEND: + Esp32SensorShow(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - HallEffectShow(0); - break; + case FUNC_WEB_SENSOR: + Esp32SensorShow(0); + break; #endif // USE_WEBSERVER - } + case FUNC_INIT: + Esp32SensorInit(); + break; + } return result; } -#endif // USE_HALLEFFECT -#endif // CONFIG_IDF_TARGET_ESP32 #endif // ESP32