Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

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