Skip to content

Commit 477a22a

Browse files
Added sensor logging on SPIFFS.
1 parent a58348f commit 477a22a

File tree

6 files changed

+907
-60
lines changed

6 files changed

+907
-60
lines changed

ESP8266-temp-server.ino

Lines changed: 93 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22
#include <ESP8266WiFi.h>
33
#include <ESPAsyncTCP.h>
44
#include <ESPAsyncWebServer.h>
5+
#include <FS.h>
6+
#include <time.h> // time() ctime()
7+
#include <sys/time.h> // struct timeval
8+
59
#include "index_htm.h"
10+
#include "logs_htm.h"
611

712
#define ONE_WIRE_BUS D1
813

9-
const char * WIFISSID = "yourSSID";
10-
const char * WIFIPSK = "yourPSK";
14+
#define TZ 1 // (utc+) TZ in hours
15+
#define DST_MN 60 // use 60mn for summer time in some countries
16+
#define TZ_MN ((TZ)*60)
17+
#define TZ_SEC ((TZ)*3600)
18+
#define DST_SEC ((DST_MN)*60)
19+
20+
const char WIFISSID[] = "yourSSID";
21+
const char WIFIPSK[] = "yourPSK";
22+
23+
const float SENSOR_ERROR = -273.15;
1124

1225
AsyncWebServer server(80);
1326

14-
float currentTemp = -273.15;
27+
float currentTemp = SENSOR_ERROR;
28+
bool dstStatus = true;
1529

1630
OneWire ds( ONE_WIRE_BUS ); // (a 4.7K resistor is necessary)
1731

@@ -20,9 +34,7 @@ void setup(void)
2034
Serial.begin( 115200 );
2135
Serial.println();
2236

23-
analogWriteFreq( 40000 );
24-
pinMode( BUILTIN_LED, OUTPUT );
25-
digitalWrite( BUILTIN_LED, LOW );
37+
configTime( TZ_SEC, DST_SEC, "nl.pool.ntp.org" );
2638

2739
WiFi.mode( WIFI_STA );
2840
if ( !connectWifi() )
@@ -36,12 +48,6 @@ void setup(void)
3648
delay( 100 );
3749
}
3850
}
39-
else
40-
{
41-
Serial.println( "Connected!" );
42-
Serial.println( WiFi.localIP().toString() );
43-
digitalWrite( BUILTIN_LED, HIGH );
44-
}
4551

4652
static const char * HTML_HEADER = "text/html";
4753

@@ -51,45 +57,77 @@ void setup(void)
5157
request->send(response);
5258
});
5359

60+
5461
server.on( "/data", HTTP_GET, [] ( AsyncWebServerRequest * request )
62+
{
63+
if ( currentTemp == SENSOR_ERROR )
64+
{
65+
request->send( 200, HTML_HEADER, F( "<p style=\"font-size:100px;color:red;\">SENSOR ERROR</p>" ) );
66+
}
67+
else
68+
{
69+
AsyncResponseStream *response = request->beginResponseStream( HTML_HEADER );
70+
response->printf( "%.1f&deg;C", currentTemp );
71+
request->send( response );
72+
}
73+
});
74+
75+
server.on( "/logs", HTTP_GET, [] ( AsyncWebServerRequest * request )
76+
{
77+
AsyncWebServerResponse *response = request->beginResponse_P( 200, HTML_HEADER, logs_htm, logs_htm_len );
78+
request->send(response);
79+
});
80+
81+
server.on( "/files", HTTP_GET, [] ( AsyncWebServerRequest * request )
5582
{
5683
AsyncResponseStream *response = request->beginResponseStream( HTML_HEADER );
57-
response->printf( "%.1f&deg;C", currentTemp );
84+
Dir dir = SPIFFS.openDir("/");
85+
while (dir.next()) response->printf( "%s\n", dir.fileName().c_str() );
5886
request->send( response );
5987
});
6088

89+
server.on( "/dststatus", HTTP_GET, [] ( AsyncWebServerRequest * request )
90+
{
91+
if ( request->hasArg( "dst" ) )
92+
{
93+
if ( request->arg( "dst" ) == "on" ) dstStatus = true;
94+
else if ( request->arg( "dst" ) == "off" ) dstStatus = false;
95+
}
96+
AsyncResponseStream *response = request->beginResponseStream( HTML_HEADER );
97+
response->printf( "DST is %s", dstStatus ? "on" : "off" );
98+
request->send( response );
99+
});
100+
101+
server.serveStatic( "/", SPIFFS, "/" );
102+
61103
server.onNotFound( []( AsyncWebServerRequest * request )
62104
{
63-
Serial.printf( "Not found http://%s%s\n", request->host().c_str(), request->url().c_str());
64105
request->send( 404 );
65106
});
66107

