mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-25 20:06:08 +00:00
Improved ethernet connection
This commit is contained in:
45
lib/ConnectionHandler/include/ConnectionHandler.h
Normal file
45
lib/ConnectionHandler/include/ConnectionHandler.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _CONNECTIONHANDLER_H
|
||||
#define _CONNECTIONHANDLER_H
|
||||
|
||||
#include "AmsConfiguration.h"
|
||||
#if defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ESP32)
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#define NETWORK_MODE_WIFI_CLIENT 1
|
||||
#define NETWORK_MODE_WIFI_AP 2
|
||||
#define NETWORK_MODE_ETH_CLIENT 3
|
||||
|
||||
class ConnectionHandler {
|
||||
public:
|
||||
virtual ~ConnectionHandler() {};
|
||||
virtual bool connect(NetworkConfig config, SystemConfig sys);
|
||||
virtual void disconnect(unsigned long reconnectDelay);
|
||||
virtual bool isConnected();
|
||||
virtual bool isConfigChanged();
|
||||
virtual void getCurrentConfig(NetworkConfig& networkConfig);
|
||||
virtual IPAddress getIP();
|
||||
virtual IPAddress getSubnetMask();
|
||||
virtual IPAddress getGateway();
|
||||
virtual IPAddress getDns(uint8_t idx);
|
||||
#if defined(ESP32)
|
||||
virtual void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
#endif
|
||||
|
||||
uint8_t getMode() {
|
||||
return this->mode;
|
||||
}
|
||||
|
||||
protected:
|
||||
uint8_t mode;
|
||||
};
|
||||
|
||||
#endif
|
||||
48
lib/ConnectionHandler/include/EthernetConnectionHandler.h
Normal file
48
lib/ConnectionHandler/include/EthernetConnectionHandler.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ETHERNETCONNECTIONHANDLER_H
|
||||
#define _ETHERNETCONNECTIONHANDLER_H
|
||||
|
||||
#include "ConnectionHandler.h"
|
||||
#include <Arduino.h>
|
||||
#include "RemoteDebug.h"
|
||||
|
||||
#define CONNECTION_TIMEOUT 30000
|
||||
|
||||
class EthernetConnectionHandler : public ConnectionHandler {
|
||||
public:
|
||||
EthernetConnectionHandler(RemoteDebug* debugger);
|
||||
|
||||
bool connect(NetworkConfig config, SystemConfig sys);
|
||||
void disconnect(unsigned long reconnectDelay);
|
||||
bool isConnected();
|
||||
bool isConfigChanged();
|
||||
void getCurrentConfig(NetworkConfig& networkConfig);
|
||||
IPAddress getIP();
|
||||
IPAddress getSubnetMask();
|
||||
IPAddress getGateway();
|
||||
IPAddress getDns(uint8_t idx);
|
||||
|
||||
#if defined(ESP32)
|
||||
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
#endif
|
||||
|
||||
private:
|
||||
RemoteDebug* debugger;
|
||||
NetworkConfig config;
|
||||
|
||||
bool connected = false;
|
||||
bool configChanged = false;
|
||||
unsigned long timeout = CONNECTION_TIMEOUT;
|
||||
unsigned long lastRetry = 0;
|
||||
|
||||
int8_t ethPowerPin = -1;
|
||||
uint8_t ethEnablePin = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _WIFIACCESSPOINTCONNECTIONHANDLER_H
|
||||
#define _WIFIACCESSPOINTCONNECTIONHANDLER_H
|
||||
|
||||
#include "ConnectionHandler.h"
|
||||
#include <Arduino.h>
|
||||
#include "RemoteDebug.h"
|
||||
#include <DNSServer.h>
|
||||
|
||||
class WiFiAccessPointConnectionHandler : public ConnectionHandler {
|
||||
public:
|
||||
WiFiAccessPointConnectionHandler(RemoteDebug* debugger);
|
||||
|
||||
bool connect(NetworkConfig config, SystemConfig sys);
|
||||
void disconnect(unsigned long reconnectDelay);
|
||||
bool isConnected();
|
||||
bool isConfigChanged();
|
||||
void getCurrentConfig(NetworkConfig& networkConfig);
|
||||
IPAddress getIP();
|
||||
IPAddress getSubnetMask();
|
||||
IPAddress getGateway();
|
||||
IPAddress getDns(uint8_t idx);
|
||||
|
||||
#if defined(ESP32)
|
||||
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
#endif
|
||||
|
||||
private:
|
||||
RemoteDebug* debugger;
|
||||
NetworkConfig config;
|
||||
|
||||
DNSServer dnsServer;
|
||||
|
||||
bool connected = false;
|
||||
bool configChanged = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
46
lib/ConnectionHandler/include/WiFiClientConnectionHandler.h
Normal file
46
lib/ConnectionHandler/include/WiFiClientConnectionHandler.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _WIFICLIENTCONNECTIONHANDLER_H
|
||||
#define _WIFICLIENTCONNECTIONHANDLER_H
|
||||
|
||||
#include "ConnectionHandler.h"
|
||||
#include <Arduino.h>
|
||||
#include "RemoteDebug.h"
|
||||
|
||||
#define CONNECTION_TIMEOUT 30000
|
||||
#define RECONNECT_TIMEOUT 5000
|
||||
|
||||
class WiFiClientConnectionHandler : public ConnectionHandler {
|
||||
public:
|
||||
WiFiClientConnectionHandler(RemoteDebug* debugger);
|
||||
|
||||
bool connect(NetworkConfig config, SystemConfig sys);
|
||||
void disconnect(unsigned long reconnectDelay);
|
||||
bool isConnected();
|
||||
bool isConfigChanged();
|
||||
void getCurrentConfig(NetworkConfig& networkConfig);
|
||||
IPAddress getIP();
|
||||
IPAddress getSubnetMask();
|
||||
IPAddress getGateway();
|
||||
IPAddress getDns(uint8_t idx);
|
||||
|
||||
#if defined(ESP32)
|
||||
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
#endif
|
||||
|
||||
private:
|
||||
RemoteDebug* debugger;
|
||||
NetworkConfig config;
|
||||
bool busPowered = false;
|
||||
bool firstConnect = true;
|
||||
bool configChanged = false;
|
||||
|
||||
unsigned long timeout = CONNECTION_TIMEOUT;
|
||||
unsigned long lastRetry = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
162
lib/ConnectionHandler/src/EthernetConnectionHandler.cpp
Normal file
162
lib/ConnectionHandler/src/EthernetConnectionHandler.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#include "EthernetConnectionHandler.h"
|
||||
|
||||
#if defined(ESP32)
|
||||
#include <ETH.h>
|
||||
#endif
|
||||
|
||||
EthernetConnectionHandler::EthernetConnectionHandler(RemoteDebug* debugger) {
|
||||
this->debugger = debugger;
|
||||
this->mode = NETWORK_MODE_ETH_CLIENT;
|
||||
}
|
||||
|
||||
bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys) {
|
||||
if(lastRetry > 0 && (millis() - lastRetry) < timeout) {
|
||||
delay(50);
|
||||
return false;
|
||||
}
|
||||
lastRetry = millis();
|
||||
|
||||
#if defined(ESP32)
|
||||
if (!connected) {
|
||||
eth_phy_type_t ethType = ETH_PHY_LAN8720;
|
||||
eth_clock_mode_t ethClkMode = ETH_CLOCK_GPIO0_IN;
|
||||
uint8_t ethAddr = 0;
|
||||
uint8_t ethMdc = 0;
|
||||
uint8_t ethMdio = 0;
|
||||
|
||||
if(sys.boardType == 241) {
|
||||
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("LilyGO T-ETH-POE\n"));
|
||||
ethType = ETH_PHY_LAN8720;
|
||||
ethEnablePin = -1;
|
||||
ethAddr = 0;
|
||||
ethClkMode = ETH_CLOCK_GPIO17_OUT;
|
||||
ethPowerPin = 5;
|
||||
ethMdc = 23;
|
||||
ethMdio = 18;
|
||||
} else if(sys.boardType == 242) {
|
||||
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("M5 PoESP32\n"));
|
||||
ethType = ETH_PHY_IP101;
|
||||
ethEnablePin = -1;
|
||||
ethAddr = 1;
|
||||
ethClkMode = ETH_CLOCK_GPIO0_IN;
|
||||
ethPowerPin = 5;
|
||||
ethMdc = 23;
|
||||
ethMdio = 18;
|
||||
} else if(sys.boardType == 243) {
|
||||
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("WT32-ETH01\n"));
|
||||
ethType = ETH_PHY_LAN8720;
|
||||
ethEnablePin = -1;
|
||||
ethAddr = 1;
|
||||
ethClkMode = ETH_CLOCK_GPIO17_OUT;
|
||||
ethPowerPin = 16;
|
||||
ethMdc = 23;
|
||||
ethMdio = 18;
|
||||
} else {
|
||||
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Board type %d incompatible with ETH\n"), sys.boardType);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(ethEnablePin > 0) {
|
||||
pinMode(ethEnablePin, OUTPUT);
|
||||
digitalWrite(ethEnablePin, 1);
|
||||
}
|
||||
|
||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to Ethernet\n"));
|
||||
|
||||
if(ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) {
|
||||
#if defined(ESP32)
|
||||
if(strlen(config.hostname) > 0) {
|
||||
ETH.setHostname(config.hostname);
|
||||
}
|
||||
#endif
|
||||
|
||||
if(strlen(config.ip) > 0) {
|
||||
IPAddress ip, gw, sn(255,255,255,0), dns1, dns2;
|
||||
ip.fromString(config.ip);
|
||||
gw.fromString(config.gateway);
|
||||
sn.fromString(config.subnet);
|
||||
if(strlen(config.dns1) > 0) {
|
||||
dns1.fromString(config.dns1);
|
||||
} else if(strlen(config.gateway) > 0) {
|
||||
dns1.fromString(config.gateway); // If no DNS, set gateway by default
|
||||
}
|
||||
if(strlen(config.dns2) > 0) {
|
||||
dns2.fromString(config.dns2);
|
||||
} else if(dns1.toString().isEmpty()) {
|
||||
dns2.fromString(F("208.67.220.220")); // Add OpenDNS as second by default if nothing is configured
|
||||
}
|
||||
if(!ETH.config(ip, gw, sn, dns1, dns2)) {
|
||||
debugger->printf_P(PSTR("Static IP configuration is invalid, not using\n"));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start Ethernet\n"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
void EthernetConnectionHandler::disconnect(unsigned long reconnectDelay) {
|
||||
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Disconnecting!\n"));
|
||||
}
|
||||
|
||||
bool EthernetConnectionHandler::isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
#if defined(ESP32)
|
||||
void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
switch(event) {
|
||||
case ARDUINO_EVENT_ETH_CONNECTED:
|
||||
connected = true;
|
||||
if(debugger->isActive(RemoteDebug::INFO)) {
|
||||
debugger->printf_P(PSTR("Successfully connected to Ethernet!\n"));
|
||||
}
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_GOT_IP:
|
||||
if(debugger->isActive(RemoteDebug::INFO)) {
|
||||
debugger->printf_P(PSTR("IP: %s\n"), ETH.localIP().toString().c_str());
|
||||
debugger->printf_P(PSTR("GW: %s\n"), ETH.gatewayIP().toString().c_str());
|
||||
debugger->printf_P(PSTR("DNS: %s\n"), ETH.dnsIP().toString().c_str());
|
||||
}
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||
connected = false;
|
||||
if(debugger->isActive(RemoteDebug::WARNING)) {
|
||||
debugger->printf_P(PSTR("Ethernet was disconnected!\n"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool EthernetConnectionHandler::isConfigChanged() {
|
||||
return configChanged;
|
||||
}
|
||||
|
||||
void EthernetConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
||||
networkConfig = this->config;
|
||||
}
|
||||
|
||||
IPAddress EthernetConnectionHandler::getIP() {
|
||||
return ETH.localIP();
|
||||
}
|
||||
|
||||
IPAddress EthernetConnectionHandler::getSubnetMask() {
|
||||
return ETH.subnetMask();
|
||||
}
|
||||
|
||||
IPAddress EthernetConnectionHandler::getGateway() {
|
||||
return ETH.gatewayIP();
|
||||
}
|
||||
|
||||
IPAddress EthernetConnectionHandler::getDns(uint8_t idx) {
|
||||
return ETH.dnsIP(idx);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#include "WiFiAccessPointConnectionHandler.h"
|
||||
|
||||
WiFiAccessPointConnectionHandler::WiFiAccessPointConnectionHandler(RemoteDebug* debugger) {
|
||||
this->debugger = debugger;
|
||||
this->mode = NETWORK_MODE_WIFI_AP;
|
||||
}
|
||||
|
||||
bool WiFiAccessPointConnectionHandler::connect(NetworkConfig config, SystemConfig sys) {
|
||||
//wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, 0); // Disable default gw
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(config.ssid, config.psk);
|
||||
|
||||
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer.start(53, PSTR("*"), WiFi.softAPIP());
|
||||
connected = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WiFiAccessPointConnectionHandler::disconnect(unsigned long reconnectDelay) {
|
||||
WiFi.disconnect(true);
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.enableAP(false);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
yield();
|
||||
}
|
||||
|
||||
bool WiFiAccessPointConnectionHandler::isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
#if defined(ESP32)
|
||||
void WiFiAccessPointConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
uint8_t mac[6];
|
||||
IPAddress stationIP;
|
||||
switch(event) {
|
||||
case ARDUINO_EVENT_WIFI_AP_START:
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("WiFi access point started with SSID %s\n"), config.ssid);
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_AP_STOP:
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("WiFi access point stopped!\n"));
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
|
||||
memcpy(mac, info.wifi_ap_staconnected.mac, 6);
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Client connected to AP, client MAC: %02x:%02x:%02x:%02x:%02x:%02x\n"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
|
||||
memcpy(mac, info.wifi_ap_staconnected.mac, 6);
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Client disconnected from AP, client MAC: %02x:%02x:%02x:%02x:%02x:%02x\n"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
|
||||
stationIP = info.wifi_ap_staipassigned.ip.addr;
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Client was assigned IP %s\n"), stationIP.toString().c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool WiFiAccessPointConnectionHandler::isConfigChanged() {
|
||||
return configChanged;
|
||||
}
|
||||
|
||||
void WiFiAccessPointConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
||||
networkConfig = this->config;
|
||||
}
|
||||
|
||||
IPAddress WiFiAccessPointConnectionHandler::getIP() {
|
||||
return WiFi.softAPIP();
|
||||
}
|
||||
|
||||
IPAddress WiFiAccessPointConnectionHandler::getSubnetMask() {
|
||||
return WiFi.softAPSubnetMask();
|
||||
}
|
||||
|
||||
IPAddress WiFiAccessPointConnectionHandler::getGateway() {
|
||||
return WiFi.softAPIP();
|
||||
}
|
||||
|
||||
IPAddress WiFiAccessPointConnectionHandler::getDns(uint8_t idx) {
|
||||
return WiFi.softAPIP();
|
||||
}
|
||||
240
lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp
Normal file
240
lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
/**
|
||||
* @copyright Utilitech AS 2023
|
||||
* License: Fair Source
|
||||
*
|
||||
*/
|
||||
|
||||
#include "WiFiClientConnectionHandler.h"
|
||||
#if defined(ESP32)
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
|
||||
WiFiClientConnectionHandler::WiFiClientConnectionHandler(RemoteDebug* debugger) {
|
||||
this->debugger = debugger;
|
||||
this->mode = NETWORK_MODE_WIFI_CLIENT;
|
||||
}
|
||||
|
||||
bool WiFiClientConnectionHandler::connect(NetworkConfig config, SystemConfig sys) {
|
||||
if(lastRetry > 0 && (millis() - lastRetry) < timeout) {
|
||||
delay(50);
|
||||
return false;
|
||||
}
|
||||
lastRetry = millis();
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
if(config.mode != this->mode || strlen(config.ssid) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(WiFi.getMode() != WIFI_OFF) {
|
||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Not connected to WiFi, closing resources\n"));
|
||||
|
||||
disconnect(RECONNECT_TIMEOUT);
|
||||
return false;
|
||||
}
|
||||
timeout = CONNECTION_TIMEOUT;
|
||||
|
||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to WiFi network: %s\n"), config.ssid);
|
||||
switch(sys.boardType) {
|
||||
case 2: // spenceme
|
||||
case 3: // Pow-K UART0
|
||||
case 4: // Pow-U UART0
|
||||
case 5: // Pow-K+
|
||||
case 6: // Pow-P1
|
||||
case 7: // Pow-U+
|
||||
case 8: // dbeinder: HAN mosquito
|
||||
busPowered = true;
|
||||
break;
|
||||
default:
|
||||
busPowered = false;
|
||||
}
|
||||
firstConnect = sys.dataCollectionConsent == 0;
|
||||
|
||||
#if defined(ESP32)
|
||||
if(strlen(config.hostname) > 0) {
|
||||
WiFi.setHostname(config.hostname);
|
||||
}
|
||||
#endif
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if(strlen(config.ip) > 0) {
|
||||
IPAddress ip, gw, sn(255,255,255,0), dns1, dns2;
|
||||
ip.fromString(config.ip);
|
||||
gw.fromString(config.gateway);
|
||||
sn.fromString(config.subnet);
|
||||
if(strlen(config.dns1) > 0) {
|
||||
dns1.fromString(config.dns1);
|
||||
} else if(strlen(config.gateway) > 0) {
|
||||
dns1.fromString(config.gateway); // If no DNS, set gateway by default
|
||||
}
|
||||
if(strlen(config.dns2) > 0) {
|
||||
dns2.fromString(config.dns2);
|
||||
} else if(dns1.toString().isEmpty()) {
|
||||
dns2.fromString(F("208.67.220.220")); // Add OpenDNS as second by default if nothing is configured
|
||||
}
|
||||
if(!WiFi.config(ip, gw, sn, dns1, dns2)) {
|
||||
debugger->printf_P(PSTR("Static IP configuration is invalid, not using\n"));
|
||||
}
|
||||
}
|
||||
#if defined(ESP8266)
|
||||
if(strlen(config.hostname) > 0) {
|
||||
WiFi.hostname(config.hostname);
|
||||
}
|
||||
//wifi_set_phy_mode(PHY_MODE_11N);
|
||||
if(!config.use11b) {
|
||||
wifi_set_user_sup_rate(RATE_11G6M, RATE_11G54M);
|
||||
wifi_set_user_rate_limit(RC_LIMIT_11G, 0x00, RATE_11G_G54M, RATE_11G_G6M);
|
||||
wifi_set_user_rate_limit(RC_LIMIT_11N, 0x00, RATE_11N_MCS7S, RATE_11N_MCS0);
|
||||
wifi_set_user_limit_rate_mask(LIMIT_RATE_MASK_ALL);
|
||||
}
|
||||
#endif
|
||||
#if defined(ESP32)
|
||||
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
||||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
|
||||
#endif
|
||||
WiFi.setAutoReconnect(true);
|
||||
if(WiFi.begin(config.ssid, config.psk)) {
|
||||
if(config.sleep <= 2) {
|
||||
switch(config.sleep) {
|
||||
case 0:
|
||||
WiFi.setSleep(WIFI_PS_NONE);
|
||||
break;
|
||||
case 1:
|
||||
WiFi.setSleep(WIFI_PS_MIN_MODEM);
|
||||
break;
|
||||
case 2:
|
||||
WiFi.setSleep(WIFI_PS_MAX_MODEM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
yield();
|
||||
} else {
|
||||
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start WiFi\n"));
|
||||
}
|
||||
this->config = config;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void WiFiClientConnectionHandler::disconnect(unsigned long reconnectDelay) {
|
||||
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Disconnecting!\n"));
|
||||
#if defined(ESP8266)
|
||||
WiFiClient::stopAll();
|
||||
#endif
|
||||
|
||||
WiFi.disconnect(true);
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.enableAP(false);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
yield();
|
||||
timeout = reconnectDelay;
|
||||
}
|
||||
|
||||
bool WiFiClientConnectionHandler::isConnected() {
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
#if defined(ESP32)
|
||||
void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
switch(event) {
|
||||
case ARDUINO_EVENT_WIFI_READY:
|
||||
if (!config.use11b) {
|
||||
esp_wifi_config_11b_rate(WIFI_IF_AP, true);
|
||||
esp_wifi_config_11b_rate(WIFI_IF_STA, true);
|
||||
}
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP: {
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully connected to WiFi!\n"));
|
||||
if(debugger->isActive(RemoteDebug::INFO)) {
|
||||
debugger->printf_P(PSTR("IP: %s\n"), WiFi.localIP().toString().c_str());
|
||||
debugger->printf_P(PSTR("GW: %s\n"), WiFi.gatewayIP().toString().c_str());
|
||||
debugger->printf_P(PSTR("DNS: %s\n"), WiFi.dnsIP().toString().c_str());
|
||||
}
|
||||
#if defined(ESP32)
|
||||
if(firstConnect && config.use11b) {
|
||||
// If first boot and phyMode is better than 11b, disable 11b for BUS powered devices
|
||||
if(busPowered) {
|
||||
wifi_phy_mode_t phyMode;
|
||||
if(esp_wifi_sta_get_negotiated_phymode(&phyMode) == ESP_OK) {
|
||||
if(phyMode > WIFI_PHY_MODE_11B) {
|
||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("WiFi supports better rates than 802.11b, disabling\n"));
|
||||
config.use11b = false;
|
||||
configChanged = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(config.power >= 195)
|
||||
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
||||
else if(config.power >= 190)
|
||||
WiFi.setTxPower(WIFI_POWER_19dBm);
|
||||
else if(config.power >= 185)
|
||||
WiFi.setTxPower(WIFI_POWER_18_5dBm);
|
||||
else if(config.power >= 170)
|
||||
WiFi.setTxPower(WIFI_POWER_17dBm);
|
||||
else if(config.power >= 150)
|
||||
WiFi.setTxPower(WIFI_POWER_15dBm);
|
||||
else if(config.power >= 130)
|
||||
WiFi.setTxPower(WIFI_POWER_13dBm);
|
||||
else if(config.power >= 110)
|
||||
WiFi.setTxPower(WIFI_POWER_11dBm);
|
||||
else if(config.power >= 85)
|
||||
WiFi.setTxPower(WIFI_POWER_8_5dBm);
|
||||
else if(config.power >= 70)
|
||||
WiFi.setTxPower(WIFI_POWER_7dBm);
|
||||
else if(config.power >= 50)
|
||||
WiFi.setTxPower(WIFI_POWER_5dBm);
|
||||
else if(config.power >= 20)
|
||||
WiFi.setTxPower(WIFI_POWER_2dBm);
|
||||
else
|
||||
WiFi.setTxPower(WIFI_POWER_MINUS_1dBm);
|
||||
#elif defined(ESP8266)
|
||||
WiFi.setOutputPower(config.power / 10.0);
|
||||
#endif
|
||||
}
|
||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: {
|
||||
wifi_err_reason_t reason = (wifi_err_reason_t) info.wifi_sta_disconnected.reason;
|
||||
const char* descr = WiFi.disconnectReasonName(reason);
|
||||
switch(reason) {
|
||||
case WIFI_REASON_ASSOC_LEAVE:
|
||||
break;
|
||||
default:
|
||||
if(strlen(descr) > 0) {
|
||||
if(debugger->isActive(RemoteDebug::WARNING)) {
|
||||
debugger->printf_P(PSTR("WiFi disconnected, reason %s\n"), descr);
|
||||
}
|
||||
disconnect(RECONNECT_TIMEOUT);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool WiFiClientConnectionHandler::isConfigChanged() {
|
||||
return configChanged;
|
||||
}
|
||||
|
||||
void WiFiClientConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
||||
networkConfig = this->config;
|
||||
}
|
||||
|
||||
IPAddress WiFiClientConnectionHandler::getIP() {
|
||||
return WiFi.localIP();
|
||||
}
|
||||
|
||||
IPAddress WiFiClientConnectionHandler::getSubnetMask() {
|
||||
return WiFi.subnetMask();
|
||||
}
|
||||
|
||||
IPAddress WiFiClientConnectionHandler::getGateway() {
|
||||
return WiFi.gatewayIP();
|
||||
}
|
||||
|
||||
IPAddress WiFiClientConnectionHandler::getDns(uint8_t idx) {
|
||||
return WiFi.dnsIP(idx);
|
||||
}
|
||||
Reference in New Issue
Block a user