forked from WLANThermo-nano/WLANThermo_ESP82XX_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_sensor.h
520 lines (406 loc) · 15.1 KB
/
c_sensor.h
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/***************************************************
Copyright (C) 2016 Steffen Ochs
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/>.
ANALOG/DIGITAL-WANDLUNG:
- kleinster digitaler Sprung 1.06 V/1024 = 1.035 mV - eigentlich 1.0V/1024
- Hinweis zur Abweichung: https://github.com/esp8266/Arduino/issues/2672
-> ADC-Messspannung = Digitalwert * 1.035 mV
- Spannungsteiler (47k / 10k) am ADC-Eingang zur
- Transformation von BattMin - BattMax in den Messbereich von 0 - 1.06V
-> Batteriespannung = ADC-Messspannung * (47+10)/10
-> Transformationsvariable Digitalwert-to-Batteriespannung: Battdiv = 1.035 mV * 5.7
HISTORY: Please refer Github History
****************************************************/
// https://github.com/adafruit/Adafruit_HTU21DF_Library/blob/master/Adafruit_HTU21DF.cpp
#define HTU21DF_I2CADDR 0x40
#define HTU21DF_READTEMP 0xE3
#define HTU21DF_READHUM 0xE5
#define HTU21DF_WRITEREG 0xE6
#define HTU21DF_READREG 0xE7
#define HTU21DF_RESET 0xFE
#define TRIG_TEMP_RLS 0xF3 //Triggers a Temperature Measurement. Releases the SCK line (unblocks i2c bus). User must manually wait for completion before grabbing data.
#define TRIG_HUM_RLS 0xF5 //Triggers a Humidity Measurement. Releases the SCK line (unblocks i2c bus). User must manually wait for completion before grabbing data.
class HTU21DF {
private:
bool _exist;
byte _state;
float _temp, _hum;
void wireRead(byte x) {
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(x);
Wire.endTransmission();
//delay(15);
}
uint16_t wireReq() {
Wire.requestFrom(HTU21DF_I2CADDR, 3);
uint16_t h = (Wire.read() << 8) | Wire.read();
Wire.read();
return h;
}
public:
bool exist() {
return _exist;
}
byte getState() {
return _state;
}
float temp() {
return _temp;
}
float hum() {
return _hum;
}
boolean begin(void) {
wireRead(HTU21DF_RESET);
wireRead(HTU21DF_READREG);
Wire.requestFrom(HTU21DF_I2CADDR, 1);
_exist = (Wire.read() == 0x2); // after reset should be 0x2
_state = 0;
return _exist;
}
void trigTemperature() {
wireRead(TRIG_TEMP_RLS);
_state = 1;
}
void trigHumidity() {
wireRead(TRIG_HUM_RLS);
_state = 3;
}
void readTemperature(void) {
//wireRead(HTU21DF_READTEMP); delay(50);
float temp = wireReq();
temp *= 175.72;
temp /= 65536;
temp -= 46.85;
_state = 2;
_temp = temp;
}
float readHumidity(void) {
//wireRead(HTU21DF_READHUM); delay(50);
float hum = wireReq();
hum *= 125;
hum /= 65536;
hum -= 6;
_state = 0;
_hum = hum;
}
};
HTU21DF htu;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialize Sensors
byte set_sensor() {
// Piepser
pinMode(MOSI, OUTPUT);
analogWriteFreq(4000);
if (sys.typk && sys.hwversion == 1) {
// CS notwendig, da nur bei CS HIGH neue Werte im Chip gelesen werden
pinMode(THERMOCOUPLE_CS, OUTPUT);
digitalWrite(THERMOCOUPLE_CS, HIGH);
}
if (htu.begin()) Serial.println("Found HTU21D");
else Serial.println("No HTU21D");
// MAX1161x
byte reg = 0xA0; // A0 = 10100000
// page 14
// 1: setup mode
// SEL2:0 = Reference (Table 6)
// external(1)/internal(0) clock
// unipolar(0)/bipolar(1)
// 0: reset the configuration register to default
// 0: dont't care
Wire.beginTransmission(MAX1161x_ADDRESS);
Wire.write(reg);
byte error = Wire.endTransmission();
return error;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Reading ADC-Channel Average
int get_adc_average (byte ch) {
// Get the average value for the ADC channel (ch) selected.
// MAX11613/15 samples the channel 8 times and returns the average.
// Setup byte required: 0xA0
byte config = 0x21 + (ch << 1); //00100001 + ch // page 15
// 0: config mode
// 01: SCAN = Converts the ch eight times
// 0000: placeholder ch
// 1: single-ended
Wire.beginTransmission(MAX1161x_ADDRESS);
Wire.write(config);
Wire.endTransmission();
Wire.requestFrom(MAX1161x_ADDRESS, 2);
word regdata = (Wire.read() << 8) | Wire.read();
return regdata & 4095;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialize Charge Detection
void set_batdetect(boolean stat) {
if (!stat) pinMode(CHARGEDETECTION, INPUT_PULLDOWN_16);
else pinMode(CHARGEDETECTION, INPUT);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Reading Battery Voltage
void get_Vbat() {
// Digitalwert transformiert in Batteriespannung in mV
int voltage = analogRead(ANALOGREADBATTPIN);
// CHARGE DETECTION
// LOAD COMPLETE SHUTDOWN
// MCP: LOW HIGH HIGH-Z
// curStateNone: LOW HIGH HIGH
// curStatePull: LOW HIGH LOW
// 0 3 1
// Messung bei INPUT_PULLDOWN
set_batdetect(LOW);
bool curStatePull = digitalRead(CHARGEDETECTION);
// Messung bei INPUT
set_batdetect(HIGH);
bool curStateNone = digitalRead(CHARGEDETECTION);
// Ladeanzeige
battery.charge = !curStateNone;
// Standby erkennen
if (voltage < 10) {
sys.stby = true;
//return;
}
else sys.stby = false;
// Transformation Digitalwert in Batteriespannung
voltage = voltage * BATTDIV;
battery.state = curStateNone;
battery.state |= curStatePull<<1;
if (sys.god & (1<<1)) battery.state = 4; // Abschaltung der Erkennung
switch (battery.state) {
case 0: // LOAD
if (battery.setreference != -1 && battery.setreference < 180) { // Referenz setzen
battery.setreference = -1;
setconfig(eSYSTEM,{});
}
break;
case 1: // SHUTDOWN
if (battery.setreference > 0 && battery.voltage > 0 && !sys.stby) {
voltage = 4200;
battery.sim = 4200;
// Runterzählen
if ((millis() - battery.correction) > CORRECTIONTIME) {
battery.setreference -= CORRECTIONTIME/1000;
battery.setreference = constrain(battery.setreference, 0, 180);
setconfig(eSYSTEM,{}); // SPEICHERN
battery.correction = millis();
}
}
if (battery.voltage > 4000) {
if (battery.sim > 4000) {
// Reduktion nach der Aufnahme eines neuen Werts
if (battery.simc > MEDIAN_SIZE-1) { // angepasst an den Median
battery.simc = 0; // Zurücksetzen
battery.sim -= 4; // -1 mV in 2 min bei 88 mA // 1500 mAh / 88 mA = 17h // 550 mV / 17h = 32 mV/h
}
// Aufnahme eines neuen Werts vor der Reduktion
if (battery.simc < MEDIAN_SIZE-1 && battery.sim - battery.voltage > 5) voltage = battery.sim; // 9/10
//else if (battery.simc > 2 && (battery.sim - battery.voltage > 5)) voltage = battery.sim; // //10
//else if (battery.simc > 4 && (battery.sim - battery.voltage > 1)) voltage = battery.sim; // 5/10
} else if (battery.simc > 1) battery.sim = battery.voltage - 1; // Systemstart, etwas warten
} else battery.simc = 0;
break;
case 3: // COMPLETE (vollständig)
if (battery.setreference == -1) { // es wurde geladen
battery.setreference = 180; // Referenzzeit setzen
setconfig(eSYSTEM,{});
} else voltage = 4200; // 100% bei USB-Quelle
break;
case 4: // NO BATTERY
voltage = 4200;
battery.charge = false;
break;
}
// Batteriespannung wird in einen Buffer geschrieben da die gemessene
// Spannung leicht schwankt, aufgrund des aktuellen Energieverbrauchs
// wird die Batteriespannung als Mittel aus mehreren Messungen ausgegeben
// Während der Battery Initalisierung wird nicht in den Buffer geschrieben
if (millis() < BATTERYSTARTUP*2) {
vol_sum = voltage; // Battery Initalisierung
vol_count = 1;
} else {
vol_sum += voltage;
vol_count++;
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Calculate SOC
void cal_soc() {
// mittlere Batteriespannung aus dem Buffer lesen und in Prozent umrechnen
int voltage;
if (battery.state == 1 && millis() > BATTERYSTARTUP) battery.simc++;
if (vol_count > 0) {
if (vol_count == 1) { // Battery Initialiseurnv
if (battery.voltage < vol_sum)
battery.voltage = vol_sum; // beim Start Messung anpassen
} else {
voltage = vol_sum / vol_count;
median_add(voltage);
//battery.voltage = voltage;
battery.voltage = median_average();
}
battery.percentage = ((battery.voltage - battery.min)*100)/(battery.max - battery.min);
// Schwankungen verschiedener Batterien ausgleichen
if (battery.percentage > 100) {
if (battery.charge) battery.percentage = 99;
else battery.percentage = 100;
}
if (millis() > BATTERYSTARTUP) {
IPRINTF("Battery voltage: %umV,\tcharge: %u%%\r\n", battery.voltage, battery.percentage);
}
// Abschaltung des Systems bei <0% Akkuleistung
if (battery.percentage < 0) {
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/3, "LOW BATTERY");
display.drawString(DISPLAY_WIDTH/2, 2*DISPLAY_HEIGHT/3, "PLEASE SWITCH OFF");
display.display();
ESP.deepSleep(0);
delay(100); // notwendig um Prozesse zu beenden
}
vol_sum = 0;
vol_count = 0;
}
// HTU21D
if (htu.exist()) {
switch (htu.getState()) {
case 0: htu.trigTemperature(); break;
case 1: htu.readTemperature();
case 2: htu.trigHumidity(); break;
case 3: htu.readHumidity(); break;
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialize Hardware Alarm
void set_piepser() {
// Hardware-Alarm bereit machen
pinMode(MOSI, OUTPUT);
analogWriteFreq(4000);
//sys.hwalarm = false;
}
void piepserON() {
analogWrite(MOSI,512);
}
void piepserOFF() {
analogWrite(MOSI,0);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Control Hardware Alarm
void controlAlarm(bool action){ // action dient zur Pulsung des Signals
bool setalarm = false;
for (int i=0; i < CHANNELS; i++) {
//if (ch[i].alarm > 0) { // CHANNEL ALARM ENABLED
// CHECK LIMITS
if ((ch[i].temp <= ch[i].max && ch[i].temp >= ch[i].min) || ch[i].temp == INACTIVEVALUE) {
// everything is ok
ch[i].isalarm = false; // no alarm
ch[i].showalarm = 0; // no OLED note
notification.index &= ~(1<<i); // delete channel from index
//notification.limit &= ~(1<<i);
} else if (ch[i].isalarm && ch[i].showalarm > 0) { // Alarm noch nicht quittiert
// do summer alarm
setalarm = true;
// Show Alarm on OLED
if (ch[i].showalarm == 2 && !displayblocked) { // falls noch nicht angezeigt
ch[i].showalarm = 1; // nur noch Summer
question.typ = HARDWAREALARM;
question.con = i;
drawQuestion(i);
}
} else if (!ch[i].isalarm && ch[i].temp != INACTIVEVALUE) {
// first rising limits
ch[i].isalarm = true; // alarm
if (ch[i].alarm == 1 || ch[i].alarm == 3) { // push or all
notification.index &= ~(1<<i);
notification.index |= 1<<i; // Add channel to index
if (ch[i].temp > ch[i].max) {
notification.limit |= 1<<i; // add upper limit
}
else if (ch[i].temp < ch[i].min) {
notification.limit &= ~(1<<i); // add lower limit
}
}
if (ch[i].alarm > 1) { // only if summer
ch[i].showalarm = 2; // show OLED note first time
}
}
//} else { // CHANNEL ALARM DISABLED
// ch[i].isalarm = false;
// ch[i].showalarm = 0;
// notification.index &= ~(1<<i); // delete channel from index
//notification.limit &= ~(1<<i);
//}
}
// Hardware-Alarm-Variable: sys.hwalarm
if (setalarm && action) {
piepserON();
}
else {
piepserOFF();
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Reading Ampere IC
unsigned long ampere_sum = 0;
unsigned long ampere_con = 0;
float ampere = 0;
unsigned long ampere_time;
void ampere_control() {
ampere_sum += ((get_adc_average(5) * 2.048 )/ 4096.0)*1000.0;
ampere_con++;
if (millis()-ampere_time > 10*60*1000) {
ampere_time = millis();
ampere = ampere_sum/ampere_con;
ampere_con = 0;
ampere_sum = 0;
} else if (millis() < 120000) {
ampere = ampere_sum/ampere_con;
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Reading Temperature KTYPE
double get_thermocouple(bool internal) {
long dd = 0;
// Communication per I2C Pins but with CS
digitalWrite(THERMOCOUPLE_CS, LOW); // START
for (uint8_t i=32; i; i--){
dd = dd <<1;
if (twi_read_bit()) dd |= 0x01;
}
digitalWrite(THERMOCOUPLE_CS, HIGH); // END
// Invalid Measurement
if (dd & 0x7) { // Abfrage von D0/D1/D2 (Fault)
return INACTIVEVALUE;
}
if (internal) {
// Internal Reference
double ii = (dd >> 4) & 0x7FF; // Abfrage D4-D14
ii *= 0.0625;
if ((dd >> 4) & 0x800) ii *= -1; // Abfrage D15 (Vorzeichen)
return ii;
}
// Temperatur
if (dd & 0x80000000) { // Abfrage D31 (Vorzeichen)
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
dd = 0xFFFFC000 | ((dd >> 18) & 0x00003FFFF);
}
else {
// Positive value, just drop the lower 18 bits.
dd >>= 18;
}
// Temperature in Celsius
double vv = dd;
vv *= 0.25;
return vv;
}