Initial project commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// Nathan Hinton
|
||||
// Main file for the hub node. Starting with the WIFI initialization.
|
||||
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "services/service_manager.h"
|
||||
#include "services/wifi_service.h"
|
||||
#include "services/web_service.h"
|
||||
#include "services/ota_service.h"
|
||||
|
||||
|
||||
ServiceManager services;
|
||||
|
||||
|
||||
WiFiService wifi;
|
||||
WebService web;
|
||||
OTAService ota;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
services.add(&wifi);
|
||||
services.add(&ota);
|
||||
services.add(&web);
|
||||
|
||||
services.begin();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
services.update();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "ota_service.h"
|
||||
|
||||
|
||||
void OTAService::begin()
|
||||
{
|
||||
ArduinoOTA.setHostname("ESP32-Dashboard");
|
||||
|
||||
ArduinoOTA.onStart([](){
|
||||
Serial.println("OTA started");
|
||||
});
|
||||
|
||||
ArduinoOTA.onEnd([](){
|
||||
Serial.println("OTA finished");
|
||||
});
|
||||
|
||||
ArduinoOTA.begin();
|
||||
|
||||
Serial.println("OTA ready");
|
||||
}
|
||||
|
||||
|
||||
void OTAService::update()
|
||||
{
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "service.h"
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
class OTAService : public Service {
|
||||
|
||||
public:
|
||||
|
||||
OTAService()
|
||||
: Service("OTA", 10)
|
||||
{}
|
||||
|
||||
void begin() override;
|
||||
void update() override;
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class Service {
|
||||
public:
|
||||
Service(const char* name, uint32_t intervalMs)
|
||||
: name(name),
|
||||
intervalMs(intervalMs),
|
||||
lastRun(0),
|
||||
lastExecutionUs(0)
|
||||
{}
|
||||
|
||||
virtual void begin() = 0;
|
||||
virtual void update() = 0;
|
||||
|
||||
void run()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
if (now - lastRun < intervalMs)
|
||||
return;
|
||||
|
||||
lastRun = now;
|
||||
|
||||
uint32_t start = micros();
|
||||
|
||||
update();
|
||||
|
||||
lastExecutionUs = micros() - start;
|
||||
}
|
||||
|
||||
const char* getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
uint32_t getExecutionTimeUs() {
|
||||
return lastExecutionUs;
|
||||
}
|
||||
|
||||
protected:
|
||||
const char* name;
|
||||
uint32_t intervalMs;
|
||||
uint32_t lastRun;
|
||||
uint32_t lastExecutionUs;
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "service_manager.h"
|
||||
|
||||
void ServiceManager::add(Service* service)
|
||||
{
|
||||
if(count < MAX_SERVICES)
|
||||
{
|
||||
services[count++] = service;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ServiceManager::begin()
|
||||
{
|
||||
for(uint8_t i = 0; i < count; i++)
|
||||
{
|
||||
services[i]->begin();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ServiceManager::update()
|
||||
{
|
||||
for(uint8_t i = 0; i < count; i++)
|
||||
{
|
||||
services[i]->run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "service.h"
|
||||
|
||||
#define MAX_SERVICES 10
|
||||
|
||||
class ServiceManager {
|
||||
public:
|
||||
|
||||
void add(Service* service);
|
||||
|
||||
void begin();
|
||||
|
||||
void update();
|
||||
|
||||
private:
|
||||
|
||||
Service* services[MAX_SERVICES];
|
||||
uint8_t count = 0;
|
||||
};
|
||||
@@ -0,0 +1,161 @@
|
||||
#include "web_service.h"
|
||||
|
||||
#define FIRMWARE_VERSION "1.0.0"
|
||||
|
||||
WebService::WebService()
|
||||
:
|
||||
Service("Web", 10),
|
||||
server(80)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
String formatUptime() {
|
||||
unsigned long seconds = millis() / 1000;
|
||||
|
||||
unsigned long days = seconds / 86400;
|
||||
seconds %= 86400;
|
||||
|
||||
unsigned long hours = seconds / 3600;
|
||||
seconds %= 3600;
|
||||
|
||||
unsigned long minutes = seconds / 60;
|
||||
seconds %= 60;
|
||||
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
"%lu days %02lu:%02lu:%02lu",
|
||||
days, hours, minutes, seconds);
|
||||
|
||||
return String(buffer);
|
||||
}
|
||||
|
||||
String ROOT_HTML = R"rawliteral(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>ESP32 Dashboard</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
text-align: center;
|
||||
margin-top: 50px;
|
||||
background: #111;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: #222;
|
||||
padding: 25px;
|
||||
margin: auto;
|
||||
width: 80%;
|
||||
max-width: 400px;
|
||||
border-radius: 15px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #00ff99;
|
||||
}
|
||||
|
||||
#uptime {
|
||||
font-size: 28px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
function updateUptime() {
|
||||
fetch('/uptime')
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById("uptime").innerHTML = data;
|
||||
});
|
||||
}
|
||||
|
||||
setInterval(updateUptime, 1000);
|
||||
window.onload = updateUptime;
|
||||
</script>
|
||||
|
||||
<script>
|
||||
function getVersion() {
|
||||
fetch('/version')
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById("firmware_version").innerHTML = data;
|
||||
});
|
||||
}
|
||||
|
||||
window.onload = getVersion;
|
||||
</script>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="card">
|
||||
<h1>ESP32 Dashboard OTA</h1>
|
||||
<p>Device uptime:</p>
|
||||
<div id="uptime">Loading...</div>
|
||||
</div>
|
||||
<footer>
|
||||
<div id="firmware_version">Loading...</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
)rawliteral";
|
||||
|
||||
|
||||
// server.send(200, "text/html", html);
|
||||
// }
|
||||
//
|
||||
// void handleUptime() {
|
||||
// server.send(200, "text/plain", formatUptime());
|
||||
// }
|
||||
//
|
||||
// void handleVersion() {
|
||||
// server.send(200, "text/plain", FIRMWARE_VERSION);
|
||||
// }
|
||||
//
|
||||
|
||||
void WebService::begin() {
|
||||
// Root path
|
||||
server.on("/", [this](){
|
||||
server.send(
|
||||
200,
|
||||
"text/html",
|
||||
ROOT_HTML
|
||||
);
|
||||
});
|
||||
|
||||
// Version path
|
||||
server.on("/version", [this](){
|
||||
server.send(
|
||||
200,
|
||||
"text/plain",
|
||||
FIRMWARE_VERSION
|
||||
);
|
||||
});
|
||||
|
||||
// Uptime path
|
||||
server.on("/uptime", [this]() {
|
||||
server.send(
|
||||
200,
|
||||
"text/plain",
|
||||
formatUptime()
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
|
||||
server.begin();
|
||||
|
||||
Serial.println("Web server started");
|
||||
}
|
||||
|
||||
|
||||
void WebService::update()
|
||||
{
|
||||
server.handleClient();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "service.h"
|
||||
#include <WebServer.h>
|
||||
|
||||
|
||||
class WebService : public Service {
|
||||
|
||||
public:
|
||||
|
||||
WebService();
|
||||
|
||||
void begin() override;
|
||||
void update() override;
|
||||
|
||||
private:
|
||||
|
||||
WebServer server;
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "wifi_service.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
|
||||
const char* ssid = "ESP32-Dashboard";
|
||||
const char* password = "esp32password";
|
||||
|
||||
|
||||
WiFiService::WiFiService()
|
||||
:
|
||||
Service("WiFi", 1000)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void WiFiService::begin()
|
||||
{
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
WiFi.softAP(
|
||||
ssid,
|
||||
password
|
||||
);
|
||||
|
||||
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
}
|
||||
|
||||
|
||||
void WiFiService::update()
|
||||
{
|
||||
// Future:
|
||||
// monitor connected clients
|
||||
// reconnect logic
|
||||
// diagnostics
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "service.h"
|
||||
|
||||
|
||||
class WiFiService : public Service {
|
||||
|
||||
public:
|
||||
|
||||
WiFiService();
|
||||
|
||||
void begin() override;
|
||||
void update() override;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user