Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#if defined(ESP8266)
2
#include <ESP8266mDNS.h>
3
#elif defined(ESP32)
4
#include <ESPmDNS.h>
5
#endif
6
#include <FS.h>
7
#include <LittleFS.h>
8
#include <ArduinoJson.h>
9
#include <AsyncFsWebServer.h>  // https://github.com/cotestatnt/async-esp-fs-webserver
10
 
11
#include "index_htm.h"
12
 
13
#define FILESYSTEM LittleFS
14
 
15
char const* hostname = "fsbrowser";
16
AsyncFsWebServer server(FILESYSTEM, 80,hostname);
17
 
18
#ifndef LED_BUILTIN
19
#define LED_BUILTIN 2
20
#endif
21
#define BOOT_BUTTON 0
22
 
23
// Log messages both on Serial and WebSocket clients
24
void wsLogPrintf(bool toSerial, const char* format, ...) {
25
  char buffer[128];
26
  va_list args;
27
  va_start(args, format);
28
  vsnprintf(buffer, 128, format, args);
29
  va_end(args);
30
  server.wsBroadcast(buffer);
31
  if (toSerial)
32
    Serial.println(buffer);
33
}
34
 
35
// In this example a custom websocket event handler is used instead default
36
void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
37
  switch (type) {
38
    case WS_EVT_CONNECT:
39
      client->printf("{\"Websocket connected\": true, \"clients\": %u}", client->id());
40
      Serial.printf("Websocket client %u connected\n", client->id());
41
      break;
42
 
43
    case WS_EVT_DISCONNECT:
44
      Serial.printf("Websocket client %u connected\n", client->id());
45
      break;
46
 
47
    case WS_EVT_DATA:
48
      {
49
        AwsFrameInfo* info = (AwsFrameInfo*)arg;
50
        if (info->opcode == WS_TEXT) {
51
          char msg[len+1];
52
          msg[len] = '\0';
53
          memcpy(msg, data, len);
54
          Serial.printf("Received message \"%s\"\n", msg);
55
        }
56
      }
57
      break;
58
 
59
    default:
60
      break;
61
  }
62
}
63
 
64
// Test "config" values
65
String option1 = "Test option String";
66
uint32_t option2 = 1234567890;
67
uint8_t ledPin = LED_BUILTIN;
68
 
69
// Timezone definition to get properly time from NTP server
70
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
71
struct tm Time;
72
 
73
 
74
////////////////////////////////  NTP Time  /////////////////////////////////////
75
void getUpdatedtime(const uint32_t timeout) {
76
  uint32_t start = millis();
77
  Serial.print("Sync time...");
78
  while (millis() - start < timeout && Time.tm_year <= (1970 - 1900)) {
79
    time_t now = time(nullptr);
80
    Time = *localtime(&now);
81
    delay(5);
82
  }
83
  Serial.println(" done.");
84
}
85
 
86
 
87
////////////////////////////////  Filesystem  /////////////////////////////////////////
88
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
89
    Serial.printf("\nListing directory: %s\n", dirname);
90
    File root = fs.open(dirname, "r");
91
    if(!root){
92
        Serial.println("- failed to open directory");
93
        return;
94
    }
95
    if(!root.isDirectory()){
96
        Serial.println(" - not a directory");
97
        return;
98
    }
99
    File file = root.openNextFile();
100
    while(file){
101
        if(file.isDirectory()){
102
            if(levels){
103
            #ifdef ESP32
104
			  listDir(fs, file.path(), levels - 1);
105
			#elif defined(ESP8266)
106
			  listDir(fs, file.fullName(), levels - 1);
107
			#endif
108
            }
109
        } else {
110
            Serial.printf("|__ FILE: %s (%d bytes)\n",file.name(), file.size());
111
        }
112
        file = root.openNextFile();
113
    }
114
}
115
 
116
bool startFilesystem() {
117
  if (FILESYSTEM.begin()) {
118
    listDir(LittleFS, "/", 1);
119
    return true;
120
  } else {
121
    Serial.println("ERROR on mounting filesystem. It will be reformatted!");
122
    FILESYSTEM.format();
123
    ESP.restart();
124
  }
125
  return false;
126
}
127
 
128
 
129
////////////////////  Load and save application configuration from filesystem  ////////////////////
130
void saveApplicationConfig() {
131
  File file = server.getConfigFile("r");
132
  DynamicJsonDocument doc(file.size() * 1.33);
133
  doc["Option 1"] = option1;
134
  doc["Option 2"] = option2;
135
  doc["LED Pin"] = ledPin;
136
  serializeJsonPretty(doc, file);
137
  file.close();
138
  delay(1000);
139
  ESP.restart();
140
}
141
 
