fix platform.ini intellisense generation
rework wifiInterface and wifiHandler for esp32 target
This commit is contained in:
parent
ae7bc0da54
commit
45160ceac5
4 changed files with 94 additions and 159 deletions
|
@ -5,8 +5,9 @@
|
||||||
|
|
||||||
class wifiHandlerInterface {
|
class wifiHandlerInterface {
|
||||||
public:
|
public:
|
||||||
|
wifiHandlerInterface() = default;
|
||||||
struct WifiInfo {
|
struct WifiInfo {
|
||||||
WifiInfo(){};
|
WifiInfo() = default;
|
||||||
WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {}
|
WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {}
|
||||||
|
|
||||||
std::string ssid = "";
|
std::string ssid = "";
|
||||||
|
@ -14,10 +15,11 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wifiStatus {
|
struct wifiStatus {
|
||||||
|
wifiStatus() = default;
|
||||||
wifiStatus(bool aConnected, std::string aIp, std::string aSsid)
|
wifiStatus(bool aConnected, std::string aIp, std::string aSsid)
|
||||||
: isConnected(aConnected), IP(aIp), ssid(aSsid){};
|
: isConnected(aConnected), IP(aIp), ssid(aSsid){};
|
||||||
|
|
||||||
bool isConnected;
|
bool isConnected = false;
|
||||||
std::string IP = "";
|
std::string IP = "";
|
||||||
std::string ssid = "";
|
std::string ssid = "";
|
||||||
};
|
};
|
||||||
|
@ -25,15 +27,21 @@ public:
|
||||||
typedef std::vector<WifiInfo> ScanDoneDataTy;
|
typedef std::vector<WifiInfo> ScanDoneDataTy;
|
||||||
typedef Notification<ScanDoneDataTy> ScanNotificationTy;
|
typedef Notification<ScanDoneDataTy> ScanNotificationTy;
|
||||||
|
|
||||||
|
/// @brief Initialize the wifi handler
|
||||||
virtual void begin() = 0;
|
virtual void begin() = 0;
|
||||||
|
/// @brief Trigger a scan scan for wifi networks
|
||||||
virtual void scan() = 0;
|
virtual void scan() = 0;
|
||||||
|
/// @brief Attempt a connection to the wifi using the provided credentials
|
||||||
virtual void connect(std::string ssid, std::string password) = 0;
|
virtual void connect(std::string ssid, std::string password) = 0;
|
||||||
virtual bool isAvailable() = 0;
|
/// @brief Get the status of the current wifi connection
|
||||||
|
virtual wifiStatus GetStatus() = 0;
|
||||||
|
|
||||||
|
// Register for Scan Notification to handle when scans are completed
|
||||||
std::shared_ptr<ScanNotificationTy> ScanCompleteNotification() {
|
std::shared_ptr<ScanNotificationTy> ScanCompleteNotification() {
|
||||||
return mScanNotification;
|
return mScanNotification;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Register for Status notifications to handle changes in status
|
||||||
std::shared_ptr<Notification<wifiStatus>> WifiStatusNotification() {
|
std::shared_ptr<Notification<wifiStatus>> WifiStatusNotification() {
|
||||||
return mStatusUpdate;
|
return mStatusUpdate;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,63 +1,10 @@
|
||||||
#include "wifihandler.hpp"
|
#include "wifihandler.hpp"
|
||||||
#include "HardwareAbstract.hpp"
|
#include "HardwareAbstract.hpp"
|
||||||
#include "WiFi.h"
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
std::shared_ptr<wifiHandler> wifiHandler::mInstance = nullptr;
|
std::shared_ptr<wifiHandler> wifiHandler::mInstance = nullptr;
|
||||||
|
|
||||||
// WiFi status event
|
|
||||||
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
|
||||||
int no_networks = 0;
|
|
||||||
switch (event) {
|
|
||||||
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
|
|
||||||
Serial.println("WIFI scan done\n");
|
|
||||||
no_networks = WiFi.scanComplete();
|
|
||||||
Serial.println("making notify vector");
|
|
||||||
auto info = std::vector<WifiInfo>(no_networks);
|
|
||||||
Serial.println("loaing notify vector");
|
|
||||||
for (int i = 0; i < no_networks; i++) {
|
|
||||||
auto ssid =
|
|
||||||
WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID";
|
|
||||||
info[i] = WifiInfo(ssid, WiFi.RSSI(i));
|
|
||||||
}
|
|
||||||
if (no_networks < 0) {
|
|
||||||
Serial.println("Scan failed");
|
|
||||||
} else {
|
|
||||||
// TODO Convert To callbacks
|
|
||||||
// this->display.clear_wifi_networks();
|
|
||||||
Serial.print(no_networks);
|
|
||||||
Serial.print(" found\n");
|
|
||||||
// this->display.wifi_scan_complete( no_networks);
|
|
||||||
}
|
|
||||||
Serial.println("notifying");
|
|
||||||
mScanNotification->notify(info);
|
|
||||||
Serial.println("notified");
|
|
||||||
if (WiFi.isConnected() == false) {
|
|
||||||
WiFi.reconnect();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
|
||||||
this->update_credentials();
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_STOP:
|
|
||||||
this->update_status();
|
|
||||||
default:
|
|
||||||
Serial.print("Wifi Status: ");
|
|
||||||
Serial.println(WiFi.status());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
|
||||||
Serial.println("connection failed.");
|
|
||||||
WiFi.disconnect();
|
|
||||||
}
|
|
||||||
Serial.println(WiFi.status());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wifiHandler::isAvailable() { return true; }
|
|
||||||
std::shared_ptr<wifiHandler> wifiHandler::getInstance() {
|
std::shared_ptr<wifiHandler> wifiHandler::getInstance() {
|
||||||
if (mInstance) {
|
if (mInstance) {
|
||||||
return mInstance;
|
return mInstance;
|
||||||
|
@ -66,42 +13,67 @@ std::shared_ptr<wifiHandler> wifiHandler::getInstance() {
|
||||||
return mInstance;
|
return mInstance;
|
||||||
};
|
};
|
||||||
|
|
||||||
wifiHandler::wifiHandler() {
|
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
||||||
this->password = "";
|
int no_networks = 0;
|
||||||
this->SSID = "";
|
switch (event) {
|
||||||
|
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
|
||||||
|
no_networks = WiFi.scanComplete();
|
||||||
|
auto info = std::vector<WifiInfo>(no_networks);
|
||||||
|
for (int i = 0; i < no_networks; i++) {
|
||||||
|
auto ssid =
|
||||||
|
WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID";
|
||||||
|
info[i] = WifiInfo(ssid, WiFi.RSSI(i));
|
||||||
|
}
|
||||||
|
mScanNotification->notify(info);
|
||||||
|
if (WiFi.isConnected() == false) {
|
||||||
|
WiFi.reconnect();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
||||||
|
this->StoreCredentials();
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_STOP:
|
||||||
|
UpdateStatus();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (WiFi.status() == WL_CONNECT_FAILED) {
|
||||||
|
WiFi.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::update_status() {
|
void wifiHandler::UpdateStatus() {
|
||||||
Serial.println("update_status");
|
Serial.println("UpdateStatus");
|
||||||
auto isConnected = WiFi.isConnected();
|
mCurrentStatus.isConnected = WiFi.isConnected();
|
||||||
std::string ip(WiFi.localIP().toString().c_str());
|
mCurrentStatus.IP = WiFi.localIP().toString().c_str();
|
||||||
std::string ssid(WiFi.SSID().c_str());
|
mCurrentStatus.ssid = WiFi.SSID().c_str();
|
||||||
|
|
||||||
wifiStatus status = wifiStatus(isConnected, ip, ssid);
|
mStatusUpdate->notify(mCurrentStatus);
|
||||||
|
|
||||||
// Serial.println(WiFi.localIP());
|
|
||||||
mStatusUpdate->notify(status);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::update_credentials() {
|
void wifiHandler::StoreCredentials() {
|
||||||
// No connection was attempted so don't try to to save the creds
|
// No connection was attempted so don't try to to save the creds
|
||||||
if (!connect_attempt) {
|
if (!mIsConnectionAttempt) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
mPassword = mConnectionAttemptPassword;
|
||||||
|
mSSID = mConnectionAttemptSSID;
|
||||||
|
|
||||||
if (temporary_password == password || temporary_ssid == SSID) {
|
Preferences preferences;
|
||||||
password = temporary_password;
|
preferences.begin("wifiSettings", false);
|
||||||
SSID = temporary_ssid;
|
preferences.putString("password", mPassword.c_str());
|
||||||
|
preferences.putString("SSID", mSSID.c_str());
|
||||||
|
preferences.end();
|
||||||
|
|
||||||
Preferences preferences;
|
mConnectionAttemptPassword.clear();
|
||||||
preferences.begin("wifiSettings", false);
|
mConnectionAttemptSSID.clear();
|
||||||
String tempString = temporary_password.c_str();
|
mIsConnectionAttempt = false;
|
||||||
preferences.putString("password", tempString);
|
|
||||||
tempString = temporary_ssid.c_str();
|
|
||||||
preferences.putString("SSID", tempString);
|
|
||||||
preferences.end();
|
|
||||||
}
|
|
||||||
this->connect_attempt = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::scan() {
|
void wifiHandler::scan() {
|
||||||
|
@ -111,10 +83,8 @@ void wifiHandler::scan() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::begin() {
|
void wifiHandler::begin() {
|
||||||
// this->display = display;
|
|
||||||
WiFi.setHostname("OMOTE");
|
WiFi.setHostname("OMOTE");
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
// WiFi.onEvent([this] (WiFiEvent_t event) {mInstance->WiFiEvent(event);});
|
|
||||||
WiFi.onEvent([](WiFiEvent_t event) { mInstance->WiFiEvent(event); });
|
WiFi.onEvent([](WiFiEvent_t event) { mInstance->WiFiEvent(event); });
|
||||||
|
|
||||||
Preferences preferences;
|
Preferences preferences;
|
||||||
|
@ -123,19 +93,11 @@ void wifiHandler::begin() {
|
||||||
String password = preferences.getString("password");
|
String password = preferences.getString("password");
|
||||||
preferences.end();
|
preferences.end();
|
||||||
|
|
||||||
/* If the SSID is not empty, there was a value stored in the preferences and
|
// Attempt Connection with stored Credentials
|
||||||
* we try to use it.*/
|
|
||||||
if (!ssid.isEmpty()) {
|
if (!ssid.isEmpty()) {
|
||||||
Serial.print("Connecting to wifi ");
|
connect(mSSID, mPassword);
|
||||||
Serial.println(ssid);
|
|
||||||
this->SSID = ssid.c_str();
|
|
||||||
this->password = password.c_str();
|
|
||||||
this->connect(SSID, this->password);
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println("no SSID or password stored");
|
Serial.println("no SSID or password stored");
|
||||||
/*Set first character to \0 indicates an empty string*/
|
|
||||||
this->SSID[0] = '\0';
|
|
||||||
this->password[0] = '\0';
|
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,21 +105,9 @@ void wifiHandler::begin() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::connect(std::string ssid, std::string password) {
|
void wifiHandler::connect(std::string ssid, std::string password) {
|
||||||
this->connect_attempt = true;
|
Serial.printf("Attempting Wifi Connection To %s \n", mSSID.c_str());
|
||||||
// this->temporary_password = password;
|
mIsConnectionAttempt = true;
|
||||||
// this->temporary_ssid = ssid;
|
mConnectionAttemptPassword = password;
|
||||||
|
mConnectionAttemptSSID = ssid;
|
||||||
WiFi.begin(ssid.c_str(), password.c_str());
|
WiFi.begin(ssid.c_str(), password.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::turnOff() {
|
|
||||||
WiFi.disconnect();
|
|
||||||
WiFi.mode(WIFI_OFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wifiHandler::disconnect() { WiFi.disconnect(); }
|
|
||||||
|
|
||||||
bool wifiHandler::isConnected() { return WiFi.isConnected(); }
|
|
||||||
|
|
||||||
std::string wifiHandler::getIP() {
|
|
||||||
return std::string(WiFi.localIP().toString().c_str());
|
|
||||||
}
|
|
|
@ -6,71 +6,48 @@
|
||||||
|
|
||||||
class wifiHandler : public wifiHandlerInterface {
|
class wifiHandler : public wifiHandlerInterface {
|
||||||
public:
|
public:
|
||||||
wifiHandler();
|
|
||||||
static std::shared_ptr<wifiHandler> getInstance();
|
static std::shared_ptr<wifiHandler> getInstance();
|
||||||
/**
|
|
||||||
* @brief Function to initialize the wifi handler
|
|
||||||
*/
|
|
||||||
void begin();
|
|
||||||
|
|
||||||
/**
|
// wifiHandlerInterface Implementation
|
||||||
* @brief Connect to the wifi using the provided credetials
|
void begin() override;
|
||||||
*/
|
void scan() override;
|
||||||
void connect(std::string ssid, std::string password) override;
|
void connect(std::string ssid, std::string password) override;
|
||||||
|
wifiStatus GetStatus() override { return mCurrentStatus; };
|
||||||
|
//
|
||||||
|
|
||||||
|
protected:
|
||||||
|
wifiHandler() = default;
|
||||||
|
static std::shared_ptr<wifiHandler> mInstance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief function to trigger asynchronous scan for wifi networks
|
* @brief Function to store the credentials when we have had a
|
||||||
|
* successful connection
|
||||||
*/
|
*/
|
||||||
void scan();
|
void StoreCredentials();
|
||||||
bool isAvailable();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief Function to update the wifi credentials. This function is called in
|
* @brief Handler for incoming arduino wifi events
|
||||||
* the wifi event callback function after a connection is established. Only
|
* @param event - a Wifi event
|
||||||
* then is the new credentials stored and the old stored credentials
|
|
||||||
* overwritten.
|
|
||||||
*
|
|
||||||
* @param temporary_ssid
|
|
||||||
* @param temporary_password
|
|
||||||
*/
|
*/
|
||||||
void update_credentials();
|
|
||||||
|
|
||||||
void WiFiEvent(WiFiEvent_t event);
|
void WiFiEvent(WiFiEvent_t event);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Function to turn off wifi
|
* @brief Update Internal status and send out a notification
|
||||||
*/
|
*/
|
||||||
void turnOff();
|
void UpdateStatus();
|
||||||
/**
|
wifiStatus mCurrentStatus;
|
||||||
* @brief Function to get the IP address of this device
|
|
||||||
* @return String IP Address of the device
|
|
||||||
*/
|
|
||||||
std::string getIP();
|
|
||||||
static std::shared_ptr<wifiHandler> mInstance;
|
|
||||||
bool connect_attempt = false;
|
|
||||||
std::string temporary_password;
|
|
||||||
std::string temporary_ssid;
|
|
||||||
|
|
||||||
std::string password;
|
|
||||||
std::string SSID;
|
|
||||||
|
|
||||||
void update_status();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Function to disconnect from the network
|
* @brief Variables used to track wifi connection attempts
|
||||||
*/
|
*/
|
||||||
void disconnect();
|
bool mIsConnectionAttempt = false;
|
||||||
|
std::string mConnectionAttemptPassword;
|
||||||
|
std::string mConnectionAttemptSSID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Function to determine wether or not we are connected to a network
|
* @brief Verified Working User and Pass to Wifi network
|
||||||
*
|
|
||||||
* @return true Device is connected to wifi network
|
|
||||||
* @return false Device is not connected to wifi network
|
|
||||||
*/
|
|
||||||
bool isConnected();
|
|
||||||
/**
|
|
||||||
* @brief Internal variable to store the wifi SSID
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
std::string mPassword;
|
||||||
|
std::string mSSID;
|
||||||
};
|
};
|
|
@ -22,7 +22,7 @@ build_flags =
|
||||||
-D LV_CONF_SKIP
|
-D LV_CONF_SKIP
|
||||||
;------------- LVGL ------------------------------------------
|
;------------- LVGL ------------------------------------------
|
||||||
-D LV_MEM_CUSTOM=1
|
-D LV_MEM_CUSTOM=1
|
||||||
-D LV_MEM_SIZE="\(48U * 1024U\)"
|
-D LV_MEM_SIZE="(48U * 1024U)"
|
||||||
-D LV_FONT_MONTSERRAT_12=1
|
-D LV_FONT_MONTSERRAT_12=1
|
||||||
-D LV_FONT_MONTSERRAT_16=1
|
-D LV_FONT_MONTSERRAT_16=1
|
||||||
-D LV_FONT_MONTSERRAT_24=1
|
-D LV_FONT_MONTSERRAT_24=1
|
||||||
|
|
Loading…
Add table
Reference in a new issue