2021-03-27 14:15:15 +00:00
|
|
|
/*
|
2021-07-14 09:57:05 +01:00
|
|
|
xsns_127_esp32_sensors.ino - ESP32 Temperature and Hall Effect sensor for Tasmota
|
2021-03-27 14:15:15 +00:00
|
|
|
|
|
|
|
Copyright (C) 2021 Theo Arends
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
/*********************************************************************************************\
|
2021-04-12 15:50:04 +01:00
|
|
|
* ESP32 CPU Temperature and optional Hall Effect sensor
|
2021-03-28 11:25:30 +01:00
|
|
|
*
|
2021-07-14 09:57:05 +01:00
|
|
|
* To allow for not updating the global temperature by the ESP32 temperature sensor this
|
|
|
|
* driver needs to be the highest numbered driver (currently 127)
|
|
|
|
*
|
2021-04-12 15:50:04 +01:00
|
|
|
* ESP32 internal Hall Effect sensor connected to both GPIO36 and GPIO39
|
2021-03-28 11:25:30 +01:00
|
|
|
* To enable set
|
|
|
|
* GPIO36 as HallEffect 1
|
|
|
|
* GPIO39 as HallEffect 2
|
2021-03-27 14:15:15 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2021-07-14 09:57:05 +01:00
|
|
|
#define XSNS_127 127
|
2021-03-28 11:25:30 +01:00
|
|
|
|
2021-04-12 15:21:33 +01:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2021-03-28 11:25:30 +01:00
|
|
|
#define HALLEFFECT_SAMPLE_COUNT 32 // 32 takes about 12 mS at 80MHz CPU frequency
|
2021-03-27 14:15:15 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
bool present = false;
|
|
|
|
} HEData;
|
|
|
|
|
2021-04-12 15:21:33 +01:00
|
|
|
void Esp32SensorInit(void) {
|
2021-03-27 14:15:15 +00:00
|
|
|
if (PinUsed(GPIO_HALLEFFECT) && PinUsed(GPIO_HALLEFFECT, 1)) {
|
2021-03-28 13:38:16 +01:00
|
|
|
if (((36 == Pin(GPIO_HALLEFFECT)) && (39 == Pin(GPIO_HALLEFFECT, 1))) ||
|
|
|
|
((39 == Pin(GPIO_HALLEFFECT)) && (36 == Pin(GPIO_HALLEFFECT, 1)))) {
|
|
|
|
HEData.present = true;
|
|
|
|
hallRead();
|
|
|
|
}
|
2021-03-27 14:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 15:21:33 +01:00
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
|
|
|
void Esp32SensorShow(bool json) {
|
2021-07-13 15:44:28 +01:00
|
|
|
static bool add_global_temp = false;
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
add_global_temp = !ResponseContains_P(PSTR(D_JSON_TEMPERATURE));
|
|
|
|
}
|
|
|
|
float c = CpuTemperature(); // in Celsius
|
|
|
|
if (add_global_temp) {
|
|
|
|
UpdateGlobalTemperature(c);
|
|
|
|
}
|
|
|
|
float t = ConvertTempToFahrenheit(c);
|
2021-04-12 15:21:33 +01:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2021-03-28 11:25:30 +01:00
|
|
|
int value = 0;
|
2021-04-12 15:21:33 +01:00
|
|
|
if (HEData.present) {
|
|
|
|
for (uint32_t i = 0; i < HALLEFFECT_SAMPLE_COUNT; i++) {
|
|
|
|
value += hallRead();
|
|
|
|
}
|
|
|
|
value /= HALLEFFECT_SAMPLE_COUNT;
|
2021-03-28 11:25:30 +01:00
|
|
|
}
|
2021-04-12 15:21:33 +01:00
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2021-03-27 14:15:15 +00:00
|
|
|
if (json) {
|
2021-05-23 15:50:17 +01:00
|
|
|
bool temperature_present = (ResponseContains_P(PSTR(D_JSON_TEMPERATURE)));
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"ESP32\":{\"" D_JSON_TEMPERATURE "\":%*_f"), Settings->flag2.temperature_resolution, &t);
|
2021-04-12 15:21:33 +01:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
if (HEData.present) {
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_HALLEFFECT "\":%d"), value);
|
|
|
|
}
|
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
|
|
|
ResponseJsonEnd();
|
2021-03-27 14:15:15 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (0 == TasmotaGlobal.tele_period) {
|
2021-04-12 15:50:04 +01:00
|
|
|
if (!temperature_present) { // Only send if no other sensor already did
|
|
|
|
DomoticzFloatSensor(DZ_TEMP, t);
|
|
|
|
}
|
2021-04-12 15:21:33 +01:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
if (HEData.present) {
|
|
|
|
DomoticzSensor(DZ_COUNT, value);
|
|
|
|
}
|
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2021-03-27 14:15:15 +00:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2021-04-12 15:50:04 +01:00
|
|
|
WSContentSend_Temp("ESP32", t);
|
2021-04-12 15:21:33 +01:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
if (HEData.present) {
|
2021-04-12 15:50:04 +01:00
|
|
|
WSContentSend_P(HTTP_SNS_HALL_EFFECT, "ESP32", value);
|
2021-04-12 15:21:33 +01:00
|
|
|
}
|
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2021-03-27 14:15:15 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2021-07-14 09:57:05 +01:00
|
|
|
bool Xsns127(uint8_t function) {
|
2021-03-27 14:15:15 +00:00
|
|
|
bool result = false;
|
|
|
|
|
2021-04-12 15:21:33 +01:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Esp32SensorShow(1);
|
|
|
|
break;
|
2021-03-27 14:15:15 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2021-04-12 15:21:33 +01:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Esp32SensorShow(0);
|
|
|
|
break;
|
2021-03-27 14:15:15 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2021-07-14 10:39:59 +01:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2021-04-12 15:21:33 +01:00
|
|
|
case FUNC_INIT:
|
|
|
|
Esp32SensorInit();
|
|
|
|
break;
|
2021-07-14 10:39:59 +01:00
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
2021-03-27 14:15:15 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ESP32
|