142
bool loadApplicationConfig() {
143
  if (FILESYSTEM.exists(server.getConfiFileName())) {
144
    File file = server.getConfigFile("r");
145
    DynamicJsonDocument doc(file.size() * 1.33);
146
    DeserializationError error = deserializeJson(doc, file);
147
    file.close();
148
    if (!error) {
149
      option1 = doc["Option 1"].as<String>();
150
      option2 = doc["Option 2"];
151
      ledPin = doc["LED Pin"];
152
      return true;
153
    } else {
154
      Serial.print(F("Failed to deserialize JSON. Error: "));
155
      Serial.println(error.c_str());
156
    }
157
  }
158
  return false;
159
}
160
 
161
 
162
void setup() {
163
  pinMode(LED_BUILTIN, OUTPUT);
164
  pinMode(BOOT_BUTTON, INPUT_PULLUP);
165
 
166
  Serial.begin(115200);
167
  delay(1000);
168
 
169
  // FILESYSTEM INIT
170
  if (startFilesystem()) {
171
    // Load configuration (if not present, default will be created when webserver will start)
172
    if (loadApplicationConfig())
173
      Serial.println(F("Application option loaded"));
174
    else
175
      Serial.println(F("Application options NOT loaded!"));
176
  }
177
 
178
  // Try to connect to WiFi (will start AP if not connected after timeout)
179
  if (!server.startWiFi(10000)) {
180
    Serial.println("\nWiFi not connected! Starting AP mode...");
181
    server.startCaptivePortal("ESP_AP", "123456789", "/setup");
182
  }
183
 
184
  // Configure /setup page
185
  server.addOptionBox("My Options");
186
  server.addOption("LED Pin", ledPin);
187
  server.addOption("Option 1", option1.c_str());
188
  server.addOption("Option 2", option2);
189
 
190
  // Add custom page handlers
191
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
192
    request->send(200, "text/html", homepage);
193
  });
194
 
195
  // Enable ACE FS file web editor and add FS info callback function
196
  server.enableFsCodeEditor();
197
  /*
198
  * Getting FS info (total and free bytes) is strictly related to
199
  * filesystem library used (LittleFS, FFat, SPIFFS etc etc)
200
  * (On ESP8266 will be used "built-in" fsInfo data type)
201
  */
202
#ifdef ESP32
203
  server.setFsInfoCallback([](fsInfo_t* fsInfo) {
204
    fsInfo->fsName = "LittleFS";
205
    fsInfo->totalBytes = LittleFS.totalBytes();
206
    fsInfo->usedBytes = LittleFS.usedBytes();  
207
  });
208
#endif
209
 
210
  // Init with custom WebSocket event handler and start server
211
  server.init(onWsEvent);
212
 
213
  Serial.print(F("ESP Web Server started on IP Address: "));
214
  Serial.println(server.getServerIP());
215
  Serial.println(F(
216
    "This is \"withWebSocket.ino\" example.\n"
217
    "Open /setup page to configure optional parameters.\n"
218
    "Open /edit page to view, edit or upload example or your custom webserver source files."
219
  ));
220
 
221
  // Set hostname
222
#ifdef ESP8266
223
  WiFi.hostname(hostname);
224
  configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
225
#elif defined(ESP32)
226
  WiFi.setHostname(hostname);
227
  configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
228
#endif
229
 
230
  // Start MDSN responder
231
  if (WiFi.status() == WL_CONNECTED) {
232
    if (MDNS.begin(hostname)) {
233
      Serial.println(F("MDNS responder started."));
234
      Serial.printf("You should be able to connect with address  http://%s.local/\n", hostname);
235
      // Add service to MDNS-SD
236
      MDNS.addService("http", "tcp", 80);
237
    }
238
  }
239
}
240
 
241
 
242
void loop() {
243
 
244
  if (digitalRead(BOOT_BUTTON) == LOW) {
245
    wsLogPrintf(true, "Button on GPIO %d clicked", BOOT_BUTTON);
246
    delay(1000);
247
  }
248
 
249
  if (WiFi.status() == WL_CONNECTED) {
250
#ifdef ESP8266
251
    MDNS.update();
252
#endif
253
  }
254
 
255
  // Send ESP system time (epoch) to WS client
256
  static uint32_t sendToClientTime;
257
  if (millis() - sendToClientTime > 1000) {
258
    sendToClientTime = millis();
259
    time_t now = time(nullptr);
260
    wsLogPrintf(false, "{\"esptime\": %d}", (int)now);
261
  }
262
}