67 lines
1.7 KiB
C++
67 lines
1.7 KiB
C++
#include <Arduino.h>
|
|
#include <Ethernet.h>
|
|
#include "WebServerData.h"
|
|
#include "WebServer.h"
|
|
#include "DebugLog.h"
|
|
|
|
// Constructor
|
|
WebServer::WebServer() : _server(80) {}
|
|
|
|
WebServer::WebServer(byte mac[6], IPAddress ip, uint16_t port) : _ip(ip), _server(port)
|
|
{
|
|
memcpy(_mac, mac, 6);
|
|
}
|
|
|
|
// Initialize the Ethernet and start the server
|
|
void WebServer::init()
|
|
{
|
|
DebugLog::log("Starting WebServer mac: " + String(_mac[0]) + String(_mac[1]) + ", IP: " + _ip);
|
|
Ethernet.begin(_mac, _ip);
|
|
DebugLog::log("Ethernet IP: " + Ethernet.localIP());
|
|
_server.begin();
|
|
DebugLog::log("Server Start");
|
|
}
|
|
|
|
EthernetClient client;
|
|
// Handle client requests
|
|
WebServerData WebServer::getWebServerData()
|
|
{
|
|
// Check if a client has connected
|
|
client = _server.available();
|
|
if (client)
|
|
{
|
|
if (client.connected())
|
|
{
|
|
//DebugLog::log("Client connected");
|
|
if (client.available())
|
|
{
|
|
|
|
String request = client.readStringUntil('\r');
|
|
// client.flush(); // Clear the incoming buffer after reading the GET request
|
|
// If you've gotten to the end of the line (received a newline
|
|
// character) and the line is blank, the http request has ended,
|
|
// so you can send a reply
|
|
if (request.startsWith("GET"))
|
|
{
|
|
DebugLog::log("GET WEB SERVER DATA: " + request);
|
|
WebServerData webServerData = WebServerData(client, request);
|
|
return webServerData;
|
|
}
|
|
}
|
|
}
|
|
// Give the web browser time to receive the data
|
|
}
|
|
|
|
return WebServerData(client); // Return a default-constructed WebServerData object if no client is available
|
|
}
|
|
|
|
void WebServer::endServer()
|
|
{
|
|
if (_server.available())
|
|
{
|
|
delay(10);
|
|
// Close the connection
|
|
client.stop();
|
|
}
|
|
}
|