#include #include #include "../global_data/defines.h" #include #include "../global_data/global_data.h" #include #define ETH_PHY_TYPE ETH_PHY_LAN8720 #define ETH_PHY_ADDR 0 #define ETH_PHY_MDC 23 #define ETH_PHY_MDIO 18 #define ETH_PHY_POWER 14 #define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN #include int64_t mac_address = ESP.getEfuseMac(); uint8_t failed_connection_attempts = 0; extern NetworkData wifi_data; extern NetworkData ethernet_data; extern Preferences prefs; // Defines the type of connection for which the hostname should be created enum HostnameType { Wireless, Ethernet, Generic }; const char * get_hostname(HostnameType host_type) { String default_hostname = "Waterlevel-" + String(mac_address, HEX); String hostname = prefs.getString("hostname", default_hostname); switch (host_type) { case Wireless: return (hostname + "-wl").c_str(); case Ethernet: return (hostname + "-eth").c_str(); default: return hostname.c_str(); } } void wifi_task(void* parameter) { LOG(ELOG_LEVEL_DEBUG, "Starting WiFi Task"); WiFi.setHostname(get_hostname(Wireless)); while (true) { if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) { wifi_data.link = false; if (failed_connection_attempts > 5) { LOG(ELOG_LEVEL_WARNING, "Failed to connect to currently saved SSID, starting SoftAP"); } else { LOG(ELOG_LEVEL_WARNING, "No SSID saved, starting SoftAP"); } String ap_ssid = get_hostname(Wireless); WiFi.softAP(ap_ssid.c_str(), ""); LOG(ELOG_LEVEL_DEBUG, "Waiting for SSID now..."); String old_ssid = prefs.getString(ssid_key, "xxx"); while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) { delay(5000); } failed_connection_attempts = 0; } else { if (WiFi.isConnected() && WiFi.SSID() == prefs.getString(ssid_key, "")) { failed_connection_attempts = 0; wifi_data.rssi = WiFi.RSSI(); wifi_data.link = true; wifi_data.network_name = WiFi.SSID(); wifi_data.ip_address = WiFi.localIP().toString(); LOG(ELOG_LEVEL_DEBUG, "WIFI connected; RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID")); delay(1000 * 60); } else { String ssid = prefs.getString(ssid_key, ""); String password = prefs.getString(wifi_password_key, ""); LOG(ELOG_LEVEL_DEBUG, "Connecting to %s...", ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); failed_connection_attempts++; LOG(ELOG_LEVEL_WARNING, "Failed to connect, retrying..."); delay(5000); } } } } void ethernet_task(void* parameter) { LOG(ELOG_LEVEL_DEBUG, "Starting Ethernet Task"); ETH.begin(); ETH.setHostname(get_hostname(Ethernet)); while (true) { ethernet_data.link = ETH.linkUp(); ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.ip_address = ETH.localIP().toString(); LOG(ELOG_LEVEL_DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ETH.localIP().toString(), String(ethernet_data.link)); if (ETH.linkUp() && !ETH.isDefault() && ETH.localIP().toString() != "0.0.0.0") { LOG(ELOG_LEVEL_DEBUG, "Ethernet is up, setting to default"); ETH.setDefault(); } delay(60 * 1000); } }