- implement setTxPower into main API
- implement temporary dedicated global method
- implement wifiConfig method per network
This commit is contained in:
ZanzyTHEbar 2022-12-01 21:25:29 +00:00
parent 6a1192b356
commit c850372243
9 changed files with 126 additions and 46 deletions

View File

@ -75,6 +75,7 @@ void ProjectConfig::wifiConfigSave()
std::string ssid = "ssid"; std::string ssid = "ssid";
std::string password = "pass"; std::string password = "pass";
std::string channel = "channel"; std::string channel = "channel";
std::string power = "power";
for (int i = 0; i < this->config.networks.size(); i++) for (int i = 0; i < this->config.networks.size(); i++)
{ {
char buffer[2]; char buffer[2];
@ -84,16 +85,13 @@ void ProjectConfig::wifiConfigSave()
ssid.append(iter_str); ssid.append(iter_str);
password.append(iter_str); password.append(iter_str);
channel.append(iter_str); channel.append(iter_str);
power.append(iter_str);
putString(name.c_str(), this->config.networks[i].name.c_str()); putString(name.c_str(), this->config.networks[i].name.c_str());
putString(ssid.c_str(), this->config.networks[i].ssid.c_str()); putString(ssid.c_str(), this->config.networks[i].ssid.c_str());
putString(password.c_str(), this->config.networks[i].password.c_str()); putString(password.c_str(), this->config.networks[i].password.c_str());
putInt(channel.c_str(), this->config.networks[i].channel); putUInt(channel.c_str(), this->config.networks[i].channel);
putUInt(power.c_str(), this->config.networks[i].power);
name = "name";
ssid = "ssid";
password = "pass";
channel = "channel";
} }
/* AP Config */ /* AP Config */
@ -118,6 +116,12 @@ void ProjectConfig::mdnsConfigSave()
putString("service", this->config.mdns.service.c_str()); putString("service", this->config.mdns.service.c_str());
} }
void ProjectConfig::wifiTxPowerConfigSave()
{
/* Device Config */
putInt("power", this->config.txpower.power);
}
void ProjectConfig::cameraConfigSave() void ProjectConfig::cameraConfigSave()
{ {
/* Camera Config */ /* Camera Config */
@ -150,12 +154,16 @@ void ProjectConfig::load()
/* MDNS Config */ /* MDNS Config */
this->config.mdns.hostname = getString("hostname").c_str(); this->config.mdns.hostname = getString("hostname").c_str();
this->config.mdns.service = getString("service").c_str(); this->config.mdns.service = getString("service").c_str();
/* Wifi TX Power Config */
this->config.txpower.power = getUInt("power");
/* WiFi Config */ /* WiFi Config */
int networkCount = getInt("networkCount", 0); int networkCount = getInt("networkCount", 0);
std::string name = "name"; std::string name = "name";
std::string ssid = "ssid"; std::string ssid = "ssid";
std::string password = "pass"; std::string password = "pass";
std::string channel = "channel"; std::string channel = "channel";
std::string power = "power";
for (int i = 0; i < networkCount; i++) for (int i = 0; i < networkCount; i++)
{ {
char buffer[2]; char buffer[2];
@ -165,16 +173,13 @@ void ProjectConfig::load()
ssid.append(iter_str); ssid.append(iter_str);
password.append(iter_str); password.append(iter_str);
channel.append(iter_str); channel.append(iter_str);
power.append(iter_str);
const std::string &temp_1 = getString(name.c_str()).c_str(); const std::string &temp_1 = getString(name.c_str()).c_str();
const std::string &temp_2 = getString(ssid.c_str()).c_str(); const std::string &temp_2 = getString(ssid.c_str()).c_str();
const std::string &temp_3 = getString(password.c_str()).c_str(); const std::string &temp_3 = getString(password.c_str()).c_str();
uint8_t temp_4 = getUInt(channel.c_str()); uint8_t temp_4 = getUInt(channel.c_str());
uint8_t temp_5 = getUInt(power.c_str());
name = "name";
ssid = "ssid";
password = "pass";
channel = "channel";
//! push_back creates a copy of the object, so we need to use emplace_back //! push_back creates a copy of the object, so we need to use emplace_back
this->config.networks.emplace_back( this->config.networks.emplace_back(
@ -182,6 +187,7 @@ void ProjectConfig::load()
temp_2, temp_2,
temp_3, temp_3,
temp_4, temp_4,
temp_5,
false); // false because the networks we store in the config are the ones we want the esp to connect to, rather than host as AP false); // false because the networks we store in the config are the ones we want the esp to connect to, rather than host as AP
} }
@ -240,38 +246,46 @@ void ProjectConfig::setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t
this->notify(ObserverEvent::cameraConfigUpdated); this->notify(ObserverEvent::cameraConfigUpdated);
} }
void ProjectConfig::setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify) void ProjectConfig::setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, uint8_t *power, bool adhoc, bool shouldNotify)
{ {
// we store the ADHOC flag as false because the networks we store in the config are the ones we want the esp to connect to, rather than host as AP, and here we're just updating them // we store the ADHOC flag as false because the networks we store in the config are the ones we want the esp to connect to, rather than host as AP, and here we're just updating them
size_t size = this->config.networks.size(); size_t size = this->config.networks.size();
// we're allowing to store up to three additional networks // we're allowing to store up to three additional networks
if (size == 0) { if (size == 0)
{
Serial.println("No networks, We're adding a new network"); Serial.println("No networks, We're adding a new network");
this->config.networks.emplace_back( this->config.networks.emplace_back(
networkName, networkName,
ssid, ssid,
password, password,
*channel, *channel,
*power,
false); false);
} }
int networkToUpdate = -1; int networkToUpdate = -1;
for (int i = 0; i < size; i++){ for (int i = 0; i < size; i++)
if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0){ {
if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0)
{
// we've found a preexisting network, let's upate it // we've found a preexisting network, let's upate it
networkToUpdate = i; networkToUpdate = i;
break; break;
} }
} }
if (networkToUpdate >= 0) { if (networkToUpdate >= 0)
{
this->config.networks[networkToUpdate].name = networkName; this->config.networks[networkToUpdate].name = networkName;
this->config.networks[networkToUpdate].ssid = ssid; this->config.networks[networkToUpdate].ssid = ssid;
this->config.networks[networkToUpdate].password = password; this->config.networks[networkToUpdate].password = password;
this->config.networks[networkToUpdate].channel = *channel; this->config.networks[networkToUpdate].channel = *channel;
this->config.networks[networkToUpdate].power = *power;
this->config.networks[networkToUpdate].adhoc = false; this->config.networks[networkToUpdate].adhoc = false;
} else if (size < 3) { }
else if (size < 3)
{
Serial.println("We're adding a new network"); Serial.println("We're adding a new network");
// we don't have that network yet, we can add it as we still have some space // we don't have that network yet, we can add it as we still have some space
// we're using emplace_back as push_back will create a copy of it, we want to avoid that // we're using emplace_back as push_back will create a copy of it, we want to avoid that
@ -280,6 +294,7 @@ void ProjectConfig::setWifiConfig(const std::string &networkName, const std::str
ssid, ssid,
password, password,
*channel, *channel,
*power,
false); false);
} }
@ -299,13 +314,21 @@ void ProjectConfig::setAPWifiConfig(const std::string &ssid, const std::string &
this->notify(ObserverEvent::networksConfigUpdated); this->notify(ObserverEvent::networksConfigUpdated);
} }
void ProjectConfig::setWiFiTxPower(uint8_t *power, bool shouldNotify)
{
this->config.txpower.power = *power;
log_d("Updating wifi tx power");
if (shouldNotify)
this->notify(ObserverEvent::wifiTxPowerUpdated);
}
std::string ProjectConfig::DeviceConfig_t::toRepresentation() std::string ProjectConfig::DeviceConfig_t::toRepresentation()
{ {
std::string json = Helpers::format_string( std::string json = Helpers::format_string(
"\"device_config\": {\"OTAPassword\": \"%s\", \"OTAPort\": %u}", "\"device_config\": {\"OTAPassword\": \"%s\", \"OTAPort\": %u}",
this->OTAPassword.c_str(), this->OTAPassword.c_str(),
this->OTAPort this->OTAPort);
);
return json; return json;
} }
@ -314,8 +337,7 @@ std::string ProjectConfig::MDNSConfig_t::toRepresentation()
std::string json = Helpers::format_string( std::string json = Helpers::format_string(
"\"mdns_config\": {\"hostname\": \"%s\", \"service\": \"%s\"}", "\"mdns_config\": {\"hostname\": \"%s\", \"service\": \"%s\"}",
this->hostname.c_str(), this->hostname.c_str(),
this->service.c_str() this->service.c_str());
);
return json; return json;
} }
@ -365,3 +387,4 @@ ProjectConfig::CameraConfig_t *ProjectConfig::getCameraConfig() { return &this->
std::vector<ProjectConfig::WiFiConfig_t> *ProjectConfig::getWifiConfigs() { return &this->config.networks; } std::vector<ProjectConfig::WiFiConfig_t> *ProjectConfig::getWifiConfigs() { return &this->config.networks; }
ProjectConfig::AP_WiFiConfig_t *ProjectConfig::getAPWifiConfig() { return &this->config.ap_network; } ProjectConfig::AP_WiFiConfig_t *ProjectConfig::getAPWifiConfig() { return &this->config.ap_network; }
ProjectConfig::MDNSConfig_t *ProjectConfig::getMDNSConfig() { return &this->config.mdns; } ProjectConfig::MDNSConfig_t *ProjectConfig::getMDNSConfig() { return &this->config.mdns; }
ProjectConfig::WiFiTxPower_t *ProjectConfig::getWiFiTxPowerConfig() { return &this->config.txpower; }

View File

@ -20,6 +20,7 @@ public:
void cameraConfigSave(); void cameraConfigSave();
void deviceConfigSave(); void deviceConfigSave();
void mdnsConfigSave(); void mdnsConfigSave();
void wifiTxPowerConfigSave();
bool reset(); bool reset();
void initConfig(); void initConfig();
@ -55,15 +56,18 @@ public:
const std::string &ssid, const std::string &ssid,
const std::string &password, const std::string &password,
uint8_t channel, uint8_t channel,
uint8_t power,
bool adhoc) : name(std::move(name)), bool adhoc) : name(std::move(name)),
ssid(std::move(ssid)), ssid(std::move(ssid)),
password(std::move(password)), password(std::move(password)),
channel(channel), channel(channel),
adhoc(adhoc) {} adhoc(adhoc),
power(power) {}
std::string name; std::string name;
std::string ssid; std::string ssid;
std::string password; std::string password;
uint8_t channel; uint8_t channel;
uint8_t power;
bool adhoc; bool adhoc;
std::string toRepresentation(); std::string toRepresentation();
@ -78,6 +82,12 @@ public:
std::string toRepresentation(); std::string toRepresentation();
}; };
struct WiFiTxPower_t
{
uint8_t power;
std::string toRepresentation();
};
struct TrackerConfig_t struct TrackerConfig_t
{ {
DeviceConfig_t device; DeviceConfig_t device;
@ -85,6 +95,7 @@ public:
std::vector<WiFiConfig_t> networks; std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network; AP_WiFiConfig_t ap_network;
MDNSConfig_t mdns; MDNSConfig_t mdns;
WiFiTxPower_t txpower;
}; };
DeviceConfig_t *getDeviceConfig(); DeviceConfig_t *getDeviceConfig();
@ -92,12 +103,14 @@ public:
std::vector<WiFiConfig_t> *getWifiConfigs(); std::vector<WiFiConfig_t> *getWifiConfigs();
AP_WiFiConfig_t *getAPWifiConfig(); AP_WiFiConfig_t *getAPWifiConfig();
MDNSConfig_t *getMDNSConfig(); MDNSConfig_t *getMDNSConfig();
WiFiTxPower_t *getWiFiTxPowerConfig();
void setDeviceConfig(const std::string &OTAPassword, int *OTAPort, bool shouldNotify); void setDeviceConfig(const std::string &OTAPassword, int *OTAPort, bool shouldNotify);
void setMDNSConfig(const std::string &hostname, const std::string &service, bool shouldNotify); void setMDNSConfig(const std::string &hostname, const std::string &service, bool shouldNotify);
void setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t *href, uint8_t *quality, uint8_t *brightness, bool shouldNotify); void setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t *href, uint8_t *quality, uint8_t *brightness, bool shouldNotify);
void setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify); void setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, uint8_t *power, bool adhoc, bool shouldNotify);
void setAPWifiConfig(const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify); void setAPWifiConfig(const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify);
void setWiFiTxPower(uint8_t *power, bool shouldNotify);
private: private:
TrackerConfig_t config; TrackerConfig_t config;

View File

@ -12,6 +12,7 @@ namespace ObserverEvent
cameraConfigUpdated = 3, cameraConfigUpdated = 3,
networksConfigUpdated = 4, networksConfigUpdated = 4,
mdnsConfigUpdated = 5, mdnsConfigUpdated = 5,
wifiTxPowerUpdated = 6,
}; };
} }

View File

@ -21,6 +21,7 @@ public:
ProjectConfig *configManager; ProjectConfig *configManager;
StateManager<WiFiState_e> *stateManager; StateManager<WiFiState_e> *stateManager;
ProjectConfig::WiFiTxPower_t *txpower;
bool _enable_adhoc; bool _enable_adhoc;
@ -32,5 +33,6 @@ private:
std::string ssid; std::string ssid;
std::string password; std::string password;
uint8_t channel; uint8_t channel;
uint8_t power;
}; };
#endif // WIFIHANDLER_HPP #endif // WIFIHANDLER_HPP

View File

@ -7,15 +7,18 @@ WiFiHandler::WiFiHandler(ProjectConfig *configManager,
const std::string &password, const std::string &password,
uint8_t channel) : configManager(configManager), uint8_t channel) : configManager(configManager),
stateManager(stateManager), stateManager(stateManager),
txpower(NULL),
ssid(ssid), ssid(ssid),
password(password), password(password),
channel(channel), channel(channel),
power(0),
_enable_adhoc(false) {} _enable_adhoc(false) {}
WiFiHandler::~WiFiHandler() {} WiFiHandler::~WiFiHandler() {}
void WiFiHandler::setupWifi() void WiFiHandler::setupWifi()
{ {
txpower = configManager->getWiFiTxPowerConfig();
if (this->_enable_adhoc || stateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC) if (this->_enable_adhoc || stateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
{ {
this->setUpADHOC(); this->setUpADHOC();
@ -63,9 +66,10 @@ void WiFiHandler::setupWifi()
{ {
log_i("Trying to connect to the %s network", networkIterator->ssid.c_str()); log_i("Trying to connect to the %s network", networkIterator->ssid.c_str());
WiFi.begin(networkIterator->ssid.c_str(), networkIterator->password.c_str()); WiFi.begin(networkIterator->ssid.c_str(), networkIterator->password.c_str());
WiFi.setTxPower(WIFI_POWER_5dBm); // WiFi.setTxPower(WIFI_POWER_5dBm);
// WiFi.setTxPower((wifi_power_t)networkIterator->power);
WiFi.setTxPower((wifi_power_t)txpower->power);
count++; count++;
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
progress++; progress++;
@ -101,7 +105,7 @@ void WiFiHandler::adhoc(const char *ssid, const char *password, uint8_t channel)
Serial.printf("[INFO]: AP IP address: %s.\r\n", IP.toString().c_str()); Serial.printf("[INFO]: AP IP address: %s.\r\n", IP.toString().c_str());
// You can remove the password parameter if you want the AP to be open. // You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password, channel); // AP mode with password WiFi.softAP(ssid, password, channel); // AP mode with password
WiFi.setTxPower(WIFI_POWER_11dBm); WiFi.setTxPower((wifi_power_t)txpower->power);
} }
/* /*
@ -152,7 +156,7 @@ void WiFiHandler::iniSTA()
} }
WiFi.begin(this->ssid.c_str(), this->password.c_str(), this->channel); WiFi.begin(this->ssid.c_str(), this->password.c_str(), this->channel);
WiFi.setTxPower(WIFI_POWER_5dBm); WiFi.setTxPower((wifi_power_t)txpower->power);
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
stateManager->setState(WiFiState_e::WiFiState_Connecting); stateManager->setState(WiFiState_e::WiFiState_Connecting);

View File

@ -73,6 +73,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request)
std::string ssid; std::string ssid;
std::string password; std::string password;
uint8_t channel = 0; uint8_t channel = 0;
uint8_t power = 0;
uint8_t adhoc = 0; uint8_t adhoc = 0;
log_d("Number of Params: %d", params); log_d("Number of Params: %d", params);
@ -95,6 +96,10 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request)
{ {
channel = (uint8_t)atoi(param->value().c_str()); channel = (uint8_t)atoi(param->value().c_str());
} }
else if (param->name() == "power")
{
power = (uint8_t)atoi(param->value().c_str());
}
else if (param->name() == "adhoc") else if (param->name() == "adhoc")
{ {
adhoc = (uint8_t)atoi(param->value().c_str()); adhoc = (uint8_t)atoi(param->value().c_str());
@ -103,7 +108,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request)
log_i("%s[%s]: %s\n", _networkMethodsMap[request->method()].c_str(), param->name().c_str(), param->value().c_str()); log_i("%s[%s]: %s\n", _networkMethodsMap[request->method()].c_str(), param->name().c_str(), param->value().c_str());
} }
// note: We're passing empty params by design, this is done to reset specific fields // note: We're passing empty params by design, this is done to reset specific fields
projectConfig->setWifiConfig(networkName, ssid, password, &channel, adhoc, true); projectConfig->setWifiConfig(networkName, ssid, password, &channel, &power, adhoc, true);
/* if (WiFiStateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC) /* if (WiFiStateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
{ {
@ -208,6 +213,36 @@ void BaseAPI::setDeviceConfig(AsyncWebServerRequest *request)
} }
} }
void BaseAPI::setWiFiTXPower(AsyncWebServerRequest *request)
{
switch (_networkMethodsMap_enum[request->method()])
{
case GET:
{
request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Request\"}");
break;
}
case POST:
{
int params = request->params();
uint8_t txPower = 0;
for (int i = 0; i < params; i++)
{
AsyncWebParameter *param = request->getParam(i);
if (param->name() == "txPower")
{
txPower = atoi(param->value().c_str());
}
}
projectConfig->setWiFiTxPower(&txPower, true);
projectConfig->wifiTxPowerConfigSave();
request->send(200, MIMETYPE_JSON, "{\"msg\":\"Done. TX Power has been set.\"}");
}
}
}
void BaseAPI::rebootDevice(AsyncWebServerRequest *request) void BaseAPI::rebootDevice(AsyncWebServerRequest *request)
{ {
switch (_networkMethodsMap_enum[request->method()]) switch (_networkMethodsMap_enum[request->method()])

View File

@ -44,6 +44,7 @@ protected:
protected: protected:
/* Commands */ /* Commands */
void setWiFi(AsyncWebServerRequest *request); void setWiFi(AsyncWebServerRequest *request);
void setWiFiTXPower(AsyncWebServerRequest *request);
void getJsonConfig(AsyncWebServerRequest *request); void getJsonConfig(AsyncWebServerRequest *request);
void factoryReset(AsyncWebServerRequest *request); void factoryReset(AsyncWebServerRequest *request);
void setDeviceConfig(AsyncWebServerRequest *request); void setDeviceConfig(AsyncWebServerRequest *request);

View File

@ -38,6 +38,7 @@ void APIServer::setupServer()
routes.emplace("setDevice", &APIServer::setDeviceConfig); routes.emplace("setDevice", &APIServer::setDeviceConfig);
routes.emplace("rebootDevice", &APIServer::rebootDevice); routes.emplace("rebootDevice", &APIServer::rebootDevice);
routes.emplace("getStoredConfig", &APIServer::getJsonConfig); routes.emplace("getStoredConfig", &APIServer::getJsonConfig);
routes.emplace("setTxPower", &APIServer::setWiFiTXPower);
// Camera Routes // Camera Routes
routes.emplace("setCamera", &APIServer::setCamera); routes.emplace("setCamera", &APIServer::setCamera);
routes.emplace("restartCamera", &APIServer::restartCamera); routes.emplace("restartCamera", &APIServer::restartCamera);

View File

@ -1 +1 @@
450 456