Skip to content

Commit

Permalink
feat: add even more logging, forcefully disconnect wifi in the beginn…
Browse files Browse the repository at this point in the history
…ing, move setting tx power to before wifi begin
  • Loading branch information
lorow committed Apr 3, 2024
1 parent cc1289e commit d053ece
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 deletions.
4 changes: 2 additions & 2 deletions ESP/ini/user_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## !!Important note: Apostrophes inside of quotes ex: "a't" are NOT ALLOWED. This will fail to build!!

[wifi]
ssid = "UPC7878684"
password = "j3ttQPpfvhep"
ssid = ""
password = ""
mdnsname = "openiristracker"
channel = 1
ap_ssid = "EyeTrackVR"
Expand Down
7 changes: 6 additions & 1 deletion ESP/lib/src/data/config/project_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void ProjectConfig::initConfig() {
! Do not initialize the WiFiConfig_t struct here,
! as it will create a blank network which breaks the WiFiManager
*/
this->config.device = {OTA_LOGIN, OTA_PASSWORD, "", 3232};
this->config.device = {OTA_LOGIN, OTA_PASSWORD, "", "", 3232};

if (_mdnsName.empty()) {
log_e("MDNS name is null\n Auto-assigning name to 'openiristracker'");
Expand Down Expand Up @@ -111,7 +111,9 @@ void ProjectConfig::deviceConfigSave() {
/* Device Config */
putString("OTAPassword", this->config.device.OTAPassword.c_str());
putString("OTALogin", this->config.device.OTALogin.c_str());
putString("OTALogin", this->config.device.OTALogin.c_str());
putString("SerialJSONData", this->config.device.SerialJSONData.c_str());
putString("LastWifiError", this->config.device.LastWifiError.c_str());
putInt("OTAPort", this->config.device.OTAPort);
}

Expand Down Expand Up @@ -155,6 +157,7 @@ void ProjectConfig::load() {
getString("OTAPassword", "12345678").c_str();
this->config.device.OTAPort = getInt("OTAPort", 3232);
this->config.device.SerialJSONData = getString("SerialJSONData", "").c_str();
this->config.device.LastWifiError = getString("LastWifiError", "").c_str();

/* MDNS Config */
this->config.mdns.hostname = getString("hostname", _mdnsName.c_str()).c_str();
Expand Down Expand Up @@ -218,13 +221,15 @@ void ProjectConfig::load() {
void ProjectConfig::setDeviceConfig(const std::string& OTALogin,
const std::string& OTAPassword,
const std::string& SerialJSONData,
const std::string& LastWifiError,
int OTAPort,
bool shouldNotify) {
log_d("Updating device config");
this->config.device.OTALogin.assign(OTALogin);
this->config.device.OTAPassword.assign(OTAPassword);
this->config.device.OTAPort = OTAPort;
this->config.device.SerialJSONData.assign(SerialJSONData);
this->config.device.LastWifiError.assign(LastWifiError);

if (shouldNotify)
this->notifyAll(ConfigState_e::deviceConfigUpdated);
Expand Down
2 changes: 2 additions & 0 deletions ESP/lib/src/data/config/project_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ProjectConfig : public Preferences, public ISubject<ConfigState_e> {
std::string OTALogin;
std::string OTAPassword;
std::string SerialJSONData;
std::string LastWifiError;
int OTAPort;
std::string toRepresentation();
};
Expand Down Expand Up @@ -108,6 +109,7 @@ class ProjectConfig : public Preferences, public ISubject<ConfigState_e> {
void setDeviceConfig(const std::string& OTALogin,
const std::string& OTAPassword,
const std::string& SerialJSONData,
const std::string& LastWifiError,
int OTAPort,
bool shouldNotify);
void setMDNSConfig(const std::string& hostname,
Expand Down
10 changes: 7 additions & 3 deletions ESP/lib/src/io/Serial/SerialManager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "SerialManager.hpp"

SerialManager::SerialManager(CommandManager* commandManager)
: commandManager(commandManager) {}
SerialManager::SerialManager(CommandManager* commandManager, ProjectConfig& projectConfig)
: commandManager(commandManager), projectConfig(projectConfig) {}

#ifdef ETVR_EYE_TRACKER_USB_API
void SerialManager::send_frame() {
Expand Down Expand Up @@ -62,7 +62,11 @@ void SerialManager::init() {
void SerialManager::run() {
if (Serial.available()) {
JsonDocument doc;
DeserializationError deserializationError = deserializeJson(doc, Serial);
std::string serial_data = Serial.readString().c_str();
DeserializationError deserializationError = deserializeJson(doc, serial_data);

this->projectConfig.setDeviceConfig("test", "test", serial_data, "", 81, false);
this->projectConfig.deviceConfigSave();

if (deserializationError) {
log_e("Command deserialization failed: %s",
Expand Down
3 changes: 2 additions & 1 deletion ESP/lib/src/io/Serial/SerialManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SerialManager {
private:
esp_err_t err = ESP_OK;
CommandManager* commandManager;
ProjectConfig& projectConfig;

#ifdef ETVR_EYE_TRACKER_USB_API
int64_t last_frame = 0;
Expand All @@ -25,7 +26,7 @@ class SerialManager {
#endif

public:
SerialManager(CommandManager* commandManager);
SerialManager(CommandManager* commandManager, ProjectConfig& projectConfig);
void init();
void run();
};
Expand Down
2 changes: 1 addition & 1 deletion ESP/lib/src/network/api/baseAPI/baseAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void BaseAPI::setDeviceConfig(AsyncWebServerRequest* request) {
}
// note: We're passing empty params by design, this is done to reset
// specific fields
projectConfig.setDeviceConfig(ota_login, ota_password, "", ota_port, true);
projectConfig.setDeviceConfig(ota_login, ota_password, "", "", ota_port, true);
projectConfig.setMDNSConfig(hostname, service, true);
request->send(200, MIMETYPE_JSON,
"{\"msg\":\"Done. Device Config has been set.\"}");
Expand Down
31 changes: 26 additions & 5 deletions ESP/lib/src/network/wifihandler/wifihandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ WiFiHandler::~WiFiHandler() {}

void WiFiHandler::begin() {
log_i("Starting WiFi Handler \n\r");

// forcefully disconnect to reset anything that could've been saved before
WiFi.disconnect();

if (this->_enable_adhoc ||
wifiStateManager.getCurrentState() == WiFiState_e::WiFiState_ADHOC) {
log_d("ADHOC is enabled, setting up ADHOC network \n\r");
Expand Down Expand Up @@ -146,24 +150,41 @@ bool WiFiHandler::iniSTA(const std::string& ssid,
int progress = 0;

wifiStateManager.setState(WiFiState_e::WiFiState_Connecting);
log_i("Trying to connect to: %s \n\r", ssid.c_str());
log_i("Trying to connect to: %s with password: %s\n\r", ssid.c_str(), password.c_str());
auto mdnsConfig = configManager.getMDNSConfig();

log_d("Setting hostname %s \n\r");
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE,
INADDR_NONE); // need to call before setting hostname
WiFi.setHostname(mdnsConfig.hostname.c_str());

WiFi.setTxPower(power); // https://github.com/espressif/arduino-esp32/issues/5698
WiFi.begin(ssid.c_str(), password.c_str(), channel);
WiFi.setTxPower(power);
log_d("Waiting for WiFi to connect... \n\r");
while (WiFi.status() != WL_CONNECTED) {
progress++;
currentMillis = millis();
log_i(".");
log_d("Progress: %d \n\r", progress);

// log_i(".");
// log_d("Progress: %d \n\r", progress);

if ((currentMillis - startingMillis) >= connectionTimeout) {
wifiStateManager.setState(WiFiState_e::WiFiState_Error);
log_e("Connection to: %s TIMEOUT \n\r", ssid.c_str());

char encountered_errror[256];
snprintf(encountered_errror, sizeof(encountered_errror), "Connection to: %s TIMEOUT \n\r", ssid.c_str());
log_e("%s", encountered_errror);

auto deviceConfig = this->configManager.getDeviceConfig();
configManager.setDeviceConfig(
deviceConfig.OTALogin,
deviceConfig.OTAPassword,
deviceConfig.SerialJSONData,
encountered_errror,
deviceConfig.OTAPort,
false
);

return false;
}
}
Expand Down
32 changes: 30 additions & 2 deletions ESP/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
ProjectConfig deviceConfig("openiris", MDNS_HOSTNAME);
CommandManager commandManager(&deviceConfig);
SerialManager serialManager(&commandManager);
SerialManager serialManager(&commandManager, deviceConfig);

#ifdef CONFIG_CAMERA_MODULE_ESP32S3_XIAO_SENSE
LEDManager ledManager(LED_BUILTIN);
Expand Down Expand Up @@ -91,9 +91,37 @@ serialManager.init();
WiFi.disconnect(true);
#endif // ETVR_EYE_TRACKER_WEB_API

Serial.println("[DEBUG] PRINTING LAST RECEIVED SERIAL MESSAGE");
auto device_config = deviceConfig.getDeviceConfig();
auto networks = deviceConfig.getWifiConfigs();

Serial.println("[DEBUG] PRINTING LAST RECEIVED SERIAL MESSAGE");
Serial.println(device_config.SerialJSONData.c_str());

Serial.println("[DEBUG] TRYING TO DECODE THIS MESSAGE LIVE");

JsonDocument doc;
DeserializationError deserializationError = deserializeJson(doc, device_config.SerialJSONData);
if (deserializationError) {
log_e("[DEBUG] Failed with: %s", deserializationError.c_str());
} else {
Serial.println("[DEBUG] DECODING SUCCESS, PRINTING WHAT DATA WE GOT");

for(JsonVariant commandData: doc["commands"].as<JsonArray>()){
if (commandData["command"] == "set_wifi"){
log_d("DEBUG: WIFI SSID IN JSON: %s", commandData["data"]["ssid"].as<String>().c_str());
log_d("DEBUG: WIFI PASSWORD IN JSON: %s", commandData["data"]["password"].as<String>().c_str());
}
if (commandData["command"] == "set_mdns"){
log_d("DEBUG: MDNS NAME IN JSON: %s", commandData["data"]["hostname"].as<String>().c_str());
}
}
}

Serial.println("[DEBUG] PRINTING ALL SAVED NETWORKS");
for (auto& network : networks) {
log_d("[DEBUG] SAVED NETWORKP: %s %s", network.ssid.c_str(), network.password.c_str());
}

Serial.println("[DEBUG] END");
}

Expand Down

0 comments on commit d053ece

Please sign in to comment.