Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#include <FS.h>
2
#include <LittleFS.h>
3
#include <AsyncFsWebServer.h>
4
 
5
AsyncFsWebServer server(LittleFS, 80, "esphost");
6
 
7
// Timezone definition to get properly time from NTP server
8
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
9
#include <time.h>
10
 
11
struct tm ntpTime;
12
const char* basePath = "/csv";
13
 
14
////////////////////////////////  Filesystem  /////////////////////////////////////////
15
bool startFilesystem(){
16
  if (LittleFS.begin()){
17
    server.printFileList(LittleFS, "/", 2);
18
    return true;
19
  }
20
  else {
21
    Serial.println("ERROR on mounting filesystem. It will be formmatted!");
22
    LittleFS.format();
23
    ESP.restart();
24
  }
25
  return false;
26
}
27
 
28
//////////////////////////// Append a row to csv file ///////////////////////////////////
29
bool appenRow() {
30
 
31
  getLocalTime(&ntpTime, 10);
32
 
33
  char filename[32];
34
  snprintf(filename, sizeof(filename),
35
    "%s/%04d_%02d_%02d.csv",
36
    basePath,
37
    ntpTime.tm_year + 1900,
38
    ntpTime.tm_mon + 1,
39
    ntpTime.tm_mday
40
  );
41
 
42
  File file;
43
  if (LittleFS.exists(filename)) {
44
    file = LittleFS.open(filename, "a");   // Append to existing file
45
  }
46
  else {
47
    file = LittleFS.open(filename, "w");   // Create a new file
48
    file.println("timestamp, free heap, largest free block, connected, wifi strength");
49
  }
50
 
51
  if (file) {
52
    char timestamp[25];
53
    strftime(timestamp, sizeof(timestamp), "%c", &ntpTime);
54
 
55
    char row[64];
56
  #ifdef ESP32
57
      snprintf(row, sizeof(row), "%s, %d, %d, %s, %d",
58
        timestamp,
59
        heap_caps_get_free_size(0),
60
        heap_caps_get_largest_free_block(0),
61
        (WiFi.status() == WL_CONNECTED) ? "true" : "false",
62
        WiFi.RSSI()
63
      );
64
  #elif defined(ESP8266)
65
      uint32_t free;
66
      uint32_t max;
67
      ESP.getHeapStats(&free, &max, nullptr);
68
      snprintf(row, sizeof(row),
69
        "%s, %d, %d, %s, %d",
70
        timestamp, free, max,
71
        (WiFi.status() == WL_CONNECTED) ? "true" : "false",
72
        WiFi.RSSI()
73
      );
74
  #endif
75
    Serial.println(row);
76
    file.println(row);
77
    file.close();
78
    return true;
79
  }
80
 
81
  return false;
82
}
83
 
84
 
85
void setup() {
86
    Serial.begin(115200);
87
    delay(1000);
88
    startFilesystem();
89
 
90
	// Try to connect to WiFi (will start AP if not connected after timeout)
91
	if (!server.startWiFi(10000)) {
92
		Serial.println("\nWiFi not connected! Starting AP mode...");
93
		server.startCaptivePortal("ESP32_LOGGER", "123456789", "/setup");
94
	}
95
 
96
    // Enable ACE FS file web editor and add FS info callback fucntion
97
    server.enableFsCodeEditor();
98
 
99
    // Start server
100
    server.init();
101
    Serial.print(F("Async ESP Web Server started on IP Address: "));
102
    Serial.println(server.getServerIP());
103
    Serial.println(F(
104
        "This is \"scvLogger.ino\" example.\n"
105
        "Open /setup page to configure optional parameters.\n"
106
        "Open /edit page to view, edit or upload example or your custom webserver source files."
107
    ));
108
 
109
    // Set NTP servers
110
    #ifdef ESP8266
111
    configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
112
    #elif defined(ESP32)
113
    configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
114
    #endif
115
    // Wait for NTP sync (with timeout)
116
    getLocalTime(&ntpTime, 5000);
117
 
118
 
119
    // Create csv logs folder if not exists
120
    if (!LittleFS.exists(basePath)) {
121
      LittleFS.mkdir(basePath);
122
    }
123
}
124
 
125
void loop() {
126
    if (server.isAccessPointMode())
127
        server.updateDNS();
128
 
129
    static uint32_t updateTime;
130
    if (millis()- updateTime > 30000) {
131
        updateTime = millis();
132
        appenRow();
133
    }
134
}