-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
xsns_127_esp32_sensors.ino
150 lines (126 loc) · 4.42 KB
/
xsns_127_esp32_sensors.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
xsns_127_esp32_sensors.ino - ESP32 Temperature and Hall Effect sensor for Tasmota
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
#ifdef USE_ESP32_SENSORS
/*********************************************************************************************\
* ESP32 CPU Temperature and optional Hall Effect sensor
*
* To allow for not updating the global temperature by the ESP32 temperature sensor this
* driver needs to be the highest numbered driver (currently 127)
*
* ESP32 internal Hall Effect sensor connected to both GPIO36 and GPIO39
* To enable set
* GPIO36 as HallEffect 1
* GPIO39 as HallEffect 2
\*********************************************************************************************/
#define XSNS_127 127
#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 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)))) {
HEData.present = true;
hallRead();
}
}
}
#endif // CONFIG_IDF_TARGET_ESP32
void Esp32SensorShow(bool json) {
bool json_end = false;
if (Settings->flag6.use_esp32_temperature) { // SetOption146 - (ESP32) Show ESP32 internal temperature sensor
float c = CpuTemperature(); // in Celsius
float t = ConvertTempToFahrenheit(c);
if (json) {
if (!ResponseContains_P(PSTR(D_JSON_TEMPERATURE))) {
UpdateGlobalTemperature(c);
}
ResponseAppend_P(PSTR(",\"ESP32\":{\"" D_JSON_TEMPERATURE "\":%*_f"), Settings->flag2.temperature_resolution, &t);
json_end = true;
#ifdef USE_DOMOTICZ
// Instead of below code use a rule like 'on tele-esp32#temperature do dzsend1 9988,%value% endon'
// where 9988 is the domoticz sensor Idx
// if (0 == TasmotaGlobal.tele_period) {
// if (!ResponseContains_P(PSTR(D_JSON_TEMPERATURE))) { // Only send if no other sensor already did
// DomoticzFloatSensor(DZ_TEMP, t);
// }
// }
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_Temp("ESP32", t);
#endif // USE_WEBSERVER
}
}
#if CONFIG_IDF_TARGET_ESP32
if (HEData.present) {
int value = 0;
for (uint32_t i = 0; i < HALLEFFECT_SAMPLE_COUNT; i++) {
value += hallRead();
}
value /= HALLEFFECT_SAMPLE_COUNT;
if (json) {
if (!json_end) {
ResponseAppend_P(PSTR(",\"ESP32\":{"));
} else {
ResponseAppend_P(PSTR(","));
}
ResponseAppend_P(PSTR("\"" D_JSON_HALLEFFECT "\":%d"), value);
json_end = true;
#ifdef USE_DOMOTICZ
// Instead of below code use a rule like 'on tele-esp32#halleffect do dzsend1 9988,%value% endon'
// where 9988 is the domoticz sensor Idx
// if (0 == TasmotaGlobal.tele_period) {
// DomoticzSensor(DZ_COUNT, value);
// }
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_P(HTTP_SNS_HALL_EFFECT, "ESP32", value);
#endif // USE_WEBSERVER
}
}
#endif // CONFIG_IDF_TARGET_ESP32
if (json_end) {
ResponseJsonEnd();
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xsns127(uint32_t function) {
bool result = false;
switch (function) {
case FUNC_JSON_APPEND:
Esp32SensorShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Esp32SensorShow(0);
break;
#endif // USE_WEBSERVER
#if CONFIG_IDF_TARGET_ESP32
case FUNC_INIT:
Esp32SensorInit();
break;
#endif // CONFIG_IDF_TARGET_ESP32
}
return result;
}
#endif // USE_ESP32_SENSORS
#endif // ESP32