Englebert
3 years ago
10 changed files with 360 additions and 7538 deletions
-
33src/CustomWiFi.cpp
-
24src/CustomWiFi.h
-
4src/Screen.cpp
-
7612src/Speak.cpp
-
12src/Speak.h
-
1src/Storage.cpp
-
153src/Web.cpp
-
23src/Web.h
-
33src/main.cpp
-
3src/main.h
@ -0,0 +1,33 @@ |
|||
#include "CustomWiFi.h"
|
|||
|
|||
CustomWiFi::CustomWiFi(void) { |
|||
// pinMode(CustomWiFi_INDICATOR, OUTPUT);
|
|||
last_wifi_check = 0; |
|||
wifi_check_duration = 10; |
|||
} |
|||
|
|||
void CustomWiFi::set_credential(char *ssid, char *passphrase) { |
|||
strcpy(CustomWiFi::_ssid, ssid); |
|||
strcpy(CustomWiFi::_passphrase, passphrase); |
|||
|
|||
// Initial value
|
|||
this->wifi_check_duration = 5000; |
|||
} |
|||
|
|||
|
|||
void CustomWiFi::begin(void) { |
|||
IPAddress local_ip(192,168,1,1); |
|||
IPAddress gateway(192,168,1,1); |
|||
IPAddress subnet(255,255,255,0); |
|||
|
|||
WiFi.softAP(WIFI_SSID, WIFI_PASSPHRASE); |
|||
delay(100); |
|||
|
|||
WiFi.softAPConfig(local_ip, gateway, subnet); |
|||
delay(100); |
|||
|
|||
IPAddress IP = WiFi.softAPIP(); |
|||
// Serial.print(F("AP IP address: "); Serial.prinln(IP);
|
|||
} |
|||
|
|||
CustomWiFi customwifi; |
@ -0,0 +1,24 @@ |
|||
#ifndef CUSTOMWIFI_H_ |
|||
#define CUSTOMWIFI_H_ |
|||
|
|||
#include <Arduino.h> |
|||
#include <WiFi.h> |
|||
#include <WiFiClient.h> |
|||
|
|||
#define WIFI_SSID "M5CoreTX" |
|||
#define WIFI_PASSPHRASE "LetMeIn123" |
|||
// #define CUSTOMWIFI_INDICATOR 2 |
|||
|
|||
class CustomWiFi { |
|||
public: |
|||
CustomWiFi(void); |
|||
void set_credential(char *ssid, char *passphrase); |
|||
void begin(void); |
|||
char _ssid[32], _passphrase[32]; |
|||
|
|||
private: |
|||
uint32_t last_wifi_check, wifi_check_duration; |
|||
}; |
|||
|
|||
extern CustomWiFi customwifi; |
|||
#endif |
7612
src/Speak.cpp
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,153 @@ |
|||
#include "Web.h"
|
|||
|
|||
WebServer server(PORT_HTTP); |
|||
bool update_status = false; |
|||
bool opened = false; |
|||
File root; |
|||
|
|||
WEB::WEB(void){ |
|||
} |
|||
|
|||
void WEB::begin(void) { |
|||
// Allow CORS
|
|||
server.enableCORS(); |
|||
|
|||
// Index
|
|||
server.on("/", HTTP_GET, []() { |
|||
// File html_handler = LITTLEFS.open("/index.html.gz", FILE_READ);
|
|||
// server.streamFile(html_handler, "text/html");
|
|||
// html_handler.close();
|
|||
server.send(200, "text/html", "MSCoreTX Version 0.1"); |
|||
server.sendHeader("Connection", "close"); |
|||
}); |
|||
|
|||
// Update
|
|||
server.on("/update", HTTP_POST, []() { |
|||
server.sendHeader("Connection", "close"); |
|||
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK. Device is rebooting"); |
|||
|
|||
if(Update.hasError()) { |
|||
update_status = false; |
|||
// web.set_update_status(false);
|
|||
} else { |
|||
delay(1000); |
|||
ESP.restart(); |
|||
} |
|||
}, []() { |
|||
HTTPUpload& upload = server.upload(); |
|||
if(!update_status) |
|||
update_status = true; |
|||
|
|||
if(upload.status == UPLOAD_FILE_START) { |
|||
Serial.printf("Update: %s\n", upload.filename.c_str()); |
|||
|
|||
// Start with maximum available size
|
|||
if(!Update.begin(UPDATE_SIZE_UNKNOWN)) |
|||
Update.printError(Serial); |
|||
} else if(upload.status == UPLOAD_FILE_WRITE) { |
|||
// Flashing firmware to ESP
|
|||
if( |
|||
Update.write(upload.buf, upload.currentSize) != upload.currentSize |
|||
) |
|||
Update.printError(Serial); |
|||
|
|||
} else if(upload.status == UPLOAD_FILE_END) { |
|||
if(Update.end(true)) |
|||
// True to set the size to the current progress
|
|||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); |
|||
else |
|||
Update.printError(Serial); |
|||
} |
|||
}); |
|||
|
|||
// Reset
|
|||
server.on("/reset", HTTP_GET, []() { |
|||
server.sendHeader("Connection", "close"); |
|||
server.send(200, "text/html", "1"); |
|||
storage.deleteFile(LITTLEFS, "/MSCoreTX"); |
|||
delay(1000); |
|||
ESP.restart(); |
|||
}); |
|||
|
|||
// Reboot
|
|||
server.on("/reboot", HTTP_GET, []() { |
|||
server.sendHeader("Connection", "close"); |
|||
server.send(200, "text/html", "1"); |
|||
delay(1000); |
|||
ESP.restart(); |
|||
}); |
|||
|
|||
// Remove files
|
|||
server.on("/erase", HTTP_GET, []() { |
|||
String filename; |
|||
uint8_t detected = 0; |
|||
|
|||
for(uint8_t i = 0; i < server.args(); i++) { |
|||
String value = server.arg(i); |
|||
|
|||
if(server.argName(i) == "filename") { |
|||
filename = value; |
|||
detected++; |
|||
} |
|||
} |
|||
|
|||
server.sendHeader("Connection", "close"); |
|||
if(detected) { |
|||
LITTLEFS.remove("/" + filename); |
|||
server.send(200, "text/plain", "REMOVED - [" + filename + "]"); |
|||
} else { |
|||
server.send(200, "text/plain", "Nothing remove."); |
|||
} |
|||
}); |
|||
|
|||
// TODO: create a way to upload files to the ESP32 and also handling it
|
|||
// REF: https://techtutorialsx.com/2019/03/04/esp32-arduino-serving-bootstrap/
|
|||
// SPIFFS File upload
|
|||
server.on("/webupload", HTTP_POST, []() { |
|||
server.sendHeader("Connection", "close"); |
|||
server.send(200, "text/plain", "webupload Done"); |
|||
}, []() { |
|||
HTTPUpload& upload = server.upload(); |
|||
|
|||
if(opened == false) { |
|||
opened = true; |
|||
root = LITTLEFS.open((String("/") + upload.filename).c_str(), FILE_WRITE); |
|||
if(!root) { |
|||
Serial.printf("Failed to open file for writing. [%s]\n", upload.filename.c_str()); |
|||
return; |
|||
} else { |
|||
Serial.println("Webupload file start."); |
|||
} |
|||
} |
|||
|
|||
if(upload.status == UPLOAD_FILE_START) { |
|||
Serial.printf("webupload: %s\n", upload.filename.c_str()); |
|||
} else if(upload.status == UPLOAD_FILE_WRITE) { |
|||
// Writting to SPIFFS
|
|||
if(root.write(upload.buf, upload.currentSize) != upload.currentSize) { |
|||
Serial.printf("Failed to write. [%s]\n", upload.filename.c_str()); |
|||
return; |
|||
} |
|||
} else if(upload.status == UPLOAD_FILE_END) { |
|||
root.close(); |
|||
opened = false; |
|||
Serial.printf("webupload done. [%s]\n", upload.filename.c_str()); |
|||
|
|||
#ifdef DEBUG_MODE
|
|||
Serial.printf("SPIFFS Files:\n"); |
|||
// list_files();
|
|||
storage.listDir(LITTLEFS, "/", 0); |
|||
#endif
|
|||
|
|||
} |
|||
}); |
|||
|
|||
server.begin(); |
|||
} |
|||
|
|||
void WEB::handler(void) { |
|||
server.handleClient(); |
|||
} |
|||
|
|||
|
|||
WEB web; |
@ -0,0 +1,23 @@ |
|||
#ifndef WEB_H_ |
|||
#define WEB_H_ |
|||
|
|||
#include <Arduino.h> |
|||
#include <LITTLEFS.h> |
|||
#include <WebServer.h> |
|||
#include <Update.h> |
|||
#include "Storage.h" |
|||
|
|||
#define PORT_HTTP 80 |
|||
|
|||
class WEB { |
|||
public: |
|||
WEB(void); |
|||
void begin(void); |
|||
void handler(void); |
|||
|
|||
private: |
|||
}; |
|||
|
|||
extern WEB web; |
|||
#endif |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue