Platform.ini
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_dtr = 0
monitor_rts = 0
monitor_speed = 115200
lib_deps =
bblanchon/ArduinoJson @ ^7.2.1
256dpi/MQTT @ ^2.5.2
ayushsharma82/ElegantOTA @ ^3.1.1
majicdesigns/MD_MAX72XX @ ^3.5.1
majicdesigns/MD_Parola @ ^3.7.2
arduino-libraries/NTPClient@^3.2.1
pfeerick/elapsedMillis @ ^1.0.6
plageoj/UrlEncode @ ^1.0.1
build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1
Basic file structure:
/project-folder/
├── main.ino or main.cpp
├── mywifi.h
├── myota.h
├── myntp.h
├── mywebserver.h
mywifi.h
#ifndef MYWIFI_H
#define MYWIFI_H
#include // Required for WiFi functionality
// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* hostname = "HOSTNAME";
String IPaddr;
// Function to initialize WiFi
void wifiInit() {
WiFi.hostname(hostname);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
debug(".");
}
IPaddr = WiFi.localIP().toString();
debugln("\nConnected to WiFi");
debug("IP address: ");
debugln(IPaddr);
}
#endif
#endif
myota.h
#ifndef MYOTA_H
#define MYOTA_H
#include // Required for OTA functionality
#include "mywebserver.h" // Include the web server module
// Function to initialize ElegantOTA
void otaInit() {
ElegantOTA.begin(&server);
}
// Function to handle OTA updates in the loop
void otaLoop() {
ElegantOTA.loop();
}
#endif
mywebserver.h
#ifndef MYWEBSERVER_H
#define MYWEBSERVER_H
#include // Required for AsyncWebServer
#include // Required for ESPAsyncWebServer dependency
#include // Required for WiFi functionality
// Declare and define Web Server objects
AsyncWebServer server(80);
AsyncEventSource events("/time");
// HTML content
const char index_html[] PROGMEM = R"rawliteral(
ESP8266 Time Server
ESP8266 Time Server
--:--:--
OTA Update
)rawliteral";
void handleRoot(AsyncWebServerRequest *request) {
String message = "";
message += "ESP32-CAM Project: IoT Clock
";
message += "Hostname: " + String(hostname) + "
";
message += "IP Address: " + IPaddr + "
";
message += "ElegantOTA ready for firmware updates.
";
message += "";
request->send(200, "text/html", message);
}
// Function to initialize the web server
void webServerInit() {
// Route for root / web page
server.on("/", HTTP_GET, handleRoot);
server.on("/time", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});
// Handle Server-Sent Events (SSE) for time updates
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
debugln("Client reconnected! Last message ID: %u\n", client->lastId());
}
client->send("hello!", NULL, millis(), 1000);
});
server.addHandler(&events);
// Start server
server.begin();
}
#endif
myntp.h
#ifndef MYNTP_H
#define MYNTP_H
#include // Required for NTP functionality
#include // Required for NTPClient dependency
#include // Required for WiFi functionality
// Declare and define NTP objects
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.pagasa.dost.gov.ph", 28800, 60000); // Update every 60 seconds
// Function to initialize NTP
void ntpInit() {
timeClient.begin();
timeClient.update();
}
// Function to get the current time
String getCurrentTime() {
timeClient.update();
return timeClient.getFormattedTime();
}
#endif
main
#include "mywifi.h"
#include "myntp.h"
#include "mywebserver.h"
#include "myota.h"
void setup() {
Serial.begin(115200);
// Initialize WiFi
wifiInit();
// Initialize NTP
ntpInit();
// Initialize Web Server
webServerInit();
// Initialize OTA
otaInit();
}
void loop() {
static unsigned long lastUpdate = 0;
// Send time update every second to connected clients
if (millis() - lastUpdate >= 1000) {
lastUpdate = millis();
String currentTime = getCurrentTime();
events.send(currentTime.c_str(), "time");
}
// Handle OTA updates
otaLoop();
}