-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBME680_UniversalSensor.ino
1801 lines (1611 loc) · 56.6 KB
/
BME680_UniversalSensor.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 Robert Bosch. All Rights Reserved.
*
* Disclaimer
*
* Common:
* Bosch Sensortec products are developed for the consumer goods industry. They may only be used
* within the parameters of the respective valid product data sheet. Bosch Sensortec products are
* provided with the express understanding that there is no warranty of fitness for a particular purpose.
* They are not fit for use in life-sustaining, safety or security sensitive systems or any system or device
* that may lead to bodily harm or property damage if the system or device malfunctions. In addition,
* Bosch Sensortec products are not fit for use in products which interact with motor vehicle systems.
* The resale and/or use of products are at the purchasers own risk and his own responsibility. The
* examination of fitness for the intended use is the sole responsibility of the Purchaser.
*
* The purchaser shall indemnify Bosch Sensortec from all third party claims, including any claims for
* incidental, or consequential damages, arising from any product use not covered by the parameters of
* the respective valid product data sheet or not approved by Bosch Sensortec and reimburse Bosch
* Sensortec for all costs in connection with such claims.
*
* The purchaser must monitor the market for the purchased products, particularly with regard to
* product safety and inform Bosch Sensortec without delay of all security relevant incidents.
*
* Engineering Samples are marked with an asterisk (*) or (e). Samples may vary from the valid
* technical specifications of the product series. They are therefore not intended or fit for resale to third
* parties or for use in end products. Their sole purpose is internal client testing. The testing of an
* engineering sample may in no way replace the testing of a product series. Bosch Sensortec
* assumes no liability for the use of engineering samples. By accepting the engineering samples, the
* Purchaser agrees to indemnify Bosch Sensortec from all claims arising from the use of engineering
* samples.
*
* Special:
* This software module (hereinafter called "Software") and any information on application-sheets
* (hereinafter called "Information") is provided free of charge for the sole purpose to support your
* application work. The Software and Information is subject to the following terms and conditions:
*
* The Software is specifically designed for the exclusive use for Bosch Sensortec products by
* personnel who have special experience and training. Do not use this Software if you do not have the
* proper experience or training.
*
* This Software package is provided `` as is `` and without any expressed or implied warranties,
* including without limitation, the implied warranties of merchantability and fitness for a particular
* purpose.
*
* Bosch Sensortec and their representatives and agents deny any liability for the functional impairment
* of this Software in terms of fitness, performance and safety. Bosch Sensortec and their
* representatives and agents shall not be liable for any direct or indirect damages or injury, except as
* otherwise stipulated in mandatory applicable law.
*
* The Information provided is believed to be accurate and reliable. Bosch Sensortec assumes no
* responsibility for the consequences of use of such Information nor for any infringement of patents or
* other rights of third parties which may result from its use. No license is granted by implication or
* otherwise under any patent or patent rights of Bosch. Specifications mentioned in the Information are
* subject to change without notice.
*
* It is not allowed to deliver the source code of the Software to any third party without permission of
* Bosch Sensortec.
*
***********************************************************************************************************************
*
* Wireless sensor for measuring temperature, humidity, airpressure, lightlevel and airquality.
* Hardware: NodeMCU V1.0, WEMOS D1 mini, Generic STM32F103C8(e.g.BluePill) or Maple mini + BME680
* Optional: RFM69CW, BH1750 and 128x64 OLED Display(SH1106 or SSD1306)
* If you use a RFM69CW, protokoll is UniversalSensor, frequency is 868.3 MHz
* New since V3.3:
* MQTT is possible on ESP8266 boards, therefore we use WiFi. Configuration for this is in file secrets.h .
* CO2e and bVOC works now !
*
* V1.3 10.11.2017, T.Hirte
* V1.4 include BH1750 light sensor, 17.11.2017, T.Hirte
* V1.5 - changed to Bosch Sensortec Software from 17.11.2017, 01.12.2017, T.Hirte
* - rounding of values for OLED Display is done by "display.print()", 05.12.2017, T.Hirte
* - changed display of gas resistance on OLED to 123.45kOhm, 18.12.2017, T.Hirte
* - you can now deactivate vcc measuring, BSEC version is shown at startup,
* crc-errors at receiving end: SPI clock reduced to 400kHz, now it's ok,
* correct hardware wiring (IRQ of RFM69 is not needed), 28.12.2017, T.Hirte
* V1.6 UniversalSensor
* Added Altitude configuration
* Get voltage from ESP.getVcc() - does not really work, so used the old way ;o)
* Changed VERSION to float to be able to transmit it
* V1.7 updated to latest changes of the CC-Sensor version, anything (except the BME680)
* can be enabled/disabled, changed to internal vcc measuring, works on NodeMCU and D1 mini...
* V1.8 changed crc calculation to crc16, by HCS
* V1.9 BME680 I2C adress is now autodetected ! NodeID is showing as hex value.
* Definition of not needed irq for RFMxx removed.
* Compiled with esp8266 V2.4.0 -> changed the vcc correction factor for exakt voltage display.
* V2.0 Lightsensor and OLED display are now autodetected.
* BH1750 Lib's changed for this to work !
* RFMxx Lib changed, Soft-SPI selectable in RFMxx.h, 13.01.2018, T.Hirte
* V2.1 RFM69 is now autodetected, if not present, output only on serial port.
* onboard led is flashing 3 times if setup completes successfully, code improvements, 15.01.2018, T.Hirte
* V2.2 Debug mode and Soft-SPI is now selectable from the define's, no longer need to change RFMxx.h ;o)
* Debug messages added, code improvements, 20.01.2018, T.Hirte
* V3.0 First version for ESP8266 (NodeMCU or Wemos D1 mini) and STM32F1 (Maple mini, BluePill)!
* In BSEC routines: WireEndTransmission for STM32xx fixed,
* in RFMxx.cpp: SPI speed for STM32xx fixed, 19.01.2018, T.Hirte
* Input of NodeID, Altitude and Temperature Offset at first start (serial terminal), values stored in EEPROM.
* Delete settings in EEPROM at startup possible, you have 5 seconds to say "Y", otherwise the stored
* settings will be used. 12.02.2018, T.Hirte
* Autodetect of OLED I2C address moved to Adafruit lib (Adafruit_SSD1306.cpp + Adafruit_SSD1306.h),
* code improvements, 14.02.2018, T.Hirte
*
* V3.1 Update to BSEC-software V1.4.7.1, integrate changes from juergs (error if HAS_OLED = false, switch off wifi).
* Compiled with Arduino V1.8.7 (used ESP8266 package: V2.5.0 beta2) ;o) 27.12.2018, T.Hirte
*
* V3.2 Non public version for McArthur, iaq display with WS2812 NeoPixel and data transmission to Thingspeak.
* New sensor outputs: bVOC and CO2e in bsec_integration.c activated.
* 31.08.2019
*
* V3.3 Update to BSEC-software V1.4.7.4, compiled with Arduino V1.8.9, ESP8266 package: V2.5.2,
* New sensor outputs: bVOC and CO2e in bsec_integration.c activated.
* MQTT over WiFi on ESP8266 based clients, sends messages every 30 seconds,
* All login data (WiFi, MQTT) stored in secrets.h !
* update hardware setup comments, in PubSubClient.h: MQTT_KEEPALIVE changed to 35,
* Adafruit_SSD1306.cpp code improvements - 15.09.2019, T.Hirte
*
* V3.4 new option "homekit" - send iaq as value from 0 to 5, see config.h
* 24.09.2019, T.Hirte
*
***********************************************************************************************************************
******************
* HARDWARE SETUP *
******************
ESP8266:
D1 mini NodeMCU
+--\/--+ +--\/--+
RST | | TX A0 | | D0 int.LED
A0 | | RX RSV | | D1 SCL -->
D0 | | D1 SCL --> RSV | | D2 SDA <->
<--- SCK D5 | | D2 SDA <-> SD3 | | D3
<-- MISO D6 | | D3 SD2 | | D4
--> MOSI D7 | | D4 int.LED SD1 | | 3V3
<--- NSS D8 | | GND CMD | | GND
<-- 3.3V | | 5V <-- 5V SD0 | | D5 SCK ---> BME680 RFM69CW
+------+ CLK | | D6 MISO <-- +------+ +-------+
GND | | D7 MOSI --> 3.3V | 0x76 | 3.3V | | GND
3V3 | | D8 NSS ---> <-> SDA | or | --> MOSI | | NSS <--
EN | | RX --> SCL | 0x77 | <-- MISO | |
RST | | TX GND | | --> SCK | |
GND | | GND --> +------+ +-------+
5V --> Vin | | 3V3 -->
+------+
STM32F1:
mapleMini BluePill
+--\/--+ +--\/--+
<-- 3.3V | | VCC 3.3V PB12 | | GND BH1750 SH1106 OLED
<--- GND | | GND PB13 | | GND --> +------+ +-------+
BOOT0 | | VBAT PB14 | | 3V3 --> 3.3V | 0x23 | 3.3V | 0x3C |
<-> SDA PB_7 | | PC_13 PB15 | | NRST GND | or | GND | or |
<-- SCL PB_6 | | PC_14 PA8 | | PB11 --> SCL | 0x5C | --> SCL | 0x3D |
PB_5 | | PC_15 Tx1 PA9 | | PB10 <-> SDA | | <-> SDA | |
PB_4 | | RESET Rx1 PA10 | | PB1 +------+ +-------+
PB_3 | | PA_0 USB- PA11 | | PB0 A0
PA_15 | | PA_1 USB+ PA12 | | PA7 A1/MOSI1 -->
PA_14 | | PA_2 PA15 | | PA6 A2/MISO1 <--
PA_13 | | PA_3 PB3 | | PA5 A3/SCK1 -->
PA_12 | | PA_4 NSS --> PB4 | | PA4 A4/NSS1 -->
PA_11 | | PA_5 SCK --> PB5 | | PA3 A5
PA_10 | | PA_6 MISO <-- <-- SCL PB6 | | PA2 A6
PA_9 | | PA_7 MOSI --> <-> SDA PB7 | | PA1 A7
PA_8 | | PB_0 PB8 | | PA0 A8
PB_15 | | PB_2 int.LED PB9 | | PC15
PB_14 | | PB_10 5V | | PC14
PB_13 | | PB_11 GND | | PC13 int.LED
PB_12 | | + 5V 3V3 | | VBAT
+------+ +------+
BluePill-Board
==============
http://wiki.stm32duino.com/index.php?title=Blue_Pill
Maple_Mini-Board
================
https://wolfgangklenk.wordpress.com/2017/11/05/indoor-air-quality-iaq-measurement-with-bosch-bme680-and-stm32f103c8t6/
************************************************************************************************************************/
#include "config.h"
#include <Wire.h>
#include "bsec_integration.h"
#include "UniversalSensor.h"
#include "HandleEeprom.h"
#include "string"
#if USE_MQTT
#ifndef ESP8266
#error "MQTT client only for ESP8266 !"
#endif
#endif
#if !USE_MQTT
#if USE_HOMEKIT
#error "For homekit option you must enable MQTT protocol !"
#endif
#endif
#ifdef ESP8266
#include "secrets.h"
#include <ESP8266WiFi.h>
byte my_WiFi_Mode = 0;
WiFiServer server(80);
WiFiClient espClient;
#define MAX_PACKAGE_SIZE 2048
char HTML_String[5000];
char HTTP_Header[150];
#endif
#if USE_MQTT
#include "PubSubClient.h"
PubSubClient client(espClient);
uint8_t mqtt_counter;
uint8_t iaq_expr;
char temp_topic[50];
char pres_topic[50];
char hum_topic[50];
char iaq_topic[50];
char iaq_accuracy_topic[50];
char co2_topic[50];
char voc_topic[50];
char light_topic[50];
char rssi_topic[50];
#endif
#if HAS_RFM69
#include "RFMxx.h"
#include "SensorBase.h"
#endif
#if HAS_OLED
#include <Adafruit_GFX.h>
#include "Adafruit_SSD1306.h"
#endif
#if HAS_LIGHTSENSOR
#include "AS_BH1750.h"
#endif
uint8_t NODEID;
float TEMPOFFSET;
float ALTITUDE;
HandleEeprom eeprom;
//RFM69
#if HAS_RFM69
unsigned long DATA_RATE = 17241ul; //default data rate (for transmit on RFM69)
unsigned long INITIAL_FREQ = 868300; //default frequency in kHz (5 kHz steps, 860480 ... 879515)
bool RFM69 = false; //if RFM69 is not detected
uint8_t rfm_interval = 10; //time to wait between data transmissions (10: 10 * 3sec = 30sec)
uint8_t rfm_counter;
//RFM69, PIN-config for SPI:
#ifdef ESP8266
#define RFM_SS D8 // SS pin -> RFM69 (NSS) for both Soft- and HW-SPI
#define RFM_MISO D6 //MISO pin <- RFM69 (MOSI) only used by soft spi
#define RFM_MOSI D7 //MOSI pin -> RFM69 (MISO) only used by soft spi
#define RFM_SCK D5 // SCK pin -> RFM69 (SCK) only used by soft spi
#elif defined(__STM32F1__)
#define RFM_SS PA4 // SS pin -> RFM69 (NSS) for both Soft- and HW-SPI
#define RFM_SCK PA5 // SCK pin -> RFM69 (SCK) only used by soft spi
#define RFM_MISO PA6 //MISO pin <- RFM69 (MOSI) only used by soft spi
#define RFM_MOSI PA7 //MOSI pin -> RFM69 (MISO) only used by soft spi
#endif
RFMxx rfm(RFM_MOSI, RFM_MISO, RFM_SCK, RFM_SS, SOFT_SPI);
#endif
//OLED display
#if HAS_OLED
bool OLED = false; //if display not detected
Adafruit_SSD1306 display;
#endif
//Lightsensor
#if HAS_LIGHTSENSOR
bool BH1750 = false; //if BH1750 not detected
AS_BH1750 bh1750;
#endif
#ifdef ESP8266
#if VCC_MEASURE
ADC_MODE(ADC_VCC); //esp internal vcc measuring
#endif
#endif
//Webserver
#ifdef ESP8266
#define ACTION_OK 1
#define ACTION_NOTOK 2
#define ACTION_SET_MQTT 3
#define ACTION_SET_WIFI 4
#define ACTION_LIES_AUSWAHL 5
int action;
// checkboxen
char devices_tab[6][7] = {"OLED ", "RFM69 ", "BH1750", "MQTT ", "VCC ", "DEBUG "};
byte devices = 0;
char tmp_string[20];
#endif
/***********************************************************************************************************************/
static void blink (byte pin, byte n = 3, int del = 50)
{
for (byte i = 0; i < n; ++i)
{
digitalWrite(pin, LOW);
delay(del);
digitalWrite(pin, HIGH);
delay(del);
}
}
/*****************************************************/
void dim_display (byte brightness)
{
#if HAS_OLED
if (OLED)
{
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(brightness);
}
#endif
}
/*****************************************************/
#if USE_MQTT
void mqtt_reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT server ... ");
if (client.connect(mqtt_clientid,mqtt_user,mqtt_password)) {
Serial.println("connected");
blink(LEDpin,1,50);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
blink(LEDpin,4,250);
}
}
}
#endif
/*****************************************************/
#ifdef ESP8266
void WiFi_Start_STA() {
unsigned long timeout;
WiFi.mode(WIFI_STA); // Workstation
Serial.println();
Serial.print("Connecting to ");
Serial.print(wifi_ssid);
Serial.print(" ");
WiFi.begin(wifi_ssid, wifi_password);
timeout = millis() + 10000L;
while (WiFi.status() != WL_CONNECTED && millis() < timeout) {
Serial.print(".");
blink(LEDpin,2,50);
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
server.begin();
my_WiFi_Mode = WIFI_STA;
blink(LEDpin,1,100);
Serial.println(" connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
uint8_t macAddr[6];
WiFi.macAddress(macAddr);
Serial.printf("MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
}
else {
WiFi.mode(WIFI_OFF);
Serial.println(" failed");
}
Serial.println();
}
/*****************************************************/
void WiFi_Start_AP() {
WiFi.mode(WIFI_AP); // Accesspoint
WiFi.softAP(ssid_ap, password_ap);
server.begin();
IPAddress myIP = WiFi.softAPIP();
my_WiFi_Mode = WIFI_AP;
Serial.print("Accesspoint started, Name: ");
Serial.print(ssid_ap);
Serial.print( ", IP address: ");
Serial.println(myIP);
}
/*****************************************************/
void WiFi_Traffic() {
char my_char;
int htmlPtr = 0;
int myIndex;
unsigned long my_timeout;
// Check if a client has connected
espClient = server.available();
if (!espClient) {
return;
}
my_timeout = millis() + 250L;
while (!espClient.available() && (millis() < my_timeout) ) delay(10);
delay(10);
if (millis() > my_timeout) {
return;
}
//---------------------------------------------------
htmlPtr = 0;
my_char = 0;
while (espClient.available() && my_char != '\r') {
my_char = espClient.read();
HTML_String[htmlPtr++] = my_char;
}
espClient.flush();
HTML_String[htmlPtr] = 0;
#if DEBUG
exhibit ("Request : ", HTML_String);
#endif
if (Find_Start ("/?", HTML_String) < 0 && Find_Start ("GET / HTTP", HTML_String) < 0 ) {
send_not_found();
return;
}
//---------------------------------------------------
// Benutzereingaben einlesen und verarbeiten
//---------------------------------------------------
action = Pick_Parameter_Zahl("ACTION=", HTML_String);
// wifi-ssid und wifi-password
if ( action == ACTION_SET_WIFI) {
myIndex = Find_End("wifi_ssid=", HTML_String);
if (myIndex >= 0) {
Pick_Text(wifi_ssid, &HTML_String[myIndex], 32);
#if DEBUG
exhibit ("wifi_ssid : ", wifi_ssid);
#endif
}
myIndex = Find_End("wifi_password=", HTML_String);
if (myIndex >= 0) {
Pick_Text(wifi_password, &HTML_String[myIndex], 32);
#if DEBUG
exhibit ("wifi_password : ", wifi_password);
#endif
}
}
// MQTT Settings
if ( action == ACTION_SET_MQTT) {
myIndex = Find_End("mqtt_server=", HTML_String);
if (myIndex >= 0) {
Pick_Text(mqtt_server, &HTML_String[myIndex], 15);
#if DEBUG
Serial.print("MQTT Server: ");
Serial.println(mqtt_server);
#endif
}
myIndex = Find_End("mqtt_port=", HTML_String);
if (myIndex >= 0) {
Pick_Text(mqtt_port, &HTML_String[myIndex], 4);
#if DEBUG
Serial.print("MQTT Port: ");
Serial.println(mqtt_port);
#endif
}
myIndex = Find_End("mqtt_user=", HTML_String);
if (myIndex >= 0) {
Pick_Text(mqtt_user, &HTML_String[myIndex], 32);
#if DEBUG
Serial.print("MQTT User: ");
Serial.println(mqtt_user);
#endif
}
myIndex = Find_End("mqtt_password=", HTML_String);
if (myIndex >= 0) {
Pick_Text(mqtt_password, &HTML_String[myIndex], 32);
#if DEBUG
Serial.print("MQTT Password: ");
Serial.println(mqtt_password);
#endif
}
}
//Options select
if ( action == ACTION_LIES_AUSWAHL) {
devices = 0;
for (int i = 0; i < 6; i++) {
strcpy( tmp_string, "devices");
strcati( tmp_string, i);
strcat( tmp_string, "=");
if (Pick_Parameter_Zahl(tmp_string, HTML_String) == 1) {
devices |= 1 << i;
#if DEBUG
Serial.print("Option");
Serial.print(i);
Serial.print(" = ");
Serial.println(devices && i+1);
#endif
}
}
/*
//set bits
if (devices & 1) {
HAS_OLED = true;
}
else {
HAS_OLED = false;
}
if (devices & 2) {
HAS_RFM69 = true;
}
else {
HAS_RFM69 = false;
}
if (devices & 4) {
HAS_LIGHTSENSOR = true;
}
else {
HAS_LIGHTSENSOR = false;
}
if (devices & 8) {
USE_MQTT = true;
}
else {
USE_MQTT = false;
}
if (devices & 16) {
VCC_MEASURE = true;
}
else {
VCC_MEASURE = false;
}
if (devices & 32) {
DEBUG = true;
}
else {
DEBUG = false;
}
*/
#if DEBUG
Serial.print("Options (BIN): ");
Serial.println(devices,BIN);
/*
Serial.print("OLED: ");Serial.println(HAS_OLED);
Serial.print("RFM69: ");Serial.println(HAS_RFM69);
Serial.print("BH1750: ");Serial.println(HAS_LIGHTSENSOR);
Serial.print("MQTT: ");Serial.println(USE_MQTT);
Serial.print("VCC measure: ");Serial.println(VCC_MEASURE);
Serial.print("DEBUG: ");Serial.println(DEBUG);
*/
#endif
}
//---------------------------------------------------
//Antwortseite aufbauen
make_HTML01();
//---------------------------------------------------
// Header aufbauen
strcpy(HTTP_Header , "HTTP/1.1 200 OK\r\n");
strcat(HTTP_Header, "Content-Length: ");
strcati(HTTP_Header, strlen(HTML_String));
strcat(HTTP_Header, "\r\n");
strcat(HTTP_Header, "Content-Type: text/html\r\n");
strcat(HTTP_Header, "Connection: close\r\n");
strcat(HTTP_Header, "\r\n");
#if DEBUG
exhibit("Header : ", HTTP_Header);
exhibit("Laenge Header : ", strlen(HTTP_Header));
exhibit("Laenge HTML : ", strlen(HTML_String));
#endif
espClient.print(HTTP_Header);
delay(20);
send_HTML();
}
//---------------------------------------------------
// HTML Seite 01 aufbauen
//---------------------------------------------------
void make_HTML01() {
strcpy( HTML_String, "<!DOCTYPE html>");
strcat( HTML_String, "<html>");
strcat( HTML_String, "<head>");
strcat( HTML_String, "<title>Universal Sensor Setup</title>");
strcat( HTML_String, "</head>");
strcat( HTML_String, "<body bgcolor=\"#adcede\">");
strcat( HTML_String, "<font color=\"#000000\" face=\"VERDANA,ARIAL,HELVETICA\">");
strcat( HTML_String, "<h1>Universal Sensor Setup</h1>");
//---------------------------------------------------
// Textfelder wifi-ssid und wifi-password
strcat( HTML_String, "<h2>WiFi Settings</h2>");
strcat( HTML_String, "<form>");
strcat( HTML_String, "<table>");
set_colgroup(150, 270, 150, 0, 0);
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td><b>SSID</b></td>");
strcat( HTML_String, "<td>");
strcat( HTML_String, "<input type=\"text\" style= \"width:200px\" name=\"wifi_ssid\" maxlength=\"20\" Value =\"");
strcat( HTML_String, wifi_ssid);
strcat( HTML_String, "\"></td>");
strcat( HTML_String, "<td><button style= \"width:100px\" name=\"ACTION\" value=\"");
strcati(HTML_String, ACTION_SET_WIFI);
strcat( HTML_String, "\">set</button></td>");
strcat( HTML_String, "</tr>");
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td><b>Password</b></td>");
strcat( HTML_String, "<td>");
strcat( HTML_String, "<input type=\"password\" style= \"width:200px\" name=\"wifi_password\" maxlength=\"20\" Value =\"");
strcat( HTML_String, wifi_password);
strcat( HTML_String, "\"></td>");
strcat( HTML_String, "</tr>");
strcat( HTML_String, "</table>");
strcat( HTML_String, "</form>");
strcat( HTML_String, "<br>");
//---------------------------------------------------
// MQTT Settings
strcat( HTML_String, "<h2>MQTT Settings</h2>");
strcat( HTML_String, "<form>");
strcat( HTML_String, "<table>");
set_colgroup(150, 270, 150, 0, 0);
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td><b>Server</b></td>");
strcat( HTML_String, "<td><input type=\"text\" style= \"width:100px\" name=\"mqtt_server\" value=\"");
strcat( HTML_String, mqtt_server);
strcat( HTML_String, "\"></td>");
strcat( HTML_String, "<td><button style= \"width:100px\" name=\"ACTION\" value=\"");
strcati(HTML_String, ACTION_SET_MQTT);
strcat( HTML_String, "\">set</button></td>");
strcat( HTML_String, "</tr>");
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td><b>Port</b></td>");
strcat( HTML_String, "<td><input type=\"text\" style= \"width:100px\" name=\"mqtt_port\" value=\"");
strcat( HTML_String, mqtt_port);
strcat( HTML_String, "\"></td></tr>");
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td><b>User</b></td>");
strcat( HTML_String, "<td><input type=\"text\" style= \"width:200px\" name=\"mqtt_user\" value=\"");
strcat( HTML_String, mqtt_user);
strcat( HTML_String, "\"></td></tr>");
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td><b>Password</b></td>");
strcat( HTML_String, "<td><input type=\"password\" style= \"width:200px\" name=\"mqtt_password\" value=\"");
strcat( HTML_String, mqtt_password);
strcat( HTML_String, "\"></td></tr>");
strcat( HTML_String, "</table>");
strcat( HTML_String, "</form>");
strcat( HTML_String, "<br>");
//---------------------------------------------------
// Options
strcat( HTML_String, "<h2>Options</h2>");
strcat( HTML_String, "<form>");
strcat( HTML_String, "<table>");
set_colgroup(150, 270, 150, 0, 0);
strcat( HTML_String, "<tr>");
strcat( HTML_String, "<td>");
for (int i = 0; i < 6; i++) {
if (!i == 0)strcat( HTML_String, "<br>");
strcat( HTML_String, "<input type=\"checkbox\" name=\"devices");
strcati( HTML_String, i);
strcat( HTML_String, "\" id = \"DEV");
strcati( HTML_String, i);
strcat( HTML_String, "\" value = \"1\" ");
if (devices & 1 << i) strcat( HTML_String, "checked ");
strcat( HTML_String, "> ");
strcat( HTML_String, "<label for =\"DEV");
strcati( HTML_String, i);
strcat( HTML_String, "\">");
strcat( HTML_String, devices_tab[i]);
strcat( HTML_String, "</label>");
}
strcat( HTML_String, "</td>");
strcat( HTML_String, "<td><button style= \"width:100px\" name=\"ACTION\" value=\"");
strcati(HTML_String, ACTION_LIES_AUSWAHL);
strcat( HTML_String, "\">set</button></td>");
strcat( HTML_String, "</tr>");
strcat( HTML_String, "</font>");
strcat( HTML_String, "</font>");
strcat( HTML_String, "</body>");
strcat( HTML_String, "</html>");
}
//--------------------------------------------------------------------------
void send_not_found() {
#if DEBUG
Serial.println("Sende Not Found");
#endif
espClient.print("HTTP/1.1 404 Not Found\r\n\r\n");
delay(20);
espClient.stop();
}
//--------------------------------------------------------------------------
void send_HTML() {
char my_char;
int my_len = strlen(HTML_String);
int my_ptr = 0;
int my_send = 0;
//--------------------------------------------------------------------------
// in Portionen senden
while ((my_len - my_send) > 0) {
my_send = my_ptr + MAX_PACKAGE_SIZE;
if (my_send > my_len) {
espClient.print(&HTML_String[my_ptr]);
delay(20);
#if DEBUG
Serial.println(&HTML_String[my_ptr]);
#endif
my_send = my_len;
} else {
my_char = HTML_String[my_send];
// Auf Anfang eines Tags positionieren
while ( my_char != '<') my_char = HTML_String[--my_send];
HTML_String[my_send] = 0;
espClient.print(&HTML_String[my_ptr]);
delay(20);
#if DEBUG
Serial.println(&HTML_String[my_ptr]);
#endif
HTML_String[my_send] = my_char;
my_ptr = my_send;
}
}
espClient.stop();
}
//----------------------------------------------------------------------------------------------
void set_colgroup(int w1, int w2, int w3, int w4, int w5) {
strcat( HTML_String, "<colgroup>");
set_colgroup1(w1);
set_colgroup1(w2);
set_colgroup1(w3);
set_colgroup1(w4);
set_colgroup1(w5);
strcat( HTML_String, "</colgroup>");
}
//------------------------------------------------------------------------------------------
void set_colgroup1(int ww) {
if (ww == 0) return;
strcat( HTML_String, "<col width=\"");
strcati( HTML_String, ww);
strcat( HTML_String, "\">");
}
//---------------------------------------------------------------------
void strcati(char* tx, int i) {
char tmp[8];
itoa(i, tmp, 10);
strcat (tx, tmp);
}
//---------------------------------------------------------------------
void strcati2(char* tx, int i) {
char tmp[8];
itoa(i, tmp, 10);
if (strlen(tmp) < 2) strcat (tx, "0");
strcat (tx, tmp);
}
//---------------------------------------------------------------------
int Pick_Parameter_Zahl(const char * par, char * str) {
int myIdx = Find_End(par, str);
if (myIdx >= 0) return Pick_Dec(str, myIdx);
else return -1;
}
//---------------------------------------------------------------------
int Find_End(const char * such, const char * str) {
int tmp = Find_Start(such, str);
if (tmp >= 0)tmp += strlen(such);
return tmp;
}
//---------------------------------------------------------------------
int Find_Start(const char * such, const char * str) {
int tmp = -1;
int ww = strlen(str) - strlen(such);
int ll = strlen(such);
for (int i = 0; i <= ww && tmp == -1; i++) {
if (strncmp(such, &str[i], ll) == 0) tmp = i;
}
return tmp;
}
//---------------------------------------------------------------------
int Pick_Dec(const char * tx, int idx ) {
int tmp = 0;
for (int p = idx; p < idx + 5 && (tx[p] >= '0' && tx[p] <= '9') ; p++) {
tmp = 10 * tmp + tx[p] - '0';
}
return tmp;
}
//----------------------------------------------------------------------------
int Pick_N_Zahl(const char * tx, char separator, byte n) {
int ll = strlen(tx);
//int tmp = -1;
byte anz = 1;
byte i = 0;
while (i < ll && anz < n) {
if (tx[i] == separator)anz++;
i++;
}
if (i < ll) return Pick_Dec(tx, i);
else return -1;
}
//---------------------------------------------------------------------
int Pick_Hex(const char * tx, int idx ) {
int tmp = 0;
for (int p = idx; p < idx + 5 && ( (tx[p] >= '0' && tx[p] <= '9') || (tx[p] >= 'A' && tx[p] <= 'F')) ; p++) {
if (tx[p] <= '9')tmp = 16 * tmp + tx[p] - '0';
else tmp = 16 * tmp + tx[p] - 55;
}
return tmp;
}
//---------------------------------------------------------------------
void Pick_Text(char * tx_ziel, char * tx_quelle, int max_ziel) {
int p_ziel = 0;
int p_quelle = 0;
int len_quelle = strlen(tx_quelle);
while (p_ziel < max_ziel && p_quelle < len_quelle && tx_quelle[p_quelle] && tx_quelle[p_quelle] != ' ' && tx_quelle[p_quelle] != '&') {
if (tx_quelle[p_quelle] == '%') {
tx_ziel[p_ziel] = (HexChar_to_NumChar( tx_quelle[p_quelle + 1]) << 4) + HexChar_to_NumChar(tx_quelle[p_quelle + 2]);
p_quelle += 2;
} else if (tx_quelle[p_quelle] == '+') {
tx_ziel[p_ziel] = ' ';
}
else {
tx_ziel[p_ziel] = tx_quelle[p_quelle];
}
p_ziel++;
p_quelle++;
}
tx_ziel[p_ziel] = 0;
}
//---------------------------------------------------------------------
char HexChar_to_NumChar( char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 55;
return 0;
}
#if DEBUG
//---------------------------------------------------------------------
void exhibit(const char * tx, int v) {
Serial.print(tx);
Serial.println(v);
}
//---------------------------------------------------------------------
void exhibit(const char * tx, unsigned int v) {
Serial.print(tx);
Serial.println(v);
}
//---------------------------------------------------------------------
void exhibit(const char * tx, unsigned long v) {
Serial.print(tx);
Serial.println(v);
}
//---------------------------------------------------------------------
void exhibit(const char * tx, const char * v) {
Serial.print(tx);
Serial.println(v);
}
#endif
#endif //ifdef ESP8266
/*********************************************************************
* Write operation in either Wire or SPI
*
* param[in] dev_addr Wire or SPI device address
* param[in] reg_addr register address
* param[in] reg_data_ptr pointer to the data to be written
* param[in] data_len number of bytes to be written
*
* return result of the bus communication function
*/
int8_t bus_write(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data_ptr, uint16_t data_len)
{
Wire.beginTransmission(dev_addr);
Wire.write(reg_addr); /* Set register address to start writing to */
/* Write the data */
for (int index = 0; index < data_len; index++)
{
Wire.write(reg_data_ptr[index]);
}
#ifdef __STM32F1__
Wire.endTransmission();
return 0;
#else
return (int8_t)Wire.endTransmission();
#endif
}
/*********************************************************************
* Read operation in either Wire or SPI
*
* param[in] dev_addr Wire or SPI device address
* param[in] reg_addr register address
* param[out] reg_data_ptr pointer to the memory to be used to store the read data
* param[in] data_len number of bytes to be read
*
* return result of the bus communication function
*/
int8_t bus_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data_ptr, uint16_t data_len)
{
int8_t comResult = 0;
Wire.beginTransmission(dev_addr);
Wire.write(reg_addr); /* Set register address to start reading from */
comResult = Wire.endTransmission();
delayMicroseconds(150); /* Precautionary response delay */
Wire.requestFrom(dev_addr, (uint8_t)data_len); /* Request data */
int index = 0;
while (Wire.available()) /* The slave device may send less than requested (burst read) */
{
reg_data_ptr[index] = Wire.read();
index++;
}
#ifdef __STM32F1__
return 0;
#else
return comResult;
#endif
}
/*********************************************************************
* System specific implementation of sleep function
*
* param[in] t_ms time in milliseconds
*
* return none
*/
void sleep(uint32_t t_ms)
{
delay(t_ms);
}
/*********************************************************************
* Capture the system time in microseconds
*
* return system_current_time current system timestamp in microseconds
*/
int64_t get_timestamp_us()
{
return (int64_t) millis() * 1000;