PR343
This commit is contained in:
75
src/networkDevices/ClientSyncW5500.cpp
Normal file
75
src/networkDevices/ClientSyncW5500.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Copyright (c) 2022 Bert Melis. All rights reserved.
|
||||
|
||||
This work is licensed under the terms of the MIT license.
|
||||
For a copy, see <https://opensource.org/licenses/MIT> or
|
||||
the LICENSE file.
|
||||
*/
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
#include "ClientSyncW5500.h"
|
||||
#include <lwip/sockets.h> // socket options
|
||||
|
||||
namespace espMqttClientInternals {
|
||||
|
||||
ClientSyncW5500::ClientSyncW5500()
|
||||
: client() {
|
||||
// empty
|
||||
}
|
||||
|
||||
bool ClientSyncW5500::connect(IPAddress ip, uint16_t port) {
|
||||
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
|
||||
if (ret) {
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
client.setNoDelay(true);
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
||||
int val = true;
|
||||
|
||||
// TODO
|
||||
// client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ClientSyncW5500::connect(const char* host, uint16_t port) {
|
||||
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
|
||||
if (ret) {
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
client.setNoDelay(true);
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
||||
int val = true;
|
||||
|
||||
// TODO
|
||||
// client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t ClientSyncW5500::write(const uint8_t* buf, size_t size) {
|
||||
return client.write(buf, size);
|
||||
}
|
||||
|
||||
int ClientSyncW5500::read(uint8_t* buf, size_t size) {
|
||||
return client.read(buf, size);
|
||||
}
|
||||
|
||||
void ClientSyncW5500::stop() {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
bool ClientSyncW5500::connected() {
|
||||
return client.connected();
|
||||
}
|
||||
|
||||
bool ClientSyncW5500::disconnected() {
|
||||
return !client.connected();
|
||||
}
|
||||
|
||||
} // namespace espMqttClientInternals
|
||||
|
||||
#endif
|
||||
25
src/networkDevices/ClientSyncW5500.h
Normal file
25
src/networkDevices/ClientSyncW5500.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
#include "Transport/Transport.h"
|
||||
#include "EthernetClient.h"
|
||||
|
||||
namespace espMqttClientInternals {
|
||||
|
||||
class ClientSyncW5500 : public Transport {
|
||||
public:
|
||||
ClientSyncW5500();
|
||||
bool connect(IPAddress ip, uint16_t port) override;
|
||||
bool connect(const char* host, uint16_t port) override;
|
||||
size_t write(const uint8_t* buf, size_t size) override;
|
||||
int read(uint8_t* buf, size_t size) override;
|
||||
void stop() override;
|
||||
bool connected() override;
|
||||
bool disconnected() override;
|
||||
EthernetClient client;
|
||||
};
|
||||
|
||||
} // namespace espMqttClientInternals
|
||||
|
||||
#endif
|
||||
150
src/networkDevices/EthLan8720Device.cpp
Normal file
150
src/networkDevices/EthLan8720Device.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
//#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
|
||||
//#define ETH_PHY_POWER 12
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <ETH.h>
|
||||
#include "EthLan8720Device.h"
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
#include "../MqttTopics.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "../RestartReason.h"
|
||||
|
||||
EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode, bool use_mac_from_efuse)
|
||||
: NetworkDevice(hostname, ipConfiguration),
|
||||
_deviceName(deviceName),
|
||||
_phy_addr(phy_addr),
|
||||
_power(power),
|
||||
_mdc(mdc),
|
||||
_mdio(mdio),
|
||||
_type(ethtype),
|
||||
_clock_mode(clock_mode),
|
||||
_use_mac_from_efuse(use_mac_from_efuse)
|
||||
{
|
||||
_restartOnDisconnect = preferences->getBool(preference_restart_on_disconnect);
|
||||
|
||||
size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
|
||||
size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
|
||||
size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
|
||||
|
||||
_useEncryption = caLength > 1; // length is 1 when empty
|
||||
|
||||
if(_useEncryption)
|
||||
{
|
||||
Log->println(F("MQTT over TLS."));
|
||||
Log->println(_ca);
|
||||
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
||||
_mqttClientSecure->setCACert(_ca);
|
||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||
{
|
||||
Log->println(F("MQTT with client certificate."));
|
||||
Log->println(_cert);
|
||||
Log->println(_key);
|
||||
_mqttClientSecure->setCertificate(_cert);
|
||||
_mqttClientSecure->setPrivateKey(_key);
|
||||
}
|
||||
} else
|
||||
{
|
||||
Log->println(F("MQTT without TLS."));
|
||||
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
|
||||
}
|
||||
|
||||
if(preferences->getBool(preference_mqtt_log_enabled))
|
||||
{
|
||||
_path = new char[200];
|
||||
memset(_path, 0, sizeof(_path));
|
||||
|
||||
String pathStr = preferences->getString(preference_mqtt_lock_path);
|
||||
pathStr.concat(mqtt_topic_log);
|
||||
strcpy(_path, pathStr.c_str());
|
||||
Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
|
||||
}
|
||||
}
|
||||
|
||||
const String EthLan8720Device::deviceName() const
|
||||
{
|
||||
return _deviceName.c_str();
|
||||
}
|
||||
|
||||
void EthLan8720Device::initialize()
|
||||
{
|
||||
delay(250);
|
||||
|
||||
WiFi.setHostname(_hostname.c_str());
|
||||
_hardwareInitialized = ETH.begin(_phy_addr, _power, _mdc, _mdio, _type, _clock_mode, _use_mac_from_efuse);
|
||||
ETH.setHostname(_hostname.c_str());
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer());
|
||||
}
|
||||
|
||||
if(_restartOnDisconnect)
|
||||
{
|
||||
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
|
||||
{
|
||||
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED)
|
||||
{
|
||||
onDisconnected();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void EthLan8720Device::reconfigure()
|
||||
{
|
||||
delay(200);
|
||||
restartEsp(RestartReason::ReconfigureLAN8720);
|
||||
}
|
||||
|
||||
bool EthLan8720Device::supportsEncryption()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EthLan8720Device::isConnected()
|
||||
{
|
||||
bool connected = ETH.linkUp();
|
||||
|
||||
if(_lastConnected == false && connected == true)
|
||||
{
|
||||
Serial.print(F("Ethernet connected. IP address: "));
|
||||
Serial.println(ETH.localIP().toString());
|
||||
}
|
||||
|
||||
_lastConnected = connected;
|
||||
|
||||
return connected;
|
||||
}
|
||||
|
||||
ReconnectStatus EthLan8720Device::reconnect()
|
||||
{
|
||||
if(!_hardwareInitialized)
|
||||
{
|
||||
return ReconnectStatus::CriticalFailure;
|
||||
}
|
||||
delay(200);
|
||||
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||
}
|
||||
|
||||
void EthLan8720Device::onDisconnected()
|
||||
{
|
||||
if(millis() > 60000)
|
||||
{
|
||||
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t EthLan8720Device::signalStrength()
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
String EthLan8720Device::localIP()
|
||||
{
|
||||
return ETH.localIP().toString();
|
||||
}
|
||||
|
||||
String EthLan8720Device::BSSIDstr()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
61
src/networkDevices/EthLan8720Device.h
Normal file
61
src/networkDevices/EthLan8720Device.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "espMqttClient.h"
|
||||
#include <ETH.h>
|
||||
|
||||
class EthLan8720Device : public NetworkDevice
|
||||
{
|
||||
|
||||
public:
|
||||
EthLan8720Device(const String& hostname,
|
||||
Preferences* preferences,
|
||||
const IPConfiguration* ipConfiguration,
|
||||
const std::string& deviceName,
|
||||
uint8_t phy_addr = ETH_PHY_ADDR,
|
||||
int power = ETH_PHY_POWER,
|
||||
int mdc = ETH_PHY_MDC,
|
||||
int mdio = ETH_PHY_MDIO,
|
||||
eth_phy_type_t ethtype = ETH_PHY_TYPE,
|
||||
eth_clock_mode_t clock_mode = ETH_CLK_MODE,
|
||||
bool use_mac_from_efuse = false);
|
||||
|
||||
const String deviceName() const override;
|
||||
|
||||
virtual void initialize();
|
||||
virtual void reconfigure();
|
||||
virtual ReconnectStatus reconnect();
|
||||
bool supportsEncryption() override;
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
int8_t signalStrength() override;
|
||||
|
||||
String localIP() override;
|
||||
String BSSIDstr() override;
|
||||
|
||||
private:
|
||||
void onDisconnected();
|
||||
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
char* _path;
|
||||
bool _hardwareInitialized = false;
|
||||
bool _lastConnected = false;
|
||||
|
||||
const std::string _deviceName;
|
||||
uint8_t _phy_addr;
|
||||
int _power;
|
||||
int _mdc;
|
||||
int _mdio;
|
||||
eth_phy_type_t _type;
|
||||
eth_clock_mode_t _clock_mode;
|
||||
bool _use_mac_from_efuse;
|
||||
|
||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
||||
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
||||
char _key[TLS_KEY_MAX_SIZE] = {0};
|
||||
};
|
||||
56
src/networkDevices/IPConfiguration.cpp
Normal file
56
src/networkDevices/IPConfiguration.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "IPConfiguration.h"
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
|
||||
IPConfiguration::IPConfiguration(Preferences *preferences)
|
||||
: _preferences(preferences)
|
||||
{
|
||||
if(_preferences->getString(preference_ip_address).length() <= 0)
|
||||
{
|
||||
Log->println("IP address empty, falling back to DHCP.");
|
||||
_preferences->putBool(preference_ip_dhcp_enabled, true);
|
||||
}
|
||||
|
||||
_ipAddress.fromString(_preferences->getString(preference_ip_address));
|
||||
_subnet.fromString(_preferences->getString(preference_ip_subnet));
|
||||
_gateway.fromString(_preferences->getString(preference_ip_gateway));
|
||||
_dnsServer.fromString(_preferences->getString(preference_ip_dns_server));
|
||||
|
||||
Log->print(F("IP configuration: "));
|
||||
if(dhcpEnabled())
|
||||
{
|
||||
Log->println(F("DHCP"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Log->print(F("IP address: ")); Log->print(ipAddress());
|
||||
Log->print(F(", Subnet: ")); Log->print(subnet());
|
||||
Log->print(F(", Gateway: ")); Log->print(defaultGateway());
|
||||
Log->print(F(", DNS: ")); Log->println(dnsServer());
|
||||
}
|
||||
}
|
||||
|
||||
bool IPConfiguration::dhcpEnabled() const
|
||||
{
|
||||
return _preferences->getBool(preference_ip_dhcp_enabled);
|
||||
}
|
||||
|
||||
const IPAddress IPConfiguration::ipAddress() const
|
||||
{
|
||||
return _ipAddress;
|
||||
}
|
||||
|
||||
const IPAddress IPConfiguration::subnet() const
|
||||
{
|
||||
return _subnet;
|
||||
}
|
||||
|
||||
const IPAddress IPConfiguration::defaultGateway() const
|
||||
{
|
||||
return _gateway;
|
||||
}
|
||||
|
||||
const IPAddress IPConfiguration::dnsServer() const
|
||||
{
|
||||
return _dnsServer;
|
||||
}
|
||||
24
src/networkDevices/IPConfiguration.h
Normal file
24
src/networkDevices/IPConfiguration.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <Preferences.h>
|
||||
|
||||
class IPConfiguration
|
||||
{
|
||||
public:
|
||||
explicit IPConfiguration(Preferences* preferences);
|
||||
|
||||
bool dhcpEnabled() const;
|
||||
const IPAddress ipAddress() const;
|
||||
const IPAddress subnet() const;
|
||||
const IPAddress defaultGateway() const;
|
||||
const IPAddress dnsServer() const;
|
||||
|
||||
private:
|
||||
Preferences* _preferences = nullptr;
|
||||
|
||||
IPAddress _ipAddress;
|
||||
IPAddress _subnet;
|
||||
IPAddress _gateway;
|
||||
IPAddress _dnsServer;
|
||||
};
|
||||
|
||||
161
src/networkDevices/NetworkDevice.cpp
Normal file
161
src/networkDevices/NetworkDevice.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <Arduino.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "../Logger.h"
|
||||
|
||||
void NetworkDevice::printError()
|
||||
{
|
||||
Log->print(F("Free Heap: "));
|
||||
Log->println(ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void NetworkDevice::update()
|
||||
{
|
||||
if (_mqttEnabled)
|
||||
{
|
||||
getMqttClient()->loop();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttSetClientId(const char *clientId)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setClientId(clientId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setClientId(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttSetCleanSession(bool cleanSession)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setCleanSession(cleanSession);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setCleanSession(cleanSession);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
{
|
||||
return getMqttClient()->publish(topic, qos, retain, payload);
|
||||
}
|
||||
|
||||
uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length)
|
||||
{
|
||||
return getMqttClient()->publish(topic, qos, retain, payload, length);
|
||||
}
|
||||
|
||||
bool NetworkDevice::mqttConnected() const
|
||||
{
|
||||
return getMqttClient()->connected();
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttSetServer(const char *host, uint16_t port)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setServer(host, port);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setServer(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
bool NetworkDevice::mqttConnect()
|
||||
{
|
||||
return getMqttClient()->connect();
|
||||
}
|
||||
|
||||
bool NetworkDevice::mqttDisconnect(bool force)
|
||||
{
|
||||
return getMqttClient()->disconnect(force);
|
||||
}
|
||||
|
||||
void NetworkDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setWill(topic, qos, retain, payload);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setWill(topic, qos, retain, payload);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttSetCredentials(const char *username, const char *password)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setCredentials(username, password);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setCredentials(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onMessage(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onMessage(callback);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onConnect(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onConnect(callback);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback)
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onDisconnect(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onDisconnect(callback);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
||||
{
|
||||
return getMqttClient()->subscribe(topic, qos);
|
||||
}
|
||||
|
||||
void NetworkDevice::disableMqtt()
|
||||
{
|
||||
getMqttClient()->disconnect();
|
||||
_mqttEnabled = false;
|
||||
}
|
||||
|
||||
MqttClient *NetworkDevice::getMqttClient() const
|
||||
{
|
||||
if (_useEncryption)
|
||||
{
|
||||
return _mqttClientSecure;
|
||||
}
|
||||
else
|
||||
{
|
||||
return _mqttClient;
|
||||
}
|
||||
}
|
||||
66
src/networkDevices/NetworkDevice.h
Normal file
66
src/networkDevices/NetworkDevice.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "espMqttClient.h"
|
||||
#include "MqttClientSetup.h"
|
||||
#include "IPConfiguration.h"
|
||||
|
||||
enum class ReconnectStatus
|
||||
{
|
||||
Failure = 0,
|
||||
Success = 1,
|
||||
CriticalFailure = 2
|
||||
};
|
||||
|
||||
class NetworkDevice
|
||||
{
|
||||
public:
|
||||
explicit NetworkDevice(const String& hostname, const IPConfiguration* ipConfiguration)
|
||||
: _hostname(hostname),
|
||||
_ipConfiguration(ipConfiguration)
|
||||
{}
|
||||
|
||||
virtual const String deviceName() const = 0;
|
||||
|
||||
virtual void initialize() = 0;
|
||||
virtual ReconnectStatus reconnect() = 0;
|
||||
virtual void reconfigure() = 0;
|
||||
virtual void printError();
|
||||
virtual bool supportsEncryption() = 0;
|
||||
|
||||
virtual void update();
|
||||
|
||||
virtual bool isConnected() = 0;
|
||||
virtual int8_t signalStrength() = 0;
|
||||
|
||||
virtual String localIP() = 0;
|
||||
virtual String BSSIDstr() = 0;
|
||||
|
||||
virtual void mqttSetClientId(const char* clientId);
|
||||
virtual void mqttSetCleanSession(bool cleanSession);
|
||||
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const char* payload);
|
||||
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length);
|
||||
virtual bool mqttConnected() const;
|
||||
virtual void mqttSetServer(const char* host, uint16_t port);
|
||||
virtual bool mqttConnect();
|
||||
virtual bool mqttDisconnect(bool force);
|
||||
virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload);
|
||||
virtual void mqttSetCredentials(const char* username, const char* password);
|
||||
virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback);
|
||||
virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback);
|
||||
virtual void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback);
|
||||
virtual void disableMqtt();
|
||||
|
||||
virtual uint16_t mqttSubscribe(const char* topic, uint8_t qos);
|
||||
|
||||
protected:
|
||||
espMqttClient *_mqttClient = nullptr;
|
||||
espMqttClientSecure *_mqttClientSecure = nullptr;
|
||||
|
||||
bool _useEncryption = false;
|
||||
bool _mqttEnabled = true;
|
||||
|
||||
const String _hostname;
|
||||
const IPConfiguration* _ipConfiguration = nullptr;
|
||||
|
||||
MqttClient *getMqttClient() const;
|
||||
};
|
||||
232
src/networkDevices/W5500Device.cpp
Normal file
232
src/networkDevices/W5500Device.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include "W5500Device.h"
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
#include "../MqttTopics.h"
|
||||
|
||||
W5500Device::W5500Device(const String &hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, int variant)
|
||||
: NetworkDevice(hostname, ipConfiguration),
|
||||
_preferences(preferences),
|
||||
_variant((W5500Variant)variant)
|
||||
{
|
||||
initializeMacAddress(_mac);
|
||||
|
||||
Log->print("MAC Adress: ");
|
||||
for(int i=0; i < 6; i++)
|
||||
{
|
||||
if(_mac[i] < 10)
|
||||
{
|
||||
Log->print(F("0"));
|
||||
}
|
||||
Log->print(_mac[i], 16);
|
||||
if(i < 5)
|
||||
{
|
||||
Log->print(F(":"));
|
||||
}
|
||||
}
|
||||
Log->println();
|
||||
|
||||
_mqttClient = new espMqttClientW5500();
|
||||
}
|
||||
|
||||
W5500Device::~W5500Device()
|
||||
{}
|
||||
|
||||
const String W5500Device::deviceName() const
|
||||
{
|
||||
return "Wiznet W5500";
|
||||
}
|
||||
|
||||
void W5500Device::initialize()
|
||||
{
|
||||
WiFi.mode(WIFI_OFF);
|
||||
|
||||
resetDevice();
|
||||
|
||||
switch(_variant)
|
||||
{
|
||||
case W5500Variant::M5StackAtomPoe:
|
||||
_resetPin = -1;
|
||||
Ethernet.init(19, 22, 23, 33);
|
||||
break;
|
||||
default:
|
||||
_resetPin = -1;
|
||||
Ethernet.init(5);
|
||||
break;
|
||||
}
|
||||
|
||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||
{
|
||||
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
||||
pathStr.concat(mqtt_topic_log);
|
||||
_path = new char[pathStr.length() + 1];
|
||||
memset(_path, 0, sizeof(_path));
|
||||
strcpy(_path, pathStr.c_str());
|
||||
Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
|
||||
}
|
||||
|
||||
reconnect();
|
||||
}
|
||||
|
||||
ReconnectStatus W5500Device::reconnect()
|
||||
{
|
||||
_hasDHCPAddress = false;
|
||||
|
||||
// start the Ethernet connection:
|
||||
Log->println(F("Initialize Ethernet with DHCP:"));
|
||||
|
||||
int dhcpRetryCnt = 0;
|
||||
bool hardwareFound = false;
|
||||
|
||||
while(dhcpRetryCnt < 3)
|
||||
{
|
||||
Log->print(F("DHCP connect try #"));
|
||||
Log->print(dhcpRetryCnt);
|
||||
Log->println();
|
||||
dhcpRetryCnt++;
|
||||
|
||||
if (Ethernet.begin(_mac, 1000, 1000) == 0)
|
||||
{
|
||||
Log->println(F("Failed to configure Ethernet using DHCP"));
|
||||
// Check for Ethernet hardware present
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware)
|
||||
{
|
||||
Log->println(F("Ethernet module not found"));
|
||||
continue;
|
||||
}
|
||||
if (Ethernet.linkStatus() == LinkOFF)
|
||||
{
|
||||
Log->println(F("Ethernet cable is not connected."));
|
||||
}
|
||||
|
||||
hardwareFound = true;
|
||||
|
||||
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(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
hardwareFound = true;
|
||||
_hasDHCPAddress = true;
|
||||
dhcpRetryCnt = 1000;
|
||||
if(_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
Log->print(F(" DHCP assigned IP "));
|
||||
Log->println(Ethernet.localIP());
|
||||
}
|
||||
}
|
||||
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
Ethernet.setLocalIP(_ipConfiguration->ipAddress());
|
||||
Ethernet.setSubnetMask(_ipConfiguration->subnet());
|
||||
Ethernet.setGatewayIP(_ipConfiguration->defaultGateway());
|
||||
Ethernet.setDnsServerIP(_ipConfiguration->dnsServer());
|
||||
}
|
||||
}
|
||||
|
||||
if(!hardwareFound)
|
||||
{
|
||||
return ReconnectStatus::CriticalFailure;
|
||||
}
|
||||
|
||||
return _hasDHCPAddress ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||
}
|
||||
|
||||
|
||||
void W5500Device::reconfigure()
|
||||
{
|
||||
Log->println(F("Reconfigure W5500 not implemented."));
|
||||
}
|
||||
|
||||
void W5500Device::resetDevice()
|
||||
{
|
||||
if(_resetPin == -1) return;
|
||||
|
||||
Log->println(F("Resetting network hardware."));
|
||||
pinMode(_resetPin, OUTPUT);
|
||||
digitalWrite(_resetPin, HIGH);
|
||||
delay(50);
|
||||
digitalWrite(_resetPin, LOW);
|
||||
delay(50);
|
||||
digitalWrite(_resetPin, HIGH);
|
||||
delay(50);
|
||||
}
|
||||
|
||||
bool W5500Device::supportsEncryption()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool W5500Device::isConnected()
|
||||
{
|
||||
bool connected = (Ethernet.linkStatus() == EthernetLinkStatus::LinkON && _maintainResult == 0 && _hasDHCPAddress);
|
||||
|
||||
if(_lastConnected == false && connected == true)
|
||||
{
|
||||
Serial.print(F("Ethernet connected. IP address: "));
|
||||
Serial.println(Ethernet.localIP().toString());
|
||||
}
|
||||
|
||||
_lastConnected = connected;
|
||||
|
||||
return connected;
|
||||
}
|
||||
|
||||
void W5500Device::initializeMacAddress(byte *mac)
|
||||
{
|
||||
memset(mac, 0, 6);
|
||||
|
||||
mac[0] = 0x00; // wiznet prefix
|
||||
mac[1] = 0x08; // wiznet prefix
|
||||
mac[2] = 0xDC; // wiznet prefix
|
||||
|
||||
if(_preferences->getBool(preference_has_mac_saved))
|
||||
{
|
||||
mac[3] = _preferences->getChar(preference_has_mac_byte_0);
|
||||
mac[4] = _preferences->getChar(preference_has_mac_byte_1);
|
||||
mac[5] = _preferences->getChar(preference_has_mac_byte_2);
|
||||
}
|
||||
else
|
||||
{
|
||||
mac[3] = random(0,255);
|
||||
mac[4] = random(0,255);
|
||||
mac[5] = random(0,255);
|
||||
|
||||
_preferences->putChar(preference_has_mac_byte_0, mac[3]);
|
||||
_preferences->putChar(preference_has_mac_byte_1, mac[4]);
|
||||
_preferences->putChar(preference_has_mac_byte_2, mac[5]);
|
||||
_preferences->putBool(preference_has_mac_saved, true);
|
||||
}
|
||||
}
|
||||
|
||||
void W5500Device::update()
|
||||
{
|
||||
_maintainResult = Ethernet.maintain();
|
||||
NetworkDevice::update();
|
||||
}
|
||||
|
||||
int8_t W5500Device::signalStrength()
|
||||
{
|
||||
return 127;
|
||||
}
|
||||
|
||||
String W5500Device::localIP()
|
||||
{
|
||||
return Ethernet.localIP().toString();
|
||||
}
|
||||
|
||||
String W5500Device::BSSIDstr()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
52
src/networkDevices/W5500Device.h
Normal file
52
src/networkDevices/W5500Device.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "NetworkDevice.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "espMqttClientW5500.h"
|
||||
#include <Ethernet.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
enum class W5500Variant
|
||||
{
|
||||
Generic = 2,
|
||||
M5StackAtomPoe = 3
|
||||
};
|
||||
|
||||
class W5500Device : public NetworkDevice
|
||||
{
|
||||
public:
|
||||
explicit W5500Device(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration, int variant);
|
||||
~W5500Device();
|
||||
|
||||
const String deviceName() const override;
|
||||
|
||||
virtual void initialize();
|
||||
virtual ReconnectStatus reconnect();
|
||||
virtual void reconfigure();
|
||||
|
||||
bool supportsEncryption() override;
|
||||
|
||||
virtual void update() override;
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
int8_t signalStrength() override;
|
||||
|
||||
String localIP() override;
|
||||
String BSSIDstr() override;
|
||||
|
||||
private:
|
||||
void resetDevice();
|
||||
void initializeMacAddress(byte* mac);
|
||||
|
||||
Preferences* _preferences = nullptr;
|
||||
|
||||
int _maintainResult = 0;
|
||||
int _resetPin = -1;
|
||||
bool _hasDHCPAddress = false;
|
||||
char* _path;
|
||||
W5500Variant _variant;
|
||||
bool _lastConnected = false;
|
||||
|
||||
byte _mac[6];
|
||||
};
|
||||
171
src/networkDevices/WifiDevice.cpp
Normal file
171
src/networkDevices/WifiDevice.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
#include <WiFi.h>
|
||||
#include "WifiDevice.h"
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
#include "../MqttTopics.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "../RestartReason.h"
|
||||
|
||||
RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17];
|
||||
|
||||
WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration)
|
||||
: NetworkDevice(hostname, ipConfiguration),
|
||||
_preferences(preferences),
|
||||
_wm(preferences->getString(preference_cred_user).c_str(), preferences->getString(preference_cred_password).c_str())
|
||||
{
|
||||
_startAp = strcmp(WiFiDevice_reconfdetect, "reconfigure_wifi") == 0;
|
||||
|
||||
_restartOnDisconnect = preferences->getBool(preference_restart_on_disconnect);
|
||||
|
||||
size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
|
||||
size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
|
||||
size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
|
||||
|
||||
_useEncryption = caLength > 1; // length is 1 when empty
|
||||
|
||||
if(_useEncryption)
|
||||
{
|
||||
Log->println(F("MQTT over TLS."));
|
||||
Log->println(_ca);
|
||||
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
||||
_mqttClientSecure->setCACert(_ca);
|
||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||
{
|
||||
Log->println(F("MQTT with client certificate."));
|
||||
Log->println(_cert);
|
||||
Log->println(_key);
|
||||
_mqttClientSecure->setCertificate(_cert);
|
||||
_mqttClientSecure->setPrivateKey(_key);
|
||||
}
|
||||
} else
|
||||
{
|
||||
Log->println(F("MQTT without TLS."));
|
||||
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
|
||||
}
|
||||
|
||||
if(preferences->getBool(preference_mqtt_log_enabled))
|
||||
{
|
||||
_path = new char[200];
|
||||
memset(_path, 0, sizeof(_path));
|
||||
|
||||
String pathStr = preferences->getString(preference_mqtt_lock_path);
|
||||
pathStr.concat(mqtt_topic_log);
|
||||
strcpy(_path, pathStr.c_str());
|
||||
Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
|
||||
}
|
||||
}
|
||||
|
||||
const String WifiDevice::deviceName() const
|
||||
{
|
||||
return "Built-in Wi-Fi";
|
||||
}
|
||||
|
||||
void WifiDevice::initialize()
|
||||
{
|
||||
std::vector<const char *> wm_menu;
|
||||
wm_menu.push_back("wifi");
|
||||
wm_menu.push_back("exit");
|
||||
_wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled));
|
||||
// reduced tieout if ESP is set to restart on disconnect
|
||||
_wm.setFindBestRSSI(_preferences->getBool(preference_find_best_rssi));
|
||||
_wm.setConfigPortalTimeout(_restartOnDisconnect ? 60 * 3 : 60 * 30);
|
||||
_wm.setShowInfoUpdate(false);
|
||||
_wm.setMenu(wm_menu);
|
||||
_wm.setHostname(_hostname);
|
||||
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
_wm.setSTAStaticIPConfig(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer());
|
||||
}
|
||||
|
||||
_wm.setAPCallback(clearRtcInitVar);
|
||||
|
||||
bool res = false;
|
||||
|
||||
if(_startAp)
|
||||
{
|
||||
Log->println(F("Opening Wi-Fi configuration portal."));
|
||||
res = _wm.startConfigPortal();
|
||||
}
|
||||
else
|
||||
{
|
||||
res = _wm.autoConnect(); // password protected ap
|
||||
}
|
||||
|
||||
if(!res)
|
||||
{
|
||||
esp_wifi_disconnect ();
|
||||
esp_wifi_stop ();
|
||||
esp_wifi_deinit ();
|
||||
|
||||
Log->println(F("Failed to connect. Wait for ESP restart."));
|
||||
delay(1000);
|
||||
restartEsp(RestartReason::WifiInitFailed);
|
||||
}
|
||||
else {
|
||||
Log->print(F("Wi-Fi connected: "));
|
||||
Log->println(WiFi.localIP().toString());
|
||||
}
|
||||
|
||||
if(_restartOnDisconnect)
|
||||
{
|
||||
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
|
||||
{
|
||||
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED)
|
||||
{
|
||||
onDisconnected();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void WifiDevice::reconfigure()
|
||||
{
|
||||
strcpy(WiFiDevice_reconfdetect, "reconfigure_wifi");
|
||||
delay(200);
|
||||
restartEsp(RestartReason::ReconfigureWifi);
|
||||
}
|
||||
|
||||
bool WifiDevice::supportsEncryption()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiDevice::isConnected()
|
||||
{
|
||||
return WiFi.isConnected();
|
||||
}
|
||||
|
||||
ReconnectStatus WifiDevice::reconnect()
|
||||
{
|
||||
delay(3000);
|
||||
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||
}
|
||||
|
||||
void WifiDevice::onDisconnected()
|
||||
{
|
||||
if(millis() > 60000)
|
||||
{
|
||||
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t WifiDevice::signalStrength()
|
||||
{
|
||||
return WiFi.RSSI();
|
||||
}
|
||||
|
||||
String WifiDevice::localIP()
|
||||
{
|
||||
return WiFi.localIP().toString();
|
||||
}
|
||||
|
||||
String WifiDevice::BSSIDstr()
|
||||
{
|
||||
return WiFi.BSSIDstr();
|
||||
}
|
||||
|
||||
void WifiDevice::clearRtcInitVar(WiFiManager *)
|
||||
{
|
||||
memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect);
|
||||
}
|
||||
45
src/networkDevices/WifiDevice.h
Normal file
45
src/networkDevices/WifiDevice.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "WiFiManager.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "IPConfiguration.h"
|
||||
|
||||
class WifiDevice : public NetworkDevice
|
||||
{
|
||||
public:
|
||||
WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration);
|
||||
|
||||
const String deviceName() const override;
|
||||
|
||||
virtual void initialize();
|
||||
virtual void reconfigure();
|
||||
virtual ReconnectStatus reconnect();
|
||||
bool supportsEncryption() override;
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
int8_t signalStrength() override;
|
||||
|
||||
String localIP() override;
|
||||
String BSSIDstr() override;
|
||||
|
||||
private:
|
||||
static void clearRtcInitVar(WiFiManager*);
|
||||
|
||||
void onDisconnected();
|
||||
|
||||
WiFiManager _wm;
|
||||
Preferences* _preferences = nullptr;
|
||||
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
char* _path;
|
||||
|
||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
||||
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
||||
char _key[TLS_KEY_MAX_SIZE] = {0};
|
||||
};
|
||||
13
src/networkDevices/espMqttClientW5500.cpp
Normal file
13
src/networkDevices/espMqttClientW5500.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "espMqttClientW5500.h"
|
||||
|
||||
espMqttClientW5500::espMqttClientW5500()
|
||||
: espMqttClient(espMqttClientTypes::UseInternalTask::NO),
|
||||
_client()
|
||||
{
|
||||
_transport = &_client;
|
||||
}
|
||||
|
||||
void espMqttClientW5500::update()
|
||||
{
|
||||
loop();
|
||||
}
|
||||
22
src/networkDevices/espMqttClientW5500.h
Normal file
22
src/networkDevices/espMqttClientW5500.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "espMqttClient.h"
|
||||
#include "ClientSyncW5500.h"
|
||||
|
||||
class espMqttClientW5500 : public espMqttClient {
|
||||
public:
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
explicit espMqttClientW5500();
|
||||
#else
|
||||
espMqttClient();
|
||||
#endif
|
||||
|
||||
void update();
|
||||
|
||||
protected:
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
espMqttClientInternals::ClientSyncW5500 _client;
|
||||
#elif defined(__linux__)
|
||||
espMqttClientInternals::ClientPosix _client;
|
||||
#endif
|
||||
};
|
||||
Reference in New Issue
Block a user