67108
DefaultHeaders::Instance().addHeader( "Access-Control-Allow-Origin", "*" );
68109
server.begin();
69-
}
70110

71-
void loop(void)
72-
{
73-
const uint16_t delayTime = 1000; /* milliseconds */
74-
static unsigned long nextHeartBeat = millis() + delayTime;
111+
time_t now;
112+
struct tm timeinfo;
75113

76-
if ( (long)( millis() - nextHeartBeat ) >= 0 )
77-
{
78-
analogWrite( BUILTIN_LED, PWMRANGE >> 5 );
79-
delay(3);
80-
digitalWrite( BUILTIN_LED, HIGH );
81-
nextHeartBeat += delayTime;
114+
while ( timeinfo.tm_year < ( 2016 - 1900 ) ) {
115+
delay(50);
116+
time( &now );
117+
localtime_r( &now, &timeinfo );
82118
}
119+
Serial.print( F( "Time synced at " ) );
120+
Serial.println( asctime( localtime( &now ) ) );
83121

84-
if ( !WiFi.isConnected() )
85-
{
86-
analogWriteFreq( 10 );
87-
analogWrite( BUILTIN_LED, PWMRANGE / 2 );
88-
Serial.println( "WiFi is disconnected." );
89-
if ( !connectWifi() ) return;
90-
Serial.println( "WiFi just reconnected" );
91-
}
122+
if ( !SPIFFS.begin() )
123+
Serial.println( F( "SPIFFS could not be mounted" ) );
124+
}
125+
126+
time_t nextLogTime = 0;
127+
const time_t logDelaySeconds = 120;
92128

129+
void loop(void)
130+
{
93131
byte addr[8];
94132

95133
if ( !ds.search(addr))
@@ -100,7 +138,7 @@ void loop(void)
100138

101139
if (OneWire::crc8(addr, 7) != addr[7])
102140
{
103-
Serial.println("Sensor CRC is not valid!");
141+
Serial.println( F( "Sensor CRC is not valid!" ) );
104142
return;
105143
}
106144

@@ -156,6 +194,20 @@ void loop(void)
156194
//// default is 12 bit resolution, 750 ms conversion time
157195
}
158196
currentTemp = (float)raw / 16.0;
197+
198+
time_t now;
199+
time( &now );
200+
if ( now >= nextLogTime )
201+
{
202+
logToSPIFFS( now );
203+
nextLogTime = now + logDelaySeconds;
204+
}
205+
if ( !WiFi.isConnected() )
206+
{
207+
Serial.println( F( "WiFi is disconnected." ) );
208+
if ( !connectWifi() ) return;
209+
Serial.println( F( "WiFi just reconnected" ) );
210+
}
159211
}
160212

161213
bool connectWifi()
@@ -164,20 +216,19 @@ bool connectWifi()
164216

165217
if ( netWorks )
166218
{
167-
for (int i = 0; i < netWorks; ++i)
219+
for ( int i = 0; i < netWorks; ++i )
168220
{
169221
if ( WiFi.SSID(i) == WIFISSID )
170222
{
171-
Serial.print( "Found " );
172-
Serial.print(WiFi.SSID(i));
223+
Serial.print( F( "Found " ) );
224+
Serial.print( WiFi.SSID(i) );
173225
Serial.print( " " );
174-
Serial.print(WiFi.RSSI(i));
175-
Serial.println("dB");
226+
Serial.print( WiFi.RSSI(i) );
227+
Serial.println( F( "dB" ) );
176228
WiFi.persistent( false );
177-
Serial.println( "Connecting..." );
229+
Serial.print( F( "Connecting" ) );
178230
WiFi.disconnect();
179-
WiFi.mode(WIFI_STA);
180-
digitalWrite( BUILTIN_LED, LOW );
231+
WiFi.mode( WIFI_STA );
181232
WiFi.begin( WIFISSID, WIFIPSK );
182233
unsigned long timeout = millis() + 15000;
183234
while ( (long)( millis() - timeout ) < 0 && ( WiFi.status() != WL_CONNECTED ) )
@@ -186,7 +237,6 @@ bool connectWifi()
186237
Serial.print ( F( "." ) );
187238
}
188239
Serial.println();
189-
digitalWrite( BUILTIN_LED, HIGH );
190240
}
191241
}
192242
}

index.htm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <!--prevent favicon requests-->
88
<style>
99
body{
10-
margin:0;
11-
text-align: center;
10+
margin:0 auto;
11+
text-align:center;
12+
font-variant: small-caps;
13+
font-size:20px;
14+
font-family: monospace;
15+
}
16+
#linkBar{
17+
background-color:aqua;
18+
margin:0;
1219
}
1320
#data{
1421
line-height: 300px;
@@ -20,6 +27,7 @@
2027
</style>
2128
</head>
2229
<body>
30+
<p id="linkBar"><a class="systemLink" href="logs">LOGS</a></p>
2331
<h1 style="text-align:center;">ESP8266-temp-server</h1>
2432
<script>
2533
function loaded_log(str){

index_htm.h

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ const uint8_t index_htm[] PROGMEM = {
2626
0x2d, 0x70, 0x72, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x20, 0x66, 0x61, 0x76,
2727
0x69, 0x63, 0x6f, 0x6e, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
2828
0x73, 0x2d, 0x2d, 0x3e, 0x0a, 0x3c, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e,
29-
0x0a, 0x62, 0x6f, 0x64, 0x79, 0x7b, 0x0a, 0x6d, 0x61, 0x72, 0x67, 0x69,
30-
0x6e, 0x3a, 0x30, 0x3b, 0x0a, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x61, 0x6c,
31-
0x69, 0x67, 0x6e, 0x3a, 0x20, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3b,
32-
0x0a, 0x7d, 0x0a, 0x23, 0x64, 0x61, 0x74, 0x61, 0x7b, 0x0a, 0x6c, 0x69,
33-
0x6e, 0x65, 0x2d, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x20, 0x33,
34-
0x30, 0x30, 0x70, 0x78, 0x3b, 0x0a, 0x66, 0x6f, 0x6e, 0x74, 0x2d, 0x73,
35-
0x69, 0x7a, 0x65, 0x3a, 0x20, 0x32, 0x35, 0x30, 0x70, 0x78, 0x3b, 0x0a,
36-
0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x61, 0x75, 0x74, 0x6f,
37-
0x3b, 0x0a, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x3a, 0x20,
38-
0x68, 0x69, 0x64, 0x64, 0x65, 0x6e, 0x3b, 0x0a, 0x62, 0x61, 0x63, 0x6b,
39-
0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d, 0x63, 0x6f, 0x6c, 0x6f, 0x72,
40-
0x3a, 0x20, 0x61, 0x71, 0x75, 0x61, 0x3b, 0x0a, 0x7d, 0x0a, 0x3c, 0x2f,
41-
0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x65, 0x61,
42-
0x64, 0x3e, 0x0a, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x3c, 0x68,
29+
0x0a, 0x62, 0x6f, 0x64, 0x79, 0x7b, 0x0a, 0x20, 0x20, 0x6d, 0x61, 0x72,
30+
0x67, 0x69, 0x6e, 0x3a, 0x30, 0x20, 0x61, 0x75, 0x74, 0x6f, 0x3b, 0x0a,
31+
0x20, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x61, 0x6c, 0x69, 0x67, 0x6e,
32+
0x3a, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3b, 0x0a, 0x20, 0x20, 0x66,
33+
0x6f, 0x6e, 0x74, 0x2d, 0x76, 0x61, 0x72, 0x69, 0x61, 0x6e, 0x74, 0x3a,
34+
0x20, 0x73, 0x6d, 0x61, 0x6c, 0x6c, 0x2d, 0x63, 0x61, 0x70, 0x73, 0x3b,
35+
0x0a, 0x20, 0x20, 0x66, 0x6f, 0x6e, 0x74, 0x2d, 0x73, 0x69, 0x7a, 0x65,
36+
0x3a, 0x32, 0x30, 0x70, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x66, 0x6f, 0x6e,
37+
0x74, 0x2d, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x3a, 0x20, 0x6d, 0x6f,
38+
0x6e, 0x6f, 0x73, 0x70, 0x61, 0x63, 0x65, 0x3b, 0x0a, 0x7d, 0x0a, 0x23,
39+
0x6c, 0x69, 0x6e, 0x6b, 0x42, 0x61, 0x72, 0x7b, 0x0a, 0x20, 0x20, 0x62,
40+
0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d, 0x63, 0x6f,
41+
0x6c, 0x6f, 0x72, 0x3a, 0x61, 0x71, 0x75, 0x61, 0x3b, 0x0a, 0x20, 0x20,
42+
0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e, 0x3a, 0x30, 0x3b, 0x0a, 0x7d, 0x0a,
43+
0x23, 0x64, 0x61, 0x74, 0x61, 0x7b, 0x0a, 0x6c, 0x69, 0x6e, 0x65, 0x2d,
44+
0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x20, 0x33, 0x30, 0x30, 0x70,
45+
0x78, 0x3b, 0x0a, 0x66, 0x6f, 0x6e, 0x74, 0x2d, 0x73, 0x69, 0x7a, 0x65,
46+
0x3a, 0x20, 0x32, 0x35, 0x30, 0x70, 0x78, 0x3b, 0x0a, 0x6d, 0x61, 0x72,
47+
0x67, 0x69, 0x6e, 0x3a, 0x20, 0x61, 0x75, 0x74, 0x6f, 0x3b, 0x0a, 0x6f,
48+
0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x3a, 0x20, 0x68, 0x69, 0x64,
49+
0x64, 0x65, 0x6e, 0x3b, 0x0a, 0x62, 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f,
50+
0x75, 0x6e, 0x64, 0x2d, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x61,
51+
0x71, 0x75, 0x61, 0x3b, 0x0a, 0x7d, 0x0a, 0x3c, 0x2f, 0x73, 0x74, 0x79,
52+
0x6c, 0x65, 0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a,
53+
0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x3c, 0x70, 0x20, 0x69, 0x64,
54+
0x3d, 0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x42, 0x61, 0x72, 0x22, 0x3e, 0x3c,
55+
0x61, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x73, 0x79, 0x73,
56+
0x74, 0x65, 0x6d, 0x4c, 0x69, 0x6e, 0x6b, 0x22, 0x20, 0x68, 0x72, 0x65,
57+
0x66, 0x3d, 0x22, 0x6c, 0x6f, 0x67, 0x73, 0x22, 0x3e, 0x4c, 0x4f, 0x47,
58+
0x53, 0x3c, 0x2f, 0x61, 0x3e, 0x3c, 0x2f, 0x70, 0x3e, 0x0a, 0x3c, 0x68,
4359
0x31, 0x20, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x78,
4460
0x74, 0x2d, 0x61, 0x6c, 0x69, 0x67, 0x6e, 0x3a, 0x63, 0x65, 0x6e, 0x74,
4561
0x65, 0x72, 0x3b, 0x22, 0x3e, 0x45, 0x53, 0x50, 0x38, 0x32, 0x36, 0x36,
@@ -92,4 +108,4 @@ const uint8_t index_htm[] PROGMEM = {
92108
0x63, 0x72, 0x69, 0x70, 0x74, 0x3e, 0x0a, 0x3c, 0x2f, 0x62, 0x6f, 0x64,
93109
0x79, 0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a
94110
};
95-
unsigned int index_htm_len = 1103;
111+
unsigned int index_htm_len = 1295;

logger.ino

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
void logToSPIFFS( const time_t now )
2+
{
3+
struct tm timeinfo;
4+
char fileName[17];
5+
char content[60];
6+
7+
localtime_r( &now, &timeinfo );
8+
strftime( fileName , sizeof( fileName ), "/%F.log", &timeinfo );
9+
10+
Serial.printf( "Current logfile: %s\n", fileName );
11+
12+
snprintf( content, sizeof( content ), "%i,%3.2f", now - DST_SEC - TZ_SEC, currentTemp );
13+
14+
if ( !writelnFile( SPIFFS, fileName, content ) )
15+
{
16+
Serial.println( F( "Something wrong writing to file" ) );
17+
}
18+
19+
Serial.println();
20+
Serial.println( F( "Files on spiffs:" ) );
21+
Dir dir = SPIFFS.openDir( "/" );
22+
while ( dir.next() ) {
23+
Serial.print( dir.fileName() );
24+
Serial.print( F( " size: " ) );
25+
File f = dir.openFile( "r" );
26+
Serial.println( f.size() );
27+
}
28+
Serial.println();
29+
}
30+
31+
bool writelnFile( fs::FS &fs, const char * path, const char * message )
32+
{
33+
File file = fs.open( path, "a+" );
34+
if ( !file )
35+
{
36+
Serial.println( F( "file could not be opened" ) );
37+
return false;
38+
}
39+
if ( !file.println( message ) )
40+
{
41+
Serial.println( F( "file could not be written to" ) );
42+
file.close();
43+
return false;
44+
}
45+
file.close();
46+
return true;
47+
}

0 commit comments

Comments
 (0)