This commit is contained in:
70
src/networking/networking.cpp
Normal file
70
src/networking/networking.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <ArduinoLog.h>
|
||||
#include <ETH.h>
|
||||
#include <WiFi.h>
|
||||
#include "../global_data/defines.h"
|
||||
#include "../global_data/global_data.h"
|
||||
#include <Preferences.h>
|
||||
|
||||
int64_t mac_address = ESP.getEfuseMac();
|
||||
uint8_t failed_connection_attempts = 0;
|
||||
|
||||
extern NetworkData wifi_data;
|
||||
extern NetworkData ethernet_data;
|
||||
extern Preferences prefs;
|
||||
|
||||
void wifi_task(void* parameter)
|
||||
{
|
||||
Log.verbose("Starting WiFi Task");
|
||||
while (true) {
|
||||
if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) {
|
||||
wifi_data.link = false;
|
||||
if (failed_connection_attempts > 5) {
|
||||
Log.verbose("Failed to connecto to currently saved SSID, starting SoftAP");
|
||||
} else {
|
||||
Log.verbose("No SSID saved, starting SoftAP");
|
||||
}
|
||||
|
||||
String ap_ssid = "Watermeter-" + String(mac_address);
|
||||
WiFi.softAP(ap_ssid, "");
|
||||
|
||||
Log.verbose("[WIFI_TASK] 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.verbose("RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID"));
|
||||
delay(5000);
|
||||
} else {
|
||||
Log.verbose("Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, ""));
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, ""));
|
||||
failed_connection_attempts++;
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ethernet_task(void* parameter)
|
||||
{
|
||||
Log.verbose("Connecting Ethernet");
|
||||
ETH.begin(0, 17, 23, 18);
|
||||
ETH.setHostname("watermeter");
|
||||
while (true) {
|
||||
ethernet_data.link = ETH.linkUp();
|
||||
ethernet_data.rssi = ETH.linkSpeed();
|
||||
ethernet_data.ip_address = ETH.localIP().toString();
|
||||
delay(60 * 1000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user