ethernet webserver works too
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
#include "WebCfgServer.h"
|
#include "WebCfgServer.h"
|
||||||
#include "PreferencesKeys.h"
|
#include "PreferencesKeys.h"
|
||||||
#include "Version.h"
|
#include "Version.h"
|
||||||
#include "WifiEthServer.h"
|
#include "hardware/WifiEthServer.h"
|
||||||
|
|
||||||
WebCfgServer::WebCfgServer(NukiWrapper* nuki, Network* network, Preferences* preferences)
|
WebCfgServer::WebCfgServer(NukiWrapper* nuki, Network* network, EthServer* ethServer, Preferences* preferences)
|
||||||
: _server(new WifiEthServer(80)),
|
: _server(ethServer),
|
||||||
_nuki(nuki),
|
_nuki(nuki),
|
||||||
_network(network),
|
_network(network),
|
||||||
_preferences(preferences)
|
_preferences(preferences)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ enum class TokenType
|
|||||||
class WebCfgServer
|
class WebCfgServer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WebCfgServer(NukiWrapper* nuki, Network* network, Preferences* preferences);
|
WebCfgServer(NukiWrapper* nuki, Network* network, EthServer* ethServer, Preferences* preferences);
|
||||||
~WebCfgServer() = default;
|
~WebCfgServer() = default;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
|
|||||||
90
lib/WebServer/src/hardware/W5500EthClient.cpp
Normal file
90
lib/WebServer/src/hardware/W5500EthClient.cpp
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#include "W5500EthClient.h"
|
||||||
|
|
||||||
|
W5500EthClient::W5500EthClient(EthernetClient *wifiClient)
|
||||||
|
: _ethClient(wifiClient)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
W5500EthClient::~W5500EthClient()
|
||||||
|
{
|
||||||
|
_ethClient = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t W5500EthClient::connected()
|
||||||
|
{
|
||||||
|
return _ethClient->connected();
|
||||||
|
}
|
||||||
|
|
||||||
|
int W5500EthClient::setTimeout(uint32_t seconds)
|
||||||
|
{
|
||||||
|
// return _ethClient->setTimeout(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t W5500EthClient::write(const char *buffer, size_t size)
|
||||||
|
{
|
||||||
|
return _ethClient->write(buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress W5500EthClient::localIP()
|
||||||
|
{
|
||||||
|
return IPAddress(0,0,0,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500EthClient::stop()
|
||||||
|
{
|
||||||
|
_ethClient->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t W5500EthClient::write_P(const char *buf, size_t size)
|
||||||
|
{
|
||||||
|
return _ethClient->write(buf, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int W5500EthClient::available()
|
||||||
|
{
|
||||||
|
return _ethClient->available();
|
||||||
|
}
|
||||||
|
|
||||||
|
String W5500EthClient::readStringUntil(char terminator)
|
||||||
|
{
|
||||||
|
return _ethClient->readStringUntil(terminator);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t W5500EthClient::readBytes(char *buffer, size_t length)
|
||||||
|
{
|
||||||
|
return _ethClient->readBytes(buffer, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500EthClient::flush()
|
||||||
|
{
|
||||||
|
_ethClient->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
int W5500EthClient::read()
|
||||||
|
{
|
||||||
|
return _ethClient->read();
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long W5500EthClient::getTimeout(void)
|
||||||
|
{
|
||||||
|
return _ethClient->getTimeout();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t W5500EthClient::write(Stream &stream)
|
||||||
|
{
|
||||||
|
uint8_t * buf = (uint8_t *)malloc(1360);
|
||||||
|
if(!buf){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
size_t toRead = 0, toWrite = 0, written = 0;
|
||||||
|
size_t available = stream.available();
|
||||||
|
while(available){
|
||||||
|
toRead = (available > 1360)?1360:available;
|
||||||
|
toWrite = stream.readBytes(buf, toRead);
|
||||||
|
written += _ethClient->write(buf, toWrite);
|
||||||
|
available = stream.available();
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
return written;
|
||||||
|
}
|
||||||
29
lib/WebServer/src/hardware/W5500EthClient.h
Normal file
29
lib/WebServer/src/hardware/W5500EthClient.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
#include <EthernetClient.h>
|
||||||
|
#include "EthClient.h"
|
||||||
|
|
||||||
|
class W5500EthClient : public EthClient
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit W5500EthClient(EthernetClient* wifiClient);
|
||||||
|
virtual ~W5500EthClient();
|
||||||
|
|
||||||
|
uint8_t connected() override;
|
||||||
|
int available() override;
|
||||||
|
unsigned long getTimeout(void) override;
|
||||||
|
int setTimeout(uint32_t seconds) override;
|
||||||
|
int read() override;
|
||||||
|
size_t write(const char *buffer, size_t size) override;
|
||||||
|
size_t write(Stream &stream) override;
|
||||||
|
size_t write_P(const char *buf, size_t size) override;
|
||||||
|
String readStringUntil(char terminator) override;
|
||||||
|
size_t readBytes(char *buffer, size_t length) override;
|
||||||
|
IPAddress localIP() override;
|
||||||
|
void stop() override;
|
||||||
|
void flush() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EthernetClient* _ethClient;
|
||||||
|
};
|
||||||
75
lib/WebServer/src/hardware/W5500EthServer.cpp
Normal file
75
lib/WebServer/src/hardware/W5500EthServer.cpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#include "W5500EthServer.h"
|
||||||
|
|
||||||
|
|
||||||
|
W5500EthServer::W5500EthServer(IPAddress address, int port)
|
||||||
|
: EthServer(address, port),
|
||||||
|
_ethServer(address, port)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
W5500EthServer::W5500EthServer(int port)
|
||||||
|
: EthServer(port),
|
||||||
|
_ethServer(port)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500EthServer::close()
|
||||||
|
{
|
||||||
|
// _ethServer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500EthServer::begin(const int port)
|
||||||
|
{
|
||||||
|
_ethServer.begin(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500EthServer::setNoDelay(const bool value)
|
||||||
|
{
|
||||||
|
// _ethServer.setNoDelay(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
EthClient* W5500EthServer::available()
|
||||||
|
{
|
||||||
|
if(_W5500EthClient != nullptr)
|
||||||
|
{
|
||||||
|
delete _W5500EthClient;
|
||||||
|
_W5500EthClient = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ethClient = _ethServer.available();
|
||||||
|
_W5500EthClient = new W5500EthClient(&_ethClient);
|
||||||
|
return _W5500EthClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void W5500EthServer::discardClient()
|
||||||
|
{
|
||||||
|
if(_W5500EthClient != nullptr)
|
||||||
|
{
|
||||||
|
delete _W5500EthClient;
|
||||||
|
_W5500EthClient = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ethClient = EthernetClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// EthernetServerImpl
|
||||||
|
void EthernetServerImpl::begin(uint16_t port)
|
||||||
|
{
|
||||||
|
EthernetServer::begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
EthernetServerImpl::EthernetServerImpl(int address, int port)
|
||||||
|
: EthernetServer(port)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
EthernetServerImpl::EthernetServerImpl(int port)
|
||||||
|
: EthernetServer(port)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
35
lib/WebServer/src/hardware/W5500EthServer.h
Normal file
35
lib/WebServer/src/hardware/W5500EthServer.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "EthServer.h"
|
||||||
|
#include "W5500EthClient.h"
|
||||||
|
#include <WiFiServer.h>
|
||||||
|
#include <EthernetServer.h>
|
||||||
|
|
||||||
|
class EthernetServerImpl : public EthernetServer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EthernetServerImpl(int address, int port);
|
||||||
|
explicit EthernetServerImpl(int port);
|
||||||
|
|
||||||
|
virtual void begin(uint16_t port);
|
||||||
|
};
|
||||||
|
|
||||||
|
class W5500EthServer : public EthServer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
W5500EthServer(IPAddress address, int port);
|
||||||
|
explicit W5500EthServer(int port);
|
||||||
|
|
||||||
|
virtual EthClient* available();
|
||||||
|
virtual void discardClient();
|
||||||
|
|
||||||
|
virtual void begin(const int port = 80);
|
||||||
|
virtual void close();
|
||||||
|
|
||||||
|
virtual void setNoDelay(const bool value);
|
||||||
|
|
||||||
|
private:
|
||||||
|
EthernetServerImpl _ethServer;
|
||||||
|
EthernetClient _ethClient;
|
||||||
|
W5500EthClient* _W5500EthClient = nullptr;
|
||||||
|
};
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "WiFiManager.h"
|
#include "WiFiManager.h"
|
||||||
#include "WifiEthServer.h"
|
#include "hardware/WifiEthServer.h"
|
||||||
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
|
||||||
|
|||||||
37
main.cpp
37
main.cpp
@@ -6,14 +6,17 @@
|
|||||||
#include <FreeRTOS.h>
|
#include <FreeRTOS.h>
|
||||||
#include "PreferencesKeys.h"
|
#include "PreferencesKeys.h"
|
||||||
#include "PresenceDetection.h"
|
#include "PresenceDetection.h"
|
||||||
|
#include "hardware/W5500EthServer.h"
|
||||||
|
#include "hardware/WifiEthServer.h"
|
||||||
|
|
||||||
#define ESP32
|
#define ESP32
|
||||||
|
|
||||||
Network* network;
|
Network* network = nullptr;
|
||||||
WebCfgServer* webCfgServer;
|
WebCfgServer* webCfgServer = nullptr;
|
||||||
NukiWrapper* nuki;
|
NukiWrapper* nuki = nullptr;
|
||||||
PresenceDetection* presenceDetection;
|
PresenceDetection* presenceDetection = nullptr;
|
||||||
Preferences* preferences;
|
Preferences* preferences = nullptr;
|
||||||
|
EthServer* ethServer = nullptr;
|
||||||
|
|
||||||
void networkTask(void *pvParameters)
|
void networkTask(void *pvParameters)
|
||||||
{
|
{
|
||||||
@@ -79,14 +82,30 @@ uint32_t getRandomId()
|
|||||||
return deviceId;
|
return deviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void initEthServer(const NetworkDeviceType device)
|
||||||
|
{
|
||||||
|
switch (device)
|
||||||
|
{
|
||||||
|
case NetworkDeviceType::W5500:
|
||||||
|
ethServer = new W5500EthServer(80);
|
||||||
|
break;
|
||||||
|
case NetworkDeviceType::WiFi:
|
||||||
|
ethServer = new WifiEthServer(80);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ethServer = new WifiEthServer(80);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(NETWORK_SELECT, INPUT_PULLUP);
|
pinMode(NETWORK_SELECT, INPUT_PULLUP);
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi;
|
// const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi;
|
||||||
// const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500;
|
const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500;
|
||||||
|
|
||||||
preferences = new Preferences();
|
preferences = new Preferences();
|
||||||
preferences->begin("nukihub", false);
|
preferences->begin("nukihub", false);
|
||||||
@@ -100,8 +119,10 @@ void setup()
|
|||||||
preferences->putUInt(preference_deviceId, deviceId);
|
preferences->putUInt(preference_deviceId, deviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initEthServer(networkDevice);
|
||||||
|
|
||||||
nuki = new NukiWrapper("NukiHub", deviceId, network, preferences);
|
nuki = new NukiWrapper("NukiHub", deviceId, network, preferences);
|
||||||
webCfgServer = new WebCfgServer(nuki, network, preferences);
|
webCfgServer = new WebCfgServer(nuki, network, ethServer, preferences);
|
||||||
webCfgServer->initialize();
|
webCfgServer->initialize();
|
||||||
nuki->initialize();
|
nuki->initialize();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user