-
Notifications
You must be signed in to change notification settings - Fork 2
/
8266_logger.21.ino
1503 lines (1224 loc) · 46.1 KB
/
8266_logger.21.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
/*------------------------------------------------------------------------------
Simple Data Logger
Use a Wemos d1 mini and a sensor, and this program logs data without internet, SD card, ...
or anything -- just the Wemos and the sensor, and it serves up graphs to your computer or
your phone of the following data logging intervals
- 3 minutes of 1-Second samples
- 3 hours of 1-Minute samples
- 1 day of 5-Minute samples
- 10 days of 1-hour averages
- 180 days of daily averages
- and 100 samples of live moving graph at up to 1/10 of a second samples
It logs to memory that san be served as a graph, or a data file, and it saves the data to the
internal Wemos 8266 flash in case of power failure or reboot.
It uses WiFiManager to get ssid name and password with normal 192.168.4.1 startup.
Set Arduino board to LOLIN(WEMOS) D1 R2 & mini
by James Zahary Dec 18, 2020
v20 - Dec 30, 2020 - reformat text output with Exceltime
v21 - Jan 8, 2021 - fix the fake data function
https://github.com/jameszah/Simple_8266_Data_Logger
Simple_8266_Data_Logger is licensed under the
GNU General Public License v3.0
Using library ESP8266WiFi at version 1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi
Using library DNSServer at version 1.1.1 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\DNSServer
Using library ESP8266WebServer at version 1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WebServer
Using library WebSockets at version 2.3.1 in folder: C:\Users\James\Documents\Arduino\libraries\WebSockets
Using library WiFiManager at version 2.0.3-alpha in folder: C:\Users\James\Documents\Arduino\libraries\WiFiManager
Using library Ticker at version 1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\Ticker
Using library LittleFS at version 0.1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\LittleFS
Using library BME280 at version 2.3.0 in folder: C:\Users\James\Documents\Arduino\libraries\BME280
Using library Wire at version 1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\Wire
Using library Hash at version 1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\Hash
Using library SPI at version 1.0 in folder: C:\Users\James\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\SPI
Starting point of this program was from Acrobatic in 2018 below:
- I think it was the chart.js stuff with a moving graph
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
10/20/2018
Author: Cisco • A C R O B O T I C
Platforms: ESP8266
Language: C++/Arduino
File: bmp180_gui.ino
------------------------------------------------------------------------------
Description:
Code for YouTube video demonstrating how to plot sensor data from a BMP180 tem-
perature, and barometric pressure sensor. A web server running on the ESP8266
serves a web page that uses Chart.js and websockets to plot the data.
https://youtu.be/lEVoRJSsEa8
------------------------------------------------------------------------------
Do you like my work? You can support me:
https://patreon.com/acrobotic
https://paypal.me/acrobotic
https://buymeacoff.ee/acrobotic
------------------------------------------------------------------------------
Please consider buying products and kits to help fund future Open-Source
projects like this! We'll always put our best effort in every project, and
release all our design files and code for you to use.
https://acrobotic.com/
https://amazon.com/shops/acrobotic
------------------------------------------------------------------------------
License:
Please see attached LICENSE.txt file for details.
------------------------------------------------------------------------------*/
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <Ticker.h>
#include "FS.h"
#include <LittleFS.h>
#define FORMAT_LITTLEFS_IF_FAILED true
#include <time.h>
//int timezone = -6; // -6 for daylight savings, -7 regular
int dst = 0;
struct tm * timeinfo;
time_t boottime;
time_t now;
unsigned long unixtime;
void listDir(const char * dirname) {
Serial.printf("Listing directory: %s\n", dirname);
Dir root = LittleFS.openDir(dirname);
while (root.next()) {
File file = root.openFile("r");
Serial.print(" FILE: ");
Serial.print(root.fileName());
Serial.print(" SIZE: ");
Serial.print(file.size());
time_t cr = file.getCreationTime();
time_t lw = file.getLastWrite();
file.close();
struct tm * tmstruct = localtime(&cr);
Serial.printf(" CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
tmstruct = localtime(&lw);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
}
}
Ticker timer;
Ticker timer1Sec;
Ticker timer1Min;
int counter = 0;
int FiveMin;
int OneHour;
int OneDay;
#define Max_1_Sec 180
#define Max_1_Min 180
#define Max_5_Min 288
#define Max_1_Hour 240
#define Max_1_Day 180
typedef struct {
float f1;
float f2;
float f3;
unsigned long t;
} dp;
dp Buf_1_Sec [Max_1_Sec];
dp Buf_1_Min [Max_1_Min];
dp Buf_5_Min [Max_5_Min];
dp Buf_1_Hour [Max_1_Hour];
dp Buf_1_Day [Max_1_Day];
int Buf_In_1_Sec = 0;
int Buf_Out_1_Sec = 0;
int Buf_In_1_Min = 0;
int Buf_Out_1_Min = 0;
int Buf_In_5_Min = 0;
int Buf_Out_5_Min = 0;
int Buf_In_1_Hour = 0;
int Buf_Out_1_Hour = 0;
int Buf_In_1_Day = 0;
int Buf_Out_1_Day = 0;
bool get_data_1Sec = false;
bool get_data_1Min = false;
bool get_data_5Min = false;
bool get_data_1Hour = false;
bool get_data_1Day = false;
bool get_data_live = false;
bool get_data = false;
bool do_a_backup = false;
unsigned long fake = 0;
//
// BME280 sensor stuff
//
#include <BME280I2C.h>
#include <Wire.h>
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
float temp(NAN), hum(NAN), pres(NAN);
/*
#include <OneWire.h>
#include <DallasTemperature.h>
#define DS_PIN 0 // gpio 0 - D3
OneWire oneWire(DS_PIN);
DallasTemperature sensors(&oneWire);
DeviceAddress DS18B20[3];
*/
/*
Wemos d1 mini
Pin ESP-8266 Pin Function
D0 GPIO16
D1 GPIO5 SCL -- bme
D2 GPIO4 SDA -- bme
D3 GPIO0 10k Pull-up -- ds18b20
D4 GPIO2 10k Pull-up, BUILTIN_LED
D5 GPIO14 SCK
D6 GPIO12 MISO
D7 GPIO13 MOSI
D8 GPIO15 10k Pull-down, SS
*/
// Connecting to the Internet
// Running a web server
ESP8266WebServer server;
// Adding a websocket to the server
WebSocketsServer webSocket = WebSocketsServer(81);
// Serving a web page (from flash memory)
// formatted as a string literal!
//<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js'></script>
char webpage[] PROGMEM = R"=====(
<html>
<!-- Adding a data chart using Chart.js -->
<head>
<script src='https://cdn.jsdelivr.net/npm/[email protected]'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]'></script>
</head>
<body onload="javascript:init()">
<hr />
<div>
<canvas id="line-chart" width="800" height="450"></canvas>
</div>
<br>
<button onClick="f_seconds()" id="seconds">Seconds</button>
<button onClick="f_minutes()" id="minutes">Minutes</button>
<button onClick="f_5minutes()" id="5minutes">5 Minutes</button>
<button onClick="f_hours()" id="hours">Hours</button>
<button onClick="f_days()" id="days">Days</button>
<button onClick="f_live()" id="live">Live</button>
<!-- Adding a slider for controlling data rate -->
<input type="range" min="1" max="30" value="5" id="dataRateSlider" oninput="sendDataRate()" />
<label for="dataRateSlider" id="dataRateLabel">Update: 5 /10 Sec</label>
<button onClick="f_write()" id="write">Write Data to Flash</button>
<hr>
<div id="p2"></div>
<div id="danger">
<hr>
<h1>These are dangerous buttons!</h1>
<button style=" background-color:red; border-color:blue; color:white" onClick="f_erase()" id="era">Erase all current Data</button>
<button style=" background-color:blue; border-color:red; color:white" onClick="f_fake()" id="fake">Make 3 days of fake data</button>
<button style=" background-color:red; border-color:blue; color:white" onClick="f_delete()" id="del">Delete Data from Flash</button>
<hr>
</div>
<!-- Adding a websocket to the client (webpage) -->
<script>
var webSocket, dataPlot;
var maxDataPoints = 100;
function removeData(){
dataPlot.data.labels.shift();
dataPlot.data.datasets[0].data.shift();
}
function addData(label, data) {
if(dataPlot.data.labels.length > maxDataPoints) removeData();
dataPlot.data.labels.push(label);
dataPlot.data.datasets[0].data.push(data);
dataPlot.update();
}
function init() {
webSocket = new WebSocket('ws://' + window.location.hostname + ':81/');
dataPlot = new Chart(document.getElementById("line-chart"), {
type: 'line',
data: {
labels: [],
datasets: [
{
data: [],
label: "BME Temperature (C)",
yAxisID: "A",
borderColor: "red",
fill: false
},
{
data: [],
label: "Pressure (mPa)",
yAxisID: "B", // humid and pressure same axis
borderColor: "blue",
fill: false
},
{
data: [],
label: "Humidity (%)",
yAxisID: "B",
borderColor: "green",
fill: false,
display: false
}
]
},
options: {
title: {
display: true,
text: 'Logger 21 - Click Seconds, Minutes, ... and Click Temperature, Pressure, ... to add or remove it'
},
scales: {
xAxes: [{
type: 'time',
distribution: 'series',
ticks: {
autoSkip: true,
maxTicksLimit: 20
}
}],
yAxes: [{
id: 'A',
type: 'linear',
position: 'left',
}, {
id: 'B',
type: 'linear',
position: 'right',
}]
}
}
});
webSocket.onclose = async (event) => {
console.error(event);
dataPlot.options.title.text = " Logger21 - Hit Refresh - Socket has closed " ;
}
webSocket.onmessage = function(event) {
var mydata = JSON.parse(event.data);
var mytype = mydata.type;
var myLabel = mydata.myLabel;
var nowLabel = new Date();
var mydataarray = mydata.value;
//console.log(mydataarray.length, mydataarray);
if (mytype == "live") {
var data = JSON.parse(event.data);
var today = new Date();
//var t = today.getSeconds();
//console.log('live',mydataarray);
//addData('', mydataarray);
//function addData(label, data) {
if(dataPlot.data.labels.length > maxDataPoints) {
dataPlot.data.labels.shift();
dataPlot.data.datasets[0].data.shift();
dataPlot.data.datasets[1].data.shift();
dataPlot.data.datasets[2].data.shift();
}
//dataPlot.data.labels.push('');
dataPlot.data.labels.push(mydataarray.y*1000 + mydataarray.y2);
dataPlot.data.datasets[0].data.push(mydataarray.x);
dataPlot.data.datasets[1].data.push(mydataarray.x2);
dataPlot.data.datasets[2].data.push(mydataarray.x3);
dataPlot.update();
if (dataPlot.data.labels.length < maxDataPoints) {
document.getElementById("p2").innerHTML += (25569 + (mydataarray.y + mydataarray.y2/1000)/ 86400) + " , " + mydataarray.x + " , " + mydataarray.x2 + " , " + mydataarray.x3 + "<br>";
} else {
document.getElementById("p2").innerHTML = "Too many points!";
}
} else if (mytype == "LIVE") {
//dataPlot.data.datasets[0].label = myLabel + " - " + nowLabel; // data.push(mydataarray[i].x);
dataPlot.options.title.text = myLabel + " - " + nowLabel;
document.getElementById("p2").innerHTML = "<p> </p> ExcelTime , Temperature (C) , Pressure (mPa) , Humidity (%) <br>";
//console.log('LIVE',dataPlot.data.datasets[0].data.length); //jz
var len = dataPlot.data.datasets[0].data.length;
for(var i = 0; i <= len ; i++ ) {
dataPlot.data.labels.pop();
dataPlot.data.datasets[0].data.pop();
dataPlot.data.datasets[1].data.pop();
dataPlot.data.datasets[2].data.pop();
}
dataPlot.update();
} else {
dataPlot.options.title.text = myLabel + " - " + nowLabel;
//dataPlot.data.datasets[0].label = myLabel; // + " - " + nowLabel;; // data.push(mydataarray[i].x);
document.getElementById("p2").innerHTML = "<p> </p>ExcelTime , Temperature (C) , Pressure (mPa) , Humidity (%) , Browser Time<br>";
var len = dataPlot.data.datasets[0].data.length;
for(var i = 0; i <= len ; i++ ) {
dataPlot.data.labels.pop();
dataPlot.data.datasets[0].data.pop();
dataPlot.data.datasets[1].data.pop();
dataPlot.data.datasets[2].data.pop();
}
dataPlot.update();
for(var i = 0; i < mydataarray.length; i++ ) {
var t = new Date(mydataarray[i].y*1000);
dataPlot.data.labels.push(mydataarray[i].y*1000);
dataPlot.data.datasets[0].data.push(mydataarray[i].x);
dataPlot.data.datasets[1].data.push(mydataarray[i].x2);
dataPlot.data.datasets[2].data.push(mydataarray[i].x3);
document.getElementById("p2").innerHTML += (25569 + mydataarray[i].y / 86400) + " , " + mydataarray[i].x + " , " + mydataarray[i].x2 + " , " + mydataarray[i].x3 + " , " + t + "<br>";
}
dataPlot.update();
}
}
}
function f_seconds(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Hit Refresh - Socket has closed " ;
} else {
console.log('got seconds click');
document.getElementById("dataRateLabel").innerHTML = "Update: 5 /10 Sec";
document.getElementById("dataRateSlider").value = 5;
webSocket.send("s");
}
}
function f_minutes(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Hit Refresh - Socket has closed " ;
} else {
console.log('got minutes click');
document.getElementById("dataRateLabel").innerHTML = "Update: 5 /10 Sec";
document.getElementById("dataRateSlider").value = 5;
webSocket.send("m");
}
}
function f_5minutes(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Hit Refresh - Socket has closed " ;
} else {
console.log('got 5 minutes click');
document.getElementById("dataRateLabel").innerHTML = "Update: 5 /10 Sec";
document.getElementById("dataRateSlider").value = 5;
webSocket.send("f");
}
}
function f_hours(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Hit Refresh - Socket has closed " ;
} else {
console.log('got hours click');
document.getElementById("dataRateLabel").innerHTML = "Update: 5 /10 Sec";
document.getElementById("dataRateSlider").value = 5;
webSocket.send("h");
}
}
function f_days(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Hit Refresh - Socket has closed " ;
} else {
console.log('got days click');
document.getElementById("dataRateLabel").innerHTML = "Update: 5 /10 Sec";
document.getElementById("dataRateSlider").value = 5;
webSocket.send("y");
}
}
function f_fake(){
console.log('got fake click');
document.getElementById("dataRateLabel").innerHTML = "Update: 5 /10 Sec";
document.getElementById("dataRateSlider").value = 5;
webSocket.send("X");
}
function f_live(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Logger21 - Hit Refresh - Socket has closed " ;
} else {
console.log('got live click');
webSocket.send("l");
}
}
function f_write(){
if (webSocket.readyState === WebSocket.CLOSED) {
dataPlot.options.title.text = " Hit Refresh - Socket has closed " ;
} else {
console.log('got write click');
webSocket.send("w");
}
}
function f_delete(){
console.log('got delete click');
webSocket.send("d");
}
function f_erase(){
console.log('got erase click');
webSocket.send("e");
}
function sendDataRate(){
var dataRate = document.getElementById("dataRateSlider").value;
webSocket.send(dataRate);
dataRate = dataRate ;
document.getElementById("dataRateLabel").innerHTML = "Update: " + dataRate + " /10 Sec" ; //"Update: " + dataRate + " sec";
}
</script>
</body>
</html>
)=====";
void setup() {
Serial.begin(115200);
Serial.println(" ");
Serial.println("Logger21");
WiFiManager wifiManager;
//wifiManager.resetSettings();
wifiManager.autoConnect("Logger");
Serial.println("connected...yeey :)");
WiFi.mode(WIFI_STA);
WiFi.hostname("Logger");
// put your setup code here, to run once:
// WiFi.begin(ssid, password);
// Serial.begin(115200);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
//#define TIMEZONE "GMT0BST,M3.5.0/01,M10.5.0/02" // your timezone - this is GMT
setenv("TZ", "MST7MDT,M3.2.0/2:00:00,M11.1.0/2:00:00", 1); // mountain time zone
tzset();
server.on("/", []() {
server.send_P(200, "text/html", webpage);
});
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
Wire.begin();
while (!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
switch (bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
/*
sensors.begin();
int available = sensors.getDeviceCount();
Serial.print("DS Device count: ");
Serial.println( available);
for(int x = 0; x!= available; x++)
{
if(sensors.getAddress(DS18B20[x], x))
{
//Serial.println(DS18B20[x]);
sensors.setResolution(DS18B20[x], 12);
}
}
*/
Serial.println("Mount LittleFS");
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
Serial.println("Formatting LittleFS filesystem");
LittleFS.format();
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
return;
}
}
listDir("/");
ReadFile2();
Serial.println("\nWaiting for time");
//delay(10000);
while (time(nullptr) < 1000) {
Serial.print(".");
delay(1000);
}
Serial.println("");
now = time(nullptr);
Serial.println(ctime(&now));
Serial.println(now);
boottime = now;
boottime = boottime - millis() / 1000;
Serial.printf("boottime correctmod %d\n", boottime);
unsigned long rightnow = time(nullptr);
int OneMin = (60 - (rightnow % 60) + 30) % 60;
Serial.printf("Let us just delay until we get to the middle of minute: %d seconds\n", OneMin);
delay (OneMin * 1000);
rightnow = time(nullptr);
counter = 0;
FiveMin = 5 - (rightnow % 300 ) / 60;
OneHour = 60 - (rightnow % 3600 ) / 60;
OneDay = 1440 - (rightnow % (1440 * 60) ) / 60;
OneDay = (OneDay - 9 * 60 + 1440 ) % 1440; // 5pm or GMT midnight to = 8 AM Mountain Time
Serial.printf("Right Now = %d, in Minutes %d\n", rightnow, rightnow % 60);
Serial.printf("Five Min index = %d\n", FiveMin);
Serial.printf("One Hour index = %d\n", OneHour);
Serial.printf("One Day index = %d\n", OneDay);
timer.attach(5, getData);
timer1Sec.attach(1, Proc1Sec);
timer1Min.attach(60, Proc1Min);
}
bool first = true;
bool try_again_hour = false;
bool try_again_day = false;
int warmup = 5;
float f1, f2, f3;
float ds_temp = -100;
float fakef1, fakef2, fakef3;
unsigned long f1f2f3time;
unsigned long nowtime;
long loopcount = 0;
void loop() {
// put your main code here, to run repeatedly:
webSocket.loop();
server.handleClient();
loopcount++;
delay(50);
bme.read(pres, temp, hum, tempUnit, presUnit);
nowtime = time(nullptr);
f1f2f3time = 60 * (int) ((nowtime / 60 ) + 0.5);
f1 = temp;
f2 = pres / 1000; // from 88000 kpa to 88 mpa to put on same scale as humidity
f3 = hum;
/*
if (loopcount % 100 == 0) {
long star = millis();
sensors.requestTemperatures();
Serial.println(millis()-star);
ds_temp = sensors.getTempC(DS18B20[0]);
Serial.println(millis()-star);
f2 = ds_temp;
}
if (isnan(ds_temp)) {
ds_temp = 0;
} else if (ds_temp < -99){
ds_temp = 0;
}
*/
if (fake > 0 ) {
f1f2f3time = fake;
float mod = 1.0 ; // + ( abs((counter % 360) - 180)) / 360.0;
f1 = fakef1 * mod;
f2 = fakef2 * mod;
f3 = fakef3 * mod;
}
if (get_data_live && get_data) {
String json = "{\"type\":\"live\",\"value\":{\"x\":";
json += f1;
json += ",\"x2\":";
json += f2;
json += ",\"x3\":";
json += f3;
json += ",\"y\":"; // arduino gives seconds since 1970
json += boottime ; // but javascript time needs milli-seconds since 1970
json += ",\"y2\":"; // and we need a long long int to send that
json += millis() ; // which doesn't seem to work nicely
json += "}}"; // so send the seconds at boot, and the milli-seconds since boot
//Serial.printf("tempnum %d, bootime %d, nowtime %d, millis %d\n", tempnum, boottime,nowtime,millis());
webSocket.broadcastTXT(json.c_str(), json.length());
//Serial.println(json.c_str());
//Serial.println(json.length());
get_data = false;
}
if (get_data_1Hour) {
int len = (Buf_In_5_Min - Buf_Out_5_Min + Max_5_Min ) % Max_5_Min;
if (len < 12) {
//Serial.println("Not enogh data for a 1 Hour average");
try_again_hour = true;
} else {
try_again_hour = false;
if ( ((Buf_In_1_Hour + 1) % Max_1_Hour) == Buf_Out_1_Hour) {
Buf_Out_1_Hour = (Buf_Out_1_Hour + 1) % Max_1_Hour;
}
Buf_In_1_Hour = (Buf_In_1_Hour + 1) % Max_1_Hour;
float sumf1 = 0;
float sumf2 = 0;
float sumf3 = 0;
for (int i = 0; i < 12; i++) {
int ind = (Buf_In_5_Min - i + Max_5_Min ) % Max_5_Min;
sumf1 += Buf_5_Min[ind].f1;
sumf2 += Buf_5_Min[ind].f2;
sumf3 += Buf_5_Min[ind].f3;
}
Buf_1_Hour[Buf_In_1_Hour].f1 = sumf1 / 12;
Buf_1_Hour[Buf_In_1_Hour].f2 = sumf2 / 12;
Buf_1_Hour[Buf_In_1_Hour].f3 = sumf3 / 12;
Buf_1_Hour[Buf_In_1_Hour].t = f1f2f3time;
Serial.printf("1Hour Index, In %d, Out %d, f1 = %f\n", Buf_In_1_Hour, Buf_Out_1_Hour, f1);
}
get_data_1Hour = false;
}
if (get_data_1Day) {
int len = (Buf_In_1_Hour - Buf_Out_1_Hour + Max_1_Hour ) % Max_1_Hour;
if (len < 24) {
Serial.println("Tried to save 1 day, but not enough data, try again tomorrow");
try_again_day = true;
} else {
try_again_day = false;
if ( ((Buf_In_1_Day + 1) % Max_1_Day) == Buf_Out_1_Day) {
Buf_Out_1_Day = (Buf_Out_1_Day + 1) % Max_1_Day;
}
Buf_In_1_Day = (Buf_In_1_Day + 1) % Max_1_Day;
float sumf1 = 0;
float sumf2 = 0;
float sumf3 = 0;
for (int i = 0; i < 24; i++) {
int ind = (Buf_In_1_Hour - i + Max_1_Hour ) % Max_1_Hour;
sumf1 += Buf_1_Hour[ind].f1;
sumf2 += Buf_1_Hour[ind].f2;
sumf3 += Buf_1_Hour[ind].f3;
}
Buf_1_Day[Buf_In_1_Day].f1 = sumf1 / 24;
Buf_1_Day[Buf_In_1_Day].f2 = sumf2 / 24;
Buf_1_Day[Buf_In_1_Day].f3 = sumf3 / 24;
Buf_1_Day[Buf_In_1_Day].t = f1f2f3time;
Serial.printf("1Day Index, In %d, Out %d, f1 = %f\n", Buf_In_1_Day, Buf_Out_1_Day, Buf_1_Day[Buf_In_1_Day].f1);
}
get_data_1Day = false;
}
if (do_a_backup) {
do_a_backup = false;
WriteFile2();
}
} // end of loop
void getData() {
get_data = true;
}
void Proc1Sec() {
//Serial.printf("Bef 1Sec Index, In %d, Out %d, f1 = %f\n",Buf_In_1_Sec,Buf_Out_1_Sec, f1);
if ( ((Buf_In_1_Sec + 1) % Max_1_Sec) == Buf_Out_1_Sec) {
Buf_Out_1_Sec = (Buf_Out_1_Sec + 1) % Max_1_Sec;
}
Buf_In_1_Sec = (Buf_In_1_Sec + 1) % Max_1_Sec;
Buf_1_Sec[Buf_In_1_Sec].f1 = f1;
Buf_1_Sec[Buf_In_1_Sec].f2 = f2;
Buf_1_Sec[Buf_In_1_Sec].f3 = f3;
Buf_1_Sec[Buf_In_1_Sec].t = boottime + millis() / 1000; //nowtime; // use nowtime that has not been rounded to the minute
//Serial.printf("Aft 1Sec Index, In %d, Out %d, f1 = %f\n",Buf_In_1_Sec,Buf_Out_1_Sec, f1);
get_data_1Sec = false;
/*
if (warmup < 0 ) {
get_data_1Sec = true;
} else if (warmup == 0 ){
Proc1Min();
warmup--;
} else {
warmup--; }
*/
}
void Proc1Min() {
if ( ((Buf_In_1_Min + 1) % Max_1_Min) == Buf_Out_1_Min) {
Buf_Out_1_Min = (Buf_Out_1_Min + 1) % Max_1_Min;
}
Buf_In_1_Min = (Buf_In_1_Min + 1) % Max_1_Min;
Buf_1_Min[Buf_In_1_Min].f1 = f1;
Buf_1_Min[Buf_In_1_Min].f2 = f2;
Buf_1_Min[Buf_In_1_Min].f3 = f3;
Buf_1_Min[Buf_In_1_Min].t = f1f2f3time;
//Serial.printf("1Min Index, In %d, Out %d, f1 = %f\n", Buf_In_1_Min, Buf_Out_1_Min, f1);
//get_data_1Min = false;
//get_data_1Min = true;
unsigned long rightnow = time(nullptr);
unsigned long rightnowmin = rightnow - (rightnow % 60 );
if (fake > 0){
rightnow = fake;
rightnowmin = rightnow - (rightnow % 60 );
}
int xmin = (rightnow % 300 ) / 60;
int xhour = (rightnow % 3600 ) / 60;
int xday = (rightnow % (1440 * 60)) / 60;
//Serial.printf("Now %d, NowMin %d, Min %d, Hour %d, Day %d\n",rightnow,rightnowmin,xmin,xhour,xday);
if (xmin == 0) {
//get_data_5Min = true;
if ( ((Buf_In_5_Min + 1) % Max_5_Min) == Buf_Out_5_Min) {
Buf_Out_5_Min = (Buf_Out_5_Min + 1) % Max_5_Min;
}
Buf_In_5_Min = (Buf_In_5_Min + 1) % Max_5_Min;
Buf_5_Min[Buf_In_5_Min].f1 = f1;
Buf_5_Min[Buf_In_5_Min].f2 = f2;
Buf_5_Min[Buf_In_5_Min].f3 = f3;
Buf_5_Min[Buf_In_5_Min].t = f1f2f3time;
}
if (xhour == 0 || try_again_hour) {
get_data_1Hour = true;
if (xday == 0 || try_again_day) get_data_1Day = true;
}
/*
if (counter % 5 == FiveMin) {
get_data_5Min = true;
if (counter % 60 == OneHour || try_again_hour) {
get_data_1Hour = true;
if (counter % 1440 == OneDay || try_again_day) get_data_1Day = true;
}
}
*/
if (fake == 0 && counter % 240 == 120) do_a_backup = true; // WriteFile2(); // every 4 hours - not during fake
if (fake > 0) {
if (fake < time(nullptr)) {
fake = fake + 60;
} else {
Serial.println("Done making fake data.");
fake = 0;
timer1Min.detach();
timer1Min.attach(60, Proc1Min);
}
}
counter++;
}
void sendSeconds() {
int len = (Buf_In_1_Sec - Buf_Out_1_Sec + Max_1_Sec ) % Max_1_Sec;
String json = "{\"type\":\"seconds\", \"myLabel\":\" 180 Seconds - 3 Minutes\", \"value\":[";
for (int i = 1 ; i < len; i++) {
json += "{\"x\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + i) % Max_1_Sec].f1;
json += ",\"x2\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + i) % Max_1_Sec].f2;
json += ",\"x3\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + i) % Max_1_Sec].f3;
json += ",\"y\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + i) % Max_1_Sec].t;
json += "},";
}
json += "{\"x\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + len) % Max_1_Sec].f1;
json += ",\"x2\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + len) % Max_1_Sec].f2;
json += ",\"x3\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + len) % Max_1_Sec].f3;
json += ",\"y\":";
json += Buf_1_Sec[(Buf_Out_1_Sec + len) % Max_1_Sec].t;
json += "}";
json += "]}";
//Serial.println(json.c_str()); Serial.println(json.length());
webSocket.broadcastTXT(json.c_str(), json.length());
}
void sendMinutes() {
int len = (Buf_In_1_Min - Buf_Out_1_Min + Max_1_Min ) % Max_1_Min;
String json = "{\"type\":\"minutes\", \"myLabel\":\" 180 Minutes - 3 Hours\", \"value\":[";
for (int i = 1 ; i < len; i++) {
json += "{\"x\":";
json += Buf_1_Min[(Buf_Out_1_Min + i) % Max_1_Min].f1;
json += ",\"x2\":";
json += Buf_1_Min[(Buf_Out_1_Min + i) % Max_1_Min].f2;
json += ",\"x3\":";
json += Buf_1_Min[(Buf_Out_1_Min + i) % Max_1_Min].f3;
json += ",\"y\":";
json += Buf_1_Min[(Buf_Out_1_Min + i) % Max_1_Min].t;
json += "},";
}
json += "{\"x\":";
json += Buf_1_Min[(Buf_Out_1_Min + len) % Max_1_Min].f1;
json += ",\"x2\":";
json += Buf_1_Min[(Buf_Out_1_Min + len) % Max_1_Min].f2;
json += ",\"x3\":";
json += Buf_1_Min[(Buf_Out_1_Min + len) % Max_1_Min].f3;
json += ",\"y\":";
json += Buf_1_Min[(Buf_Out_1_Min + len) % Max_1_Min].t;
json += "}";
json += "]}";
//Serial.println(json.c_str()); Serial.println(json.length());
webSocket.broadcastTXT(json.c_str(), json.length());
}
void send5Minutes() {
int len = (Buf_In_5_Min - Buf_Out_5_Min + Max_5_Min ) % Max_5_Min;
String json = "{\"type\":\"5minutes\", \"myLabel\":\" 288 x 5Min - 1 Day\", \"value\":[";