-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggingTestV2.ino
186 lines (143 loc) · 4.71 KB
/
LoggingTestV2.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
//V2 add time stamps
#include <elegantWebpage.h>
#include <AsyncElegantOTA.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <logger_spiffs.h>
#include "SPIFFS.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <WebSerial.h>
const char* ssid = "***";
const char* password = "***";
int counter = 72; //Delete me
// Specify the path where the log is placed
LoggerSPIFFS sysLog("/log/syslog.txt");
AsyncWebServer server(80);
//Structure time
/**
* Input time in epoch format and return tm time format
* by Renzo Mischianti <www.mischianti.org>
*/
static tm getDateTimeByParams(long time){
struct tm *newtime;
const time_t tim = time;
newtime = localtime(&tim);
return *newtime;
}
/**
* Input tm time format and return String with format pattern
* by Renzo Mischianti <www.mischianti.org>
*/
static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)"%m/%d/%Y %r"){
char buffer[30];
strftime(buffer, 30, pattern, newtime);
return buffer;
}
/**
* Input time in epoch format format and return String with format pattern
* by Renzo Mischianti <www.mischianti.org>
*/
static String getEpochStringByParams(long time, char* pattern = (char *)"%m/%d/%Y %r"){
// struct tm *newtime;
tm newtime;
newtime = getDateTimeByParams(time);
return getDateTimeStringByParams(&newtime, pattern);
}
// Configure NTPClient
// You can specify the time server pool and the offset, (in seconds)
// additionally you can specify the update interval (in milliseconds).
WiFiUDP ntpUDP;
int gtmOffset = 0; //Using GMT as Timezone.h will adjust accordingly
int ntpPollInterval = 5;
NTPClient timeClient(ntpUDP, "pool.ntp.org", gtmOffset*60*60, ntpPollInterval*60*1000); //Set to EST and update every 5 minutes
//Configure DST
// US Eastern Time Zone (New York)
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240}; // Eastern Daylight Time = UTC - 4 hours
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300}; // Eastern Standard Time = UTC - 5 hours
Timezone usET(usEDT, usEST);
void setup()
{
Serial.begin(115200);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//Start logging
while(!Serial);
Serial.println();
Serial.println("ESP Logger - Log on internal flash memory");
sysLog.begin();
sysLog.setFlusherCallback(senderHelp);
Serial.println("Starting to log...");
//OTA support at /update
AsyncElegantOTA.begin(&server);
// WebSerial is accessible at "<IP Address>/webserial" in browser
WebSerial.begin(&server);
//Configure web server
server.on("/log", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/log/syslog.txt", "text/html");
});
server.begin();
//Start NTP client
timeClient.begin();
delay (5000);
if (timeClient.update()){
Serial.print ( "Adjust local clock" );
WebSerial.print ( "Adjust local clock" );
unsigned long epoch = timeClient.getEpochTime();
// Update local clock
setTime(epoch);
}else{
Serial.print ( "NTP update failed!!!" );
WebSerial.print ( "NTP update failed!!!" );
}
sysLog.flush(); //clear the log on startup, remove this line to keep logs forever.
}
void loop() {
timeClient.update();
delay(5000);
Serial.println(getEpochStringByParams(usET.toLocal(now())) + "This is a log entry!<br>");
WebSerial.println(getEpochStringByParams(usET.toLocal(now())) + " This has formatted time.");
sysLog.append("This is a log entry!!!<br>");
//sysLog.append(getEpochStringByParams(usET.toLocal(now())) + "This is a log entry!<br>"); //Write a log entry every 5 seconds
}
/**
* Flush a chuck of logged records. To exemplify, the records are
* flushed on Serial, but you are free to modify it to send data
* over the network.
*/
bool senderHelp(char* buffer, int n){
int index=0;
// Check if there is another string to print
while(index<n && strlen(&buffer[index])>0){
Serial.print("---");
int bytePrinted=Serial.print(&buffer[index]);
Serial.println("---");
// +1, the '\0' is processed
index += bytePrinted+1;
}
return true;
}
/* Library sources
https://github.com/fabianoriccardi/esp-logger
https://github.com/me-no-dev/ESPAsyncWebServer
https://github.com/ayushsharma82/AsyncElegantOTA
https://github.com/arduino-libraries/NTPClient
https://github.com/JChristensen/Timezone
*/