- Fix preferences lib
- Fix ADHOC
- Optimize API
- Implement full preferences lib
- Implement API with preferences lib
This commit is contained in:
ZanzyTHEbar 2022-08-15 05:54:06 +01:00
parent b69e6f9fad
commit 8ce0ea0860
12 changed files with 449 additions and 298 deletions

View File

@ -2,7 +2,7 @@
Preferences preferences;
ProjectConfig::ProjectConfig() : Config(&preferences ,"config"), _already_loaded(false) {}
ProjectConfig::ProjectConfig() : Config(&preferences, "config"), _already_loaded(false) {}
ProjectConfig::~ProjectConfig() {}
@ -18,19 +18,28 @@ void ProjectConfig::initConfig()
"",
0,
};
this->config.camera = {
0,
0,
0,
0,
};
this->config.networks = {
{
"",
"",
"",
0,
},
};
this->config.ap_network = {
"",
"",
0,
};
}
void ProjectConfig::load()
@ -42,13 +51,41 @@ void ProjectConfig::load()
return;
}
bool device_success = this->read("device", this->config.device);
bool camera_success = this->read("camera", this->config.camera);
bool network_info_success = this->read("network_info", this->config.networks);
bool device_name_success = this->read("device_name", this->config.device.name);
bool device_otapassword_success = this->read("ota_pass", this->config.device.OTAPassword);
bool device_otaport_success = this->read("ota_port", this->config.device.OTAPort);
bool device_success = device_name_success && device_otapassword_success && device_otaport_success;
bool camera_vflip_success = this->read("camera_vflip", this->config.camera.vflip);
bool camera_framesize_success = this->read("cameraFrmsz", this->config.camera.framesize);
bool camera_href_success = this->read("camera_href", this->config.camera.href);
bool camera_quality_success = this->read("camera_quality", this->config.camera.quality);
bool camera_success = camera_vflip_success && camera_framesize_success && camera_href_success && camera_quality_success;
bool network_info_success;
for (int i = 0; i < this->config.networks.size(); i++)
{
char buff[25];
snprintf(buff, sizeof(buff), "%d_name", i);
bool networks_name_success = this->read(buff, this->config.networks[i].name);
snprintf(buff, sizeof(buff), "%d_ssid", i);
bool networks_ssid_success = this->read(buff, this->config.networks[i].ssid);
snprintf(buff, sizeof(buff), "%d_password", i);
bool networks_password_success = this->read(buff, this->config.networks[i].password);
snprintf(buff, sizeof(buff), "%d_channel", i);
bool networks_channel_success = this->read(buff, this->config.networks[i].channel);
network_info_success = networks_name_success && networks_ssid_success && networks_password_success && networks_channel_success;
}
if (!device_success || !camera_success || !network_info_success)
{
log_e("Failed to load project config");
log_e("Failed to load project config - Generating config and restarting");
save();
delay(1000);
ESP.restart();
return;
}
@ -59,9 +96,28 @@ void ProjectConfig::load()
void ProjectConfig::save()
{
log_d("Saving project config");
this->write("device", this->config.device);
this->write("camera", this->config.camera);
this->write("network_info", this->config.networks);
this->write("device_name", this->config.device.name);
this->write("ota_pass", this->config.device.OTAPassword);
this->write("ota_port", this->config.device.OTAPort);
this->write("camera_vflip", this->config.camera.vflip);
this->write("cameraFrmsz", this->config.camera.framesize);
this->write("camera_href", this->config.camera.href);
this->write("camera_quality", this->config.camera.quality);
for (int i = 0; i < this->config.networks.size(); i++)
{
char buff[25];
snprintf(buff, sizeof(buff), "%d_name", i);
this->write(buff, this->config.networks[i].name);
snprintf(buff, sizeof(buff), "%d_ssid", i);
this->write(buff, this->config.networks[i].ssid);
snprintf(buff, sizeof(buff), "%d_password", i);
this->write(buff, this->config.networks[i].password);
snprintf(buff, sizeof(buff), "%d_channel", i);
this->write(buff, this->config.networks[i].channel);
}
}
void ProjectConfig::reset()
@ -79,8 +135,8 @@ void ProjectConfig::setDeviceConfig(const char *name, const char *OTAPassword, i
{
log_d("Updating device config");
this->config.device = {
name,
OTAPassword,
(char *)name,
(char *)OTAPassword,
*OTAPort,
};
if (shouldNotify)
@ -105,13 +161,13 @@ void ProjectConfig::setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t
}
}
void ProjectConfig::setWifiConfig(const char *networkName, const char *ssid, const char *password, bool shouldNotify)
void ProjectConfig::setWifiConfig(const char *networkName, const char *ssid, const char *password, uint8_t *channel, bool shouldNotify)
{
WiFiConfig_t *networkToUpdate = nullptr;
for (int i = 0; i < this->config.networks.size(); i++)
{
if (strcmp(this->config.networks[i].name, networkName) == 0)
if (strcmp(this->config.networks[i].name.c_str(), networkName) == 0)
networkToUpdate = &this->config.networks[i];
}
@ -119,9 +175,10 @@ void ProjectConfig::setWifiConfig(const char *networkName, const char *ssid, con
{
this->config.networks = {
{
networkName,
ssid,
password,
(char *)networkName,
(char *)ssid,
(char *)password,
*channel,
},
};
if (shouldNotify)
@ -129,3 +186,18 @@ void ProjectConfig::setWifiConfig(const char *networkName, const char *ssid, con
}
log_d("Updating wifi config");
}
void ProjectConfig::setAPWifiConfig(const char *ssid, const char *password, uint8_t *channel, bool shouldNotify)
{
this->config.ap_network = {
(char *)ssid,
(char *)password,
*channel,
};
log_d("Updating access point config");
if (shouldNotify)
{
this->notify(ObserverEvent::networksConfigUpdated);
}
}

View File

@ -4,6 +4,7 @@
#include <Arduino.h>
#include <preferencesAPI.hpp>
#include <vector>
#include <string>
#include "data/utilities/Observer.hpp"
@ -19,8 +20,8 @@ public:
struct DeviceConfig_t
{
const char *name;
const char *OTAPassword;
std::string name;
std::string OTAPassword;
int OTAPort;
bool data_json;
bool config_json;
@ -40,9 +41,17 @@ public:
struct WiFiConfig_t
{
const char *name;
const char *ssid;
const char *password;
std::string name;
std::string ssid;
std::string password;
uint8_t channel;
};
struct AP_WiFiConfig_t
{
std::string ssid;
std::string password;
uint8_t channel;
};
struct TrackerConfig_t
@ -50,15 +59,18 @@ public:
DeviceConfig_t device;
CameraConfig_t camera;
std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network;
};
DeviceConfig_t *getDeviceConfig() { return &this->config.device; }
CameraConfig_t *getCameraConfig() { return &this->config.camera; }
std::vector<WiFiConfig_t> *getWifiConfigs() { return &this->config.networks; }
AP_WiFiConfig_t *getAPWifiConfig() { return &this->config.ap_network; }
void setDeviceConfig(const char *name, const char *OTAPassword, int *OTAPort, bool shouldNotify);
void setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t *href, uint8_t *quality, bool shouldNotify);
void setWifiConfig(const char *networkName, const char *ssid, const char *password, bool shouldNotify);
void setWifiConfig(const char *networkName, const char *ssid, const char *password, uint8_t *channel, bool shouldNotify);
void setAPWifiConfig(const char *ssid, const char *password, uint8_t *channel, bool shouldNotify);
private:
const char *configFileName;

View File

@ -14,7 +14,8 @@ SerialManager::SerialManager(ProjectConfig *projectConfig) : projectConfig(proje
camera_config_quality{0},
wifi_config_name{0},
wifi_config_ssid{0},
wifi_config_password{0} {}
wifi_config_password{0},
wifi_config_channel(0) {}
SerialManager::~SerialManager() {}
@ -104,6 +105,9 @@ void SerialManager::parseData()
strtokIndx = strtok(NULL, ",");
strcpy(wifi_config_password, strtokIndx);
strtokIndx = strtok(NULL, ",");
wifi_config_channel = atoi(strtokIndx);
}
void SerialManager::handleSerial()
@ -115,7 +119,7 @@ void SerialManager::handleSerial()
parseData(); // split the data into tokens and store them in the data structure
projectConfig->setDeviceConfig(device_config_name, device_config_OTAPassword, &device_config_OTAPort, true); // set the values in the project config
projectConfig->setCameraConfig(&camera_config_vflip, &camera_config_framesize, &camera_config_href, &camera_config_quality, true); // set the values in the project config
projectConfig->setWifiConfig(wifi_config_name, wifi_config_ssid, wifi_config_password, true); // set the values in the project config
projectConfig->setWifiConfig(wifi_config_name, wifi_config_ssid, wifi_config_password, &wifi_config_channel, true); // set the values in the project config
projectConfig->save(); // save the config to the EEPROM
newData = false; // reset new data
}

View File

@ -30,6 +30,7 @@ public:
char wifi_config_name[32];
char wifi_config_ssid[100];
char wifi_config_password[100];
uint8_t wifi_config_channel;
private:

View File

@ -9,7 +9,7 @@ void OTA::SetupOTA()
log_e("Setting up OTA updates");
auto localConfig = _deviceConfig->getDeviceConfig();
if (strcmp(localConfig->OTAPassword, "") == 0)
if (strcmp(localConfig->OTAPassword.c_str(), "") == 0)
{
log_e("THE PASSWORD IS REQUIRED, [[ABORTING]]");
return;

View File

@ -6,25 +6,17 @@
#include "data/StateManager/StateManager.hpp"
#include "data/config/project_config.hpp"
extern "C"
{
#include <esp_err.h>
#include <esp_wifi.h>
#include <esp_event.h>
}
class WiFiHandler
{
public:
WiFiHandler(ProjectConfig *configManager, StateManager<WiFiState_e> *stateManager);
virtual ~WiFiHandler();
void setupWifi();
ProjectConfig *configManager;
StateManager<WiFiState_e> *stateManager;
private:
void setUpADHOC();
void adhoc(const char *ssid, const char *password, uint8_t channel);
void setWiFiConf(const char *value, uint8_t *location, wifi_config_t *conf);
std::unique_ptr<wifi_config_t> conf;
ProjectConfig *configManager;
private:
StateManager<WiFiState_e> *stateManager;
void iniSTA();
};
#endif // WIFIHANDLER_HPP

View File

@ -1,8 +1,7 @@
#include "WifiHandler.hpp"
#include <vector>
WiFiHandler::WiFiHandler(ProjectConfig *configManager, StateManager<WiFiState_e> *stateManager) : conf(new wifi_config_t),
configManager(configManager),
WiFiHandler::WiFiHandler(ProjectConfig *configManager, StateManager<WiFiState_e> *stateManager) : configManager(configManager),
stateManager(stateManager) {}
WiFiHandler::~WiFiHandler() {}
@ -28,7 +27,7 @@ void WiFiHandler::setupWifi()
{
log_i("Trying to connect to the %s network", networkIterator->ssid);
WiFi.begin(networkIterator->ssid, networkIterator->password);
WiFi.begin(networkIterator->ssid.c_str(), networkIterator->password.c_str());
count++;
if (!WiFi.isConnected())
@ -52,7 +51,7 @@ void WiFiHandler::setupWifi()
// we've tried all saved networks, none worked, let's error out
log_e("Could not connect to any of the save networks, check your Wifi credentials");
stateManager->setState(WiFiState_e::WiFiState_Error);
this->setUpADHOC();
this->iniSTA();
log_w("Setting up adhoc");
log_w("Please set your WiFi credentials and reboot the device");
stateManager->setState(WiFiState_e::WiFiState_ADHOC);
@ -80,53 +79,75 @@ void WiFiHandler::adhoc(const char *ssid, const char *password, uint8_t channel)
stateManager->setState(WiFiState_e::WiFiState_ADHOC);
}
/*
* *
*/
void WiFiHandler::setUpADHOC()
{
size_t ssidLen = strlen((char *)conf->ap.ssid);
size_t passwordLen = strlen((char *)conf->ap.password);
char ap_ssid[ssidLen + 1];
char ap_password[passwordLen + 1];
auto ret = esp_wifi_get_config(WIFI_IF_STA, &*conf);
if (ret == ESP_OK)
log_i("[INFO]: Setting Access Point...\n");
size_t ssidLen = strlen(configManager->getAPWifiConfig()->ssid.c_str());
size_t passwordLen = strlen(configManager->getAPWifiConfig()->password.c_str());
char ssid[ssidLen + 1];
char password[passwordLen + 1];
uint8_t channel = configManager->getAPWifiConfig()->channel;
if (ssidLen > 0 || passwordLen > 0)
{
memcpy(ap_ssid, conf->ap.ssid, ssidLen);
memcpy(ap_password, conf->ap.password, passwordLen);
strcpy(ssid, configManager->getAPWifiConfig()->ssid.c_str());
strcpy(password, configManager->getAPWifiConfig()->password.c_str());
channel = configManager->getAPWifiConfig()->channel;
}
else
{
strcpy(ssid, WIFI_AP_SSID);
strcpy(password, WIFI_AP_PASSWORD);
channel = ADHOC_CHANNEL;
}
ap_ssid[ssidLen] = '\0'; // Null-terminate the string
ap_password[passwordLen] = '\0'; // Null-terminate the string
this->adhoc(ssid, password, channel);
log_i("[INFO]: Configuring access point...\n");
log_d("[DEBUG]: ssid: %s\n", ssid);
log_d("[DEBUG]: password: %s\n", password);
log_d("[DEBUG]: channel: %d\n", channel);
}
void WiFiHandler::iniSTA()
{
log_i("[INFO]: Setting up station...\n");
int connection_timeout = 30000; // 30 seconds
unsigned long currentMillis = millis();
unsigned long _previousMillis = currentMillis;
log_i("Trying to connect to the %s network", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
if (!WiFi.isConnected())
log_i("\n\rCould not connect to %s, please try another network\n\r", WIFI_SSID);
else
{
log_i("\n\rSuccessfully connected to %s\n\r", WIFI_SSID);
stateManager->setState(WiFiState_e::WiFiState_Connected);
return;
}
if (ssidLen == 0)
while (WiFi.status() != WL_CONNECTED)
{
strcpy(ap_ssid, WIFI_SSID);
strcpy(ap_password, WIFI_PASSWORD);
conf->ap.channel = ADHOC_CHANNEL;
}
this->adhoc(ap_ssid, ap_password, conf->ap.channel);
}
// we can't assign wifiManager.resetSettings(); to reset, somehow it gets called straight away.
/**
* @brief Resets the wifi settings to the chosen settings.
*
* @param value - value to store - string.
* @param location - location to store the value. byte array - conf
*/
void WiFiHandler::setWiFiConf(const char *value, uint8_t *location, wifi_config_t *conf)
{
assert(conf != nullptr);
#if defined(ESP32)
if (WiFiGenericClass::getMode() != WIFI_MODE_NULL)
stateManager->setState(ProgramStates::DeviceStates::WiFiState_e::WiFiState_Connecting);
currentMillis = millis();
Serial.print(".");
delay(300);
if ((currentMillis - _previousMillis) >= connection_timeout)
{
esp_wifi_get_config(WIFI_IF_STA, conf);
memset(location, 0, sizeof(location));
for (int i = 0; i < sizeof(value) / sizeof(value[0]) && i < sizeof(location); i++)
location[i] = value[i];
esp_wifi_set_config(WIFI_IF_STA, conf);
log_i("[INFO]: WiFi connection timed out.\n");
// we've tried all saved networks, none worked, let's error out
log_e("Could not connect to any of the save networks, check your Wifi credentials");
stateManager->setState(WiFiState_e::WiFiState_Error);
this->iniSTA();
log_w("Setting up adhoc");
log_w("Please set your WiFi credentials and reboot the device");
stateManager->setState(WiFiState_e::WiFiState_ADHOC);
return;
}
}
#endif
}

View File

@ -4,7 +4,7 @@ void MDNSHandler::startMDNS()
{
ProjectConfig::DeviceConfig_t *deviceConfig = configManager->getDeviceConfig();
if (MDNS.begin(deviceConfig->name))
if (MDNS.begin(deviceConfig->name.c_str()))
{
stateManager->setState(MDNSState_e::MDNSState_Starting);
MDNS.addService("openIrisTracker", "tcp", 80);

View File

@ -10,6 +10,10 @@ const char *APIServer::MIMETYPE_HTML{"text/html"};
// const char *APIServer::MIMETYPE_ICO{"image/x-icon"};
const char *APIServer::MIMETYPE_JSON{"application/json"};
bool APIServer::ssid_write = false;
bool APIServer::pass_write = false;
bool APIServer::channel_write = false;
//*********************************************************************************************
//! API Server
//*********************************************************************************************
@ -125,29 +129,48 @@ void APIServer::command_handler(AsyncWebServerRequest *request)
//*********************************************************************************************
void APIServer::setSSID(const char *value)
{
#if ENABLE_ADHOC
network->setWiFiConf(value, network->conf->ap.ssid, &*network->conf);
#else
network->setWiFiConf(value, network->conf->sta.ssid, &*network->conf);
#endif // ENABLE_ADHOC
if (network->stateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
this->wifiConfig.local_WifiConfig[0].ssid = value;
else
this->wifiConfig.local_WifiConfig[1].ssid = value;
ssid_write = true;
}
void APIServer::setPass(const char *value)
{
#if ENABLE_ADHOC
network->setWiFiConf(network->conf->ap.password, value, &*network->conf);
#else
network->setWiFiConf(value, network->conf->sta.password, &*network->conf);
#endif // ENABLE_ADHOC
if (network->stateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
this->wifiConfig.local_WifiConfig[0].pass = value;
else
this->wifiConfig.local_WifiConfig[1].pass = value;
pass_write = true;
}
void APIServer::setChannel(const char *value)
{
#if ENABLE_ADHOC
network->setWiFiConf(value, network->conf->ap.channel, &*network->conf);
#else
network->setWiFiConf(value, &network->conf->sta.channel, &*network->conf);
#endif // ENABLE_ADHOC
if (network->stateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
this->wifiConfig.local_WifiConfig[0].channel = atoi(value);
else
this->wifiConfig.local_WifiConfig[1].channel = atoi(value);
channel_write = true;
}
/**
* * Trigger in main loop to save config to flash
* ? Should we force the users to update all config params before triggering a config write?
*/
void APIServer::triggerWifiConfigWrite()
{
if (ssid_write && pass_write && channel_write)
{
ssid_write = false;
pass_write = false;
channel_write = false;
if (network->stateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
network->configManager->setWifiConfig(wifiConfig.local_WifiConfig[0].ssid.c_str(), wifiConfig.local_WifiConfig[0].ssid.c_str(), wifiConfig.local_WifiConfig[0].pass.c_str(), &wifiConfig.local_WifiConfig[0].channel, true);
else
network->configManager->setWifiConfig(wifiConfig.local_WifiConfig[1].ssid.c_str(), wifiConfig.local_WifiConfig[1].ssid.c_str(), wifiConfig.local_WifiConfig[1].pass.c_str(), &wifiConfig.local_WifiConfig[1].channel, true);
network->configManager->save();
}
}
void APIServer::setDataJson(AsyncWebServerRequest *request)
@ -194,8 +217,7 @@ void APIServer::factoryReset()
APIServer::API_Utilities::API_Utilities() {}
std::string
APIServer::API_Utilities::shaEncoder(std::string data)
std::string APIServer::API_Utilities::shaEncoder(std::string data)
{
const char *data_c = data.c_str();
int size = 20;

View File

@ -21,7 +21,6 @@
#include "io/camera/cameraHandler.hpp"
#include "network/WifiHandler/WifiHandler.hpp"
class APIServer
{
private:
@ -62,11 +61,29 @@ private:
/* static const char *MIMETYPE_JPG; */
/* static const char *MIMETYPE_ICO; */
static const char *MIMETYPE_JSON;
static bool ssid_write;
static bool pass_write;
static bool channel_write;
struct LocalWifiConfig
{
std::string ssid;
std::string pass;
uint8_t channel;
};
struct WifiConfig
{
std::vector<LocalWifiConfig> local_WifiConfig;
};
WifiConfig wifiConfig;
public:
APIServer(int CONTROL_PORT, CameraHandler *cameraHandler, WiFiHandler *network);
void begin();
void startAPIServer();
void triggerWifiConfigWrite();
void findParam(AsyncWebServerRequest *request, const char *param, String &value);
class API_Utilities

View File

@ -14,8 +14,11 @@ default_envs = esp32Cam ; do not change this value
; The below options are available for all environments
; The ssid and password are requried for the trackers to connect to your network!!!
[wifi]
ssid="EyeTrackVR" ; your wifi network name goes here
password="test" ; Place your Wifi password here
ssid="" ; your wifi network name goes here
password="" ; your wifi network password goes here
channel=10 ; wifi channel
ap_ssid="EyeTrackVR" ; your AP wifi network name goes here
ap_password="test" ; Place your AP Wifi password here
OTAPassword="" ; if empty, no password will be required
OTAServerPort=3232
enableADHOC=0 ; 0 = disable, 1 = enable
@ -81,6 +84,8 @@ build_flags =
-DADHOC_CHANNEL=${wifi.adhocChannel} ;
-DWIFI_CHANNEL=${wifi.channel} ;
'-DMDNS_TRACKER_NAME="OpenIrisTracker"' ; Set the tracker name - The string literal tells platformio to include the quatations in the string - making sure that the compiler sees the string as a cstring
'-DOTA_PASSWORD=${wifi.OTAPassword}' ; Set the OTA password
@ -89,6 +94,10 @@ build_flags =
'-DWIFI_PASSWORD=${wifi.password}' ; Set the users wifi network password
'-DWIFI_AP_SSID=${wifi.ap_ssid}' ; Set the users wifi network name
'-DWIFI_AP_PASSWORD=${wifi.ap_password}' ; Set the users wifi network password
-DDEBUG_ESP_PORT=Serial
-DCORE_DEBUG_LEVEL=4

View File

@ -77,5 +77,6 @@ void loop()
{
ota.HandleOTAUpdate();
ledManager->displayStatus();
apiServer->triggerWifiConfigWrite();
// serialManager->handleSerial();
}