initial w5500 support

This commit is contained in:
technyon
2022-04-26 22:12:52 +02:00
parent 2a1f5facf9
commit 677f7e56c8
11 changed files with 314 additions and 71 deletions

View File

@@ -0,0 +1,21 @@
#pragma once
#include "PubSubClient.h"
class NetworkDevice
{
public:
explicit NetworkDevice(const String& hostname)
: _hostname(hostname)
{}
virtual PubSubClient* mqttClient() = 0;
virtual void initialize() = 0;
virtual void reconfigure() = 0;
virtual bool isConnected() = 0;
protected:
const String _hostname;
};

View File

@@ -0,0 +1,95 @@
#include "W5500Device.h"
#include "../Pins.h"
W5500Device::W5500Device(const String &hostname)
: NetworkDevice(hostname)
{
}
W5500Device::~W5500Device()
{}
void W5500Device::initialize()
{
resetDevice();
Ethernet.init(ETHERNET_CS_PIN);
_ethClient = new EthernetClient();
_mqttClient = new PubSubClient(*_ethClient);
// start the Ethernet connection:
Serial.println(F("Initialize Ethernet with DHCP:"));
int dhcpRetryCnt = 0;
while(dhcpRetryCnt < 3)
{
Serial.print(F("DHCP connect try #"));
Serial.print(dhcpRetryCnt);
Serial.println();
dhcpRetryCnt++;
byte mac[] = {0xB0,0xCD,0xAE,0x0F,0xDE,0x10};
if (Ethernet.begin(mac, 1000, 1000) == 0)
{
Serial.println(F("Failed to configure Ethernet using DHCP"));
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println(F("Ethernet module not found"));
delay(10000);
ESP.restart();
}
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println(F("Ethernet cable is not connected."));
}
IPAddress ip;
ip.fromString("192.168.4.1");
IPAddress subnet;
subnet.fromString("255.255.255.0");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
Ethernet.setSubnetMask(subnet);
delay(2000);
}
else
{
dhcpRetryCnt = 1000;
Serial.print(F(" DHCP assigned IP "));
Serial.println(Ethernet.localIP());
}
}
}
void W5500Device::reconfigure()
{
Serial.println(F("Reconfigure W5500 not implemented."));
}
void W5500Device::resetDevice()
{
Serial.println(F("Resetting network hardware."));
pinMode(ETHERNET_RESET_PIN, OUTPUT);
digitalWrite(ETHERNET_RESET_PIN, HIGH);
delay(250);
digitalWrite(ETHERNET_RESET_PIN, LOW);
delay(50);
digitalWrite(ETHERNET_RESET_PIN, HIGH);
delay(1500);
}
PubSubClient *W5500Device::mqttClient()
{
return _mqttClient;
}
bool W5500Device::isConnected()
{
return _ethClient->connected();
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "NetworkDevice.h"
#include <Ethernet.h>
class W5500Device : public NetworkDevice
{
public:
explicit W5500Device(const String& hostname);
~W5500Device();
virtual void initialize();
virtual void reconfigure();
virtual bool isConnected();
virtual PubSubClient *mqttClient();
private:
void resetDevice();
EthernetClient* _ethClient = nullptr;
PubSubClient* _mqttClient = nullptr;
};

View File

@@ -0,0 +1,62 @@
#include <WiFi.h>
#include "WifiDevice.h"
#include "WiFiManager.h"
WifiDevice::WifiDevice(const String& hostname)
: NetworkDevice(hostname),
_mqttClient(_wifiClient)
{}
PubSubClient *WifiDevice::mqttClient()
{
return &_mqttClient;
}
void WifiDevice::initialize()
{
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
WiFiManager wm;
std::vector<const char *> wm_menu;
wm_menu.push_back("wifi");
wm_menu.push_back("exit");
wm.setShowInfoUpdate(false);
wm.setMenu(wm_menu);
wm.setHostname(_hostname);
bool res = false;
if(_cookie.isSet())
{
Serial.println(F("Opening WiFi configuration portal."));
_cookie.clear();
res = wm.startConfigPortal();
}
else
{
res = wm.autoConnect(); // password protected ap
}
if(!res) {
Serial.println(F("Failed to connect. Wait for ESP restart."));
delay(10000);
ESP.restart();
}
else {
Serial.print(F("WiFi connected: "));
Serial.println(WiFi.localIP().toString());
}
}
void WifiDevice::reconfigure()
{
_cookie.set();
delay(200);
ESP.restart();
}
bool WifiDevice::isConnected()
{
return WiFi.isConnected();
}

View File

@@ -0,0 +1,23 @@
#pragma once
#include <WiFiClient.h>
#include "NetworkDevice.h"
#include "../SpiffsCookie.h"
class WifiDevice : public NetworkDevice
{
public:
WifiDevice(const String& hostname);
virtual void initialize();
virtual void reconfigure();
virtual bool isConnected();
virtual PubSubClient *mqttClient();
private:
WiFiClient _wifiClient;
PubSubClient _mqttClient;
SpiffsCookie _cookie;
};