-
Notifications
You must be signed in to change notification settings - Fork 40
/
Sensors.ino
359 lines (340 loc) · 12.8 KB
/
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// ----------------- Аналоговый вход A0
void initA0() {
static uint16_t t = readArgsInt(); //время опроса датчика
static uint8_t averageFactor = readArgsInt();
if (t < 500) t = 1000;
if (averageFactor == 0) averageFactor = 1023;
analogRead(A0);
sendStatus(stateA0S, analogRead(A0));
sendOptions(alarmA0S, 0);
String alarmSet = "ALARM " + stateA0S + " " + highalarmA0S + " " + lowalarmA0S;
sCmd.readStr(alarmSet);
//alarmLoad(stateA0S, highalarmA0S, lowalarmA0S);
ts.add(tA0, t, [&](void*) {
uint32_t a = 0;
for (uint8_t i = 1; i <= 10; i++) {
a += analogRead(A0);
}
a = a / 10;
sendStatus(stateA0S, a);
alarmTest(stateA0S, highalarmA0S, lowalarmA0S, alarmA0S);
}, nullptr, true);
modulesReg("A0");
}
// ----------------- OneWire -------------------------------------------------------------------------------
void initOneWire() {
uint8_t pin = readArgsInt();
pin = pinTest(pin, HIGH);
static uint16_t t = readArgsInt();
static uint8_t averageFactor = readArgsInt();
if (t < 750) t = 1000;
//Serial.println(t);
//Serial.println(pin);
oneWire = new OneWire(pin);
sensors.setOneWire(oneWire);
sensors.begin();
byte num = sensors.getDS18Count();
//Serial.println(num);
for (byte i = 0; i < num; i++) {
String numS = (String)(i + 1);
sendStatus(temperatureS + numS, (String)sensors.getTempCByIndex(i));
//Serial.println();
sendOptions(alarmtempS + numS, 0);
sendOptions(highalarmtempS + numS, 0);
sendOptions(lowalarmtempS + numS, 0);
alarmLoad(temperatureS + numS, highalarmtempS + numS, lowalarmtempS + numS);
modulesReg(temperatureS + numS);
}
sendOptions(temperatureS + "num", num);
ts.add(tDS, t, [&](void*) {
// static float oldTemp = 0;
float temp = 0;
sensors.requestTemperatures();
for (byte i = 0; i < sensors.getDS18Count(); i++) {
temp = sensors.getTempCByIndex(i);
String num;
num = (String)(i + 1);
sendStatus(temperatureS + num, (String)temp);
alarmTest(temperatureS + num, highalarmtempS + num, lowalarmtempS + num, alarmtempS + num);
}
}, nullptr, true);
}
// ----------------- DHT
void initDHT() {
uint8_t pin = readArgsInt();
pin = pinTest(pin, HIGH);
dht.setup(pin);
delay(1000);
static uint16_t t = readArgsInt();
static uint16_t test = dht.getMinimumSamplingPeriod();
if (t < test) t = test;
//Serial.println(t);
String temp;
temp += dht.getTemperature();
// Serial.println(temp);
if (temp != "nan") {
sendStatus(temperatureS, dht.getTemperature());
sendOptions(alarmtempS, 0);
alarmLoad(temperatureS, highalarmtempS, lowalarmtempS);
sendStatus(humidityS, dht.getHumidity());
sendOptions(alarmhumS, 0);
alarmLoad(humidityS, highalarmhumS, lowalarmhumS);
ts.add(tDHT, test, [&](void*) {
sendStatus(temperatureS, dht.getTemperature());
sendStatus(humidityS, dht.getHumidity());
alarmTest(temperatureS, highalarmtempS, lowalarmtempS, alarmtempS);
alarmTest(humidityS, highalarmhumS, lowalarmhumS, alarmhumS);
}, nullptr, true);
modulesReg(temperatureS);
modulesReg(humidityS);
}
}
// ----------------- Si7021 --------------------------------------------------------------------
#ifdef Si7021
void initSi7021() {
if (sensor_Si7021.begin()) {
sendStatus(temperatureS, sensor_Si7021.readTemperature());
sendOptions(alarmtempS, 0);
alarmLoad(temperatureS, highalarmtempS, lowalarmtempS);
sendStatus(humidityS, sensor_Si7021.readHumidity());
sendOptions(alarmhumS, 0);
alarmLoad(humidityS, highalarmhumS, lowalarmhumS);
ts.add(tSI, 1000, [&](void*) {
sendStatus(temperatureS, sensor_Si7021.readTemperature());
sendStatus(humidityS, sensor_Si7021.readHumidity());
alarmTest(temperatureS, highalarmtempS, lowalarmtempS, alarmtempS);
alarmTest(humidityS, highalarmhumS, lowalarmhumS, alarmhumS);
}, nullptr, true);
modulesReg(temperatureS);
modulesReg(humidityS);
}
}
#endif
// -----------------------Данных уровней активных модулей --------------------------------------------
void alarmLoadModules() {
String modulesN = selectToMarker(modules, "]");
modulesN = deleteBeforeDelimiter(modulesN, "[");
modulesN += ",";
modulesN.replace("\"", emptyS);
//Serial.println(modulesN);
//"upgrade","relay1","ntp","ddns","mqtt","analog"
do {
String m = selectToMarker(modulesN, ",");
//Serial.println(m);
String alarmSet = "ALARM ";
if (m == "analog") alarmSet += stateA0S + " " + highalarmA0S + " " + lowalarmA0S;
if (m == temperatureS) alarmSet += temperatureS + " " + highalarmtempS + " " + lowalarmtempS;
if (m == humidityS) alarmSet += humidityS + " " + highalarmhumS + " " + lowalarmhumS;
#ifdef POW
if (m == "pow") alarmSet += ActivePowerWS + " " + highalarmpowS + " " + lowalarmpowS;
#endif
modulesN = deleteBeforeDelimiter(modulesN, ",");
} while (modulesN != emptyS);
}
// ----------------------- Загрузка данных уровней сработки ------------------------------------------
// Имя параметра теста и имена границ
void alarmLoad(String sName, String high, String low) {
sendOptionsF(high, 0);
sendOptionsF(low, 0);
String configSensor = readFile(ScenaryS, 4096);
configSensor.replace("\r\n", "\n");
configSensor.replace("\n\n", "\n");
configSensor += "\n";
do {
String test = selectToMarker(configSensor, "\n");
// if temperature1 > 26.50
String del = "if " + sName + " >";
if (test.indexOf(del) != -1) {
// Serial.println(test);
test.replace(del, emptyS);
sendOptionsF(high, test.toFloat());
}
del = "if " + sName + " <";
if (test.indexOf(del) != -1) {
// Serial.println(test);
test.replace(del, emptyS);
sendOptionsF(low, test.toFloat());
}
configSensor = deleteBeforeDelimiter(configSensor, "\n"); //Откидываем обработанную строку
configSensor = deleteBeforeDelimiterTo(configSensor, "if"); // откидываем все до следующего if
} while (configSensor.length() != 0);
}
// ------------------------------- Проверка уровней ------------------------------------------------
// Текущее значение сенсора уровни признак датчика
void alarmTest(String value, String high, String low, String sAlarm ) {
if (getOptionsFloat(high) != 0 || getOptionsFloat(low) != 0) { // нужно добавить флаг остановки теста
//Serial.println("test");
if (getStatusFloat(value) > getOptionsFloat(high) && getOptionsInt(sAlarm) == LOW) {
sendOptions(sAlarm, HIGH);
//Serial.println(getStatusFloat(value));
// Serial.print("up<");
// Serial.println(getStatusFloat(value));
flag = sendStatus(value, getStatusFloat(value));
}
if (getStatusFloat(value) < getOptionsFloat(low) && getOptionsInt(sAlarm) == HIGH) {
sendOptions(sAlarm, LOW);
// Serial.print("down>");
// Serial.println(getStatusFloat(value));
flag = sendStatus(value, getStatusFloat(value));
}
}
}
// ----------------------Приемник ИK
void irReceived() {
byte pin = readArgsInt();
pin = pinTest(pin);
irReceiver = new IRrecv(pin); // Create a new IRrecv object. Change to what ever pin you need etc.
irReceiver->enableIRIn(); // Start the receiver
// задача опрашивать IR код
ts.add(tIR, 100, [&](void*) {
handleIrReceiv();
}, nullptr, true);
sendStatus(irReceivedS, "ffffffff");
modulesReg(irReceivedS);
}
void handleIrReceiv() {
if (irReceiver->decode(&results)) {
//serialPrintUint64(results.value, HEX);
//uint64ToString(input, base)
//Serial.println(emptyS);
dump(&results);
flag = sendStatus(irReceivedS, uint64ToString(results.value,HEX));
irReceiver->resume(); // Continue looking for IR codes after your finished dealing with the data.
}
}
void dump(decode_results *results) {
uint16_t count = results->rawlen;
//Serial.println(count);
sendOptions(irDecodeTypeS, results->decode_type);
}
// ----------------------Приемник на 433мГ
void rfReceived() {
byte pin = readArgsInt();
pin = pinTest(pin);
mySwitch.enableReceive(pin);
pinMode(pin, INPUT);
// задача опрашивать RC код
//ts.add(tRC, 5, [&](void*) {
// handleRfReceiv();
//}, nullptr, true);
sendStatus(rfReceivedS, 0);
sendOptions(rfBitS, 0);
sendOptions(rfProtocolS, 0);
modulesReg(rfReceivedS);
}
void handleRfReceiv() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
sendStatus(rfReceivedS, 0);
sendOptions(rfBitS, 0);
sendOptions(rfProtocolS, 0);
} else {
uint32_t temp = mySwitch.getReceivedValue() ;
//Serial.println(temp);
flag = sendStatus(rfReceivedS, temp);
temp = mySwitch.getReceivedBitlength();
//Serial.println(temp);
sendOptions(rfBitS, temp);
temp = mySwitch.getReceivedProtocol();
//Serial.println(temp);
sendOptions(rfProtocolS, temp);
}
mySwitch.resetAvailable();
}
}
// ----------------- Кнопка
void initTach() {
uint8_t pin = readArgsInt(); // первый аргумент pin
pin = pinTest(pin, true);
String num = readArgsString(); // второй аргумент прификс реле 0 1 2
uint16_t bDelay = readArgsInt(); // третий время нажатия
sendStatus(stateTachS + num, 0);
buttons[num.toInt()].attach(pin);
buttons[num.toInt()].interval(bDelay);
but[num.toInt()] = true;
boolean inv = readArgsInt(); // четвертый аргумент инверсия входа
sendOptions(invTachS + num, inv);
modulesReg(tachS + num);
}
void handleButtons() {
static uint8_t num = 0;
String numS = String(num, DEC);
if (but[num]) {
buttons[num].update();
if (buttons[num].fell()) {
//Serial.print("Tach on ");
//Serial.println(getStatusInt(stateTachS + numS));
flag = sendStatus(stateTachS + numS, !getOptionsInt(invTachS + numS));
}
if (buttons[num].rose()) {
//Serial.print("Tach off ");
//Serial.println(getStatusInt(stateTachS + numS));
flag = sendStatus(stateTachS + numS, getOptionsInt(invTachS + numS));
}
}
num++;
if (num == NUM_BUTTONS) num = 0;
}
#ifdef POW
// Импульс 1 Гц на выводе CF1 означает 15 мА или 0,5 В RMS в зависимости от значения в выводе SEL
void ICACHE_RAM_ATTR hlw8012_cf1_interrupt() {
hlw8012.cf1_interrupt();
}
// Импульс 1 Гц на выводе CF составляет около 12 Вт RMS
void ICACHE_RAM_ATTR hlw8012_cf_interrupt() {
hlw8012.cf_interrupt();
}
// ---------------------- Измеритель мощьности POW
void initHLW8012() {
static uint16_t t = readArgsInt();
String temp = readArgsString();
byte pinCF;
if (temp == emptyS) pinCF = pinTest(14);
else pinCF = pinTest(temp.toInt());
temp = readArgsString();
byte pinCF1;
if (temp == emptyS) pinCF1 = pinTest(13);
else pinCF1 = pinTest(temp.toInt());
temp = readArgsString();
byte pinSEL;
if (temp == emptyS) pinSEL = pinTest(5);
else pinSEL = pinTest(temp.toInt());
hlw8012.begin(pinCF, pinCF1, pinSEL, HIGH, true);
hlw8012.setResistors(CURRENT_RESISTOR, VOLTAGE_RESISTOR_UPSTREAM, VOLTAGE_RESISTOR_DOWNSTREAM);
modulesReg("pow");
attachInterrupt(pinCF1, hlw8012_cf1_interrupt, CHANGE);
attachInterrupt(pinCF, hlw8012_cf_interrupt, CHANGE);
sendStatus(ActivePowerWS, hlw8012.getActivePower());
sendOptions(alarmpowS, 0);
alarmLoad(ActivePowerWS, highalarmpowS, lowalarmpowS);
ts.add(tPOW, t, [&](void*) {
sendStatus(ActivePowerWS, hlw8012.getActivePower());
sendOptions(voltagevS, hlw8012.getVoltage());
sendOptions(currentaS, hlw8012.getCurrent());
sendOptions(apparentpowervaS, hlw8012.getApparentPower());
sendOptions(powerfactorS, hlw8012.getPowerFactor());
sendOptions(aggenergywsS, hlw8012.getEnergy());
alarmTest(ActivePowerWS, highalarmpowS, lowalarmpowS, alarmpowS);
}, nullptr, true);
HTTP.on("/pow.json", HTTP_GET, []() {
String data = graf3(getStatusFloat(ActivePowerWS), getOptionsFloat(highalarmpowS), getOptionsFloat(lowalarmpowS), 10, t, "low:0");
httpOkJson(data);
});
//calibrate();
}
void calibrate() {
// Let some time to register values
unsigned long timeout = millis();
while ((millis() - timeout) < 10000) {
delay(1);
}
// Calibrate using a 60W bulb (pure resistive) on a 230V line
hlw8012.expectedActivePower(60.0);
hlw8012.expectedVoltage(220.0);
hlw8012.expectedCurrent(60.0 / 220.0);
sendOptions("current", hlw8012.getCurrentMultiplier());
sendOptions("voltage", hlw8012.getVoltageMultiplier());
sendOptions("power", hlw8012.getPowerMultiplier());
}
#endif