Skip to content

Commit

Permalink
refactor: Update NetworkManager configuration and API handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanzyTHEbar committed Mar 31, 2024
1 parent 797ab15 commit 8c425c9
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 146 deletions.
6 changes: 3 additions & 3 deletions NetworkManager/examples/captivePortal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ EasyNetworkManager networkManager("easynetwork", MDNS_HOSTNAME, WIFI_SSID,
WIFI_PASSWORD, 1, "_easynetwork", "test",
"_tcp", "_api_port", "80", true, false);

DNSServer dnsServer;

/**
* @brief Setup the AsyncServer Instance
* @note The AsyncServer constructor takes 5 parameters:
Expand All @@ -36,7 +34,7 @@ DNSServer dnsServer;
* @param command_path The path to the command handler
*/
AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
"/wifimanager", "/mycommands");
"/wifimanager", "/mycommands", "/json");

/**
* @brief Setup the API Server Instance
Expand All @@ -46,6 +44,8 @@ AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
*/
APIServer api(networkManager.configHandler->config, async_server);

DNSServer dnsServer;

// Note: Here are two functions that can be used to handle custom API requests
void printHelloWorld(AsyncWebServerRequest* request) {
Serial.println("Hello World!");
Expand Down
42 changes: 35 additions & 7 deletions NetworkManager/examples/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EasyNetworkManager networkManager("easynetwork", MDNS_HOSTNAME, WIFI_SSID,
* @param command_path The path to the command handler
*/
AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
"/wifimanager", "/mycommands");
"/wifimanager", "/mycommands", "/json");

/**
* @brief Setup the API Server Instance
Expand All @@ -43,14 +43,43 @@ AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
*/
APIServer api(networkManager.configHandler->config, async_server);

void printHelloWorld(AsyncWebServerRequest* request) {
Serial.println("Hello World!");
request->send(200, "text/plain", "Hello World!");
}
class Temp {
public:
Temp() {
Serial.println("Temp created");
}
~Temp() {
Serial.println("Temp destroyed");
}
// Note: Here is a function that can be used to handle custom API requests
// inside Note: of a class
void grabParams(AsyncWebServerRequest* request) {
int params = request->params();
log_d("Number of Params: %d", params);
std::string y;
std::string x;
for (int i = 0; i < params; i++) {
AsyncWebParameter* param = request->getParam(i);
if (param->name() == "Axes1") {
x = param->value().c_str();
} else if (param->name() == "Axes2") {
y = param->value().c_str();
}
}
Serial.printf("Axes1: %s, Axes2: %s\n", x.c_str(), y.c_str());
request->send(200, "text/plain", "OK");
}
};

void setupServer() {
// add command handlers to the API server
// you can add as many as you want - you can also add methods.
log_d("[SETUP]: Starting API Server");
api.addAPICommand("helloWorld", printHelloWorld);
api.addAPICommand("paramsClass", [&](AsyncWebServerRequest* request) {
Temp t;
t.grabParams(request);
});

api.begin();
log_d("[SETUP]: API Server Started");
}
Expand All @@ -59,7 +88,6 @@ void setup() {
Serial.begin(115200);
pinMode(4, OUTPUT);
Serial.println("\nHello, EasyNetworkManager!");

networkManager.begin();

/**
Expand Down
17 changes: 10 additions & 7 deletions NetworkManager/examples/customConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <Arduino.h>
#include <EasyNetworkManager.h>
//! Only needed for BLOCKING OTA. AsyncOTA is recommended and builtin
// #include <network/ota/basic_ota.hpp>

/**
* @brief Setup the EasyNetworkManager Instance
Expand Down Expand Up @@ -35,7 +33,15 @@ EasyNetworkManager networkManager("easynetwork", MDNS_HOSTNAME, WIFI_SSID,
* @param command_path The path to the command handler
*/
AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
"/wifimanager", "/mycommands");
"/wifimanager", "/mycommands", "/json");

/**
* @brief Setup the API Server Instance
* @note The API Server constructor takes 2 parameters:
* @param config The config manager
* @param server The AsyncServer instance
*/
APIServer api(networkManager.configHandler->config, async_server);

/**
* @brief Setup the API Server Instance
Expand Down Expand Up @@ -111,9 +117,6 @@ void setup() {
}
}
});
// ota.begin();
}

void loop() {
// ota.handleOTAUpdate();
}
void loop() {}
104 changes: 9 additions & 95 deletions NetworkManager/examples/customHTML.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <Arduino.h>
#include <EasyNetworkManager.h>
//! Only needed for BLOCKING OTA. AsyncOTA is recommended and builtin
// #include <network/ota/basic_ota.hpp>

/**
* @brief Setup the EasyNetworkManager Instance
Expand Down Expand Up @@ -35,7 +33,7 @@ EasyNetworkManager networkManager("easynetwork", MDNS_HOSTNAME, WIFI_SSID,
* @param command_path The path to the command handler
*/
AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
"/wifimanager", "/mycommands");
"/wifimanager", "/mycommands", "/json");

/**
* @brief Setup the API Server Instance
Expand All @@ -45,84 +43,13 @@ AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
*/
APIServer api(networkManager.configHandler->config, async_server);

// Note: Not required if you are going to use the AsyncOTA feature
// OTA ota(networkManager.configHandler->config);

class CustomConfig : public CustomConfigInterface {
void save() override {
Serial.println("Saving custom config");
}

void load() override {
Serial.println("Loading custom config");
}
};

CustomConfig customConfig;

class Temp {
public:
Temp() {
Serial.println("Temp created");
}
~Temp() {
Serial.println("Temp destroyed");
}
// Note: Here is a function that can be used to handle custom API requests
// inside Note: of a class
void grabParams(AsyncWebServerRequest* request) {
int params = request->params();
log_d("Number of Params: %d", params);
std::string y;
std::string x;
for (int i = 0; i < params; i++) {
AsyncWebParameter* param = request->getParam(i);
if (param->name() == "Axes1") {
x = param->value().c_str();
} else if (param->name() == "Axes2") {
y = param->value().c_str();
}
}
Serial.printf("Axes1: %s, Axes2: %s\n", x.c_str(), y.c_str());
request->send(200, "text/plain", "OK");
}
};

// Note: Here is a function that can be used to handle custom API requests
void grabParams(AsyncWebServerRequest* request) {
int params = request->params();
std::string axes1;
std::string axes2;

for (int i = 0; i < params; i++) {
AsyncWebParameter* param = request->getParam(i);
if (param->name() == "Axes1") {
axes1 = param->value().c_str();
} else if (param->name() == "Axes2") {
axes2 = param->value().c_str();
}
}
int ax1 = atoi(axes1.c_str());
int ax2 = atoi(axes2.c_str());
int ax5 = ax1 - ax2;
int ax6 = ax1 + ax2;
Serial.printf("Axes1: %d, Axes2: %d\n", ax1, ax2);
request->send(200, "text/plain", "OK");
}

// Note: Here are two functions that can be used to handle custom API requests
void printHelloWorld(AsyncWebServerRequest* request) {
Serial.println("Hello World!");
request->send(200, "text/plain", "Hello World!");
}

void blink(AsyncWebServerRequest* request) {
digitalWrite(4, HIGH);
Network_Utilities::my_delay(1); // delay for 1sec
digitalWrite(4, LOW);
Network_Utilities::my_delay(1); // delay for 1sec
request->send(200, "text/plain", "Blink!");
}
/**
* @brief Setup the API Server Instance
* @note The API Server constructor takes 2 parameters:
* @param config The config manager
* @param server The AsyncServer instance
*/
APIServer api(networkManager.configHandler->config, async_server);

void setupServer() {
// This is an example of how to add a custom html file to the web server
Expand All @@ -134,13 +61,6 @@ void setupServer() {
async_server.custom_html_files.emplace_back("/goodbye", "/goodbye.html",
"POST");

api.addAPICommand("blink", blink);
api.addAPICommand("helloWorld", printHelloWorld);
api.addAPICommand("params", grabParams);
api.addAPICommand("paramsClass", [&](AsyncWebServerRequest* request) {
Temp t;
t.grabParams(request);
});
api.begin();
log_d("[SETUP]: API Server Started");
}
Expand All @@ -149,9 +69,6 @@ void setup() {
Serial.begin(115200);
pinMode(4, OUTPUT);
Serial.println("\nHello, EasyNetworkManager!");

//* Optionally register a custom config - this will be saved and loaded
networkManager.configHandler->config.registerUserConfig(&customConfig);
networkManager.begin();

/**
Expand Down Expand Up @@ -182,9 +99,6 @@ void setup() {
}
}
});
// ota.begin();
}

void loop() {
// ota.handleOTAUpdate();
}
void loop() {}
107 changes: 107 additions & 0 deletions NetworkManager/examples/customHandlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <Arduino.h>
#include <EasyNetworkManager.h>

/**
* @brief Setup the EasyNetworkManager Instance
* @note The EasyNetworkManager constructor takes 12 parameters:
* @param config_name The name of the project (used to create the config
* file name)
* @param hostname The hostname for your device on the network(used for
* mDNS, OTA, etc.)
* @param ssid The SSID of the WiFi network to connect to
* @param password The password of the WiFi network to connect to
* @param channel The channel of the WiFi network to connect to
* @param service_name The name of the service
* @param service_instance_name The instance name of the service
* @param service_protocol The protocol of the service
* @param service_description The description of the service
* @param service_port The port of the service
* @param enable_mdns Enable mDNS
* @param enable_adhoc Enable Adhoc
*/
EasyNetworkManager networkManager("easynetwork", MDNS_HOSTNAME, WIFI_SSID,
WIFI_PASSWORD, 1, "_easynetwork", "test",
"_tcp", "_api_port", "80", true, false);

/**
* @brief Setup the AsyncServer Instance
* @note The AsyncServer constructor takes 5 parameters:
* @param port The port to listen on
* @param config The config manager
* @param api_path The path to the API
* @param wifi_manager_path The path to the WiFi Manager
* @param command_path The path to the command handler
*/
AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
"/wifimanager", "/mycommands", "/json");

/**
* @brief Setup the API Server Instance
* @note The API Server constructor takes 2 parameters:
* @param config The config manager
* @param server The AsyncServer instance
*/
APIServer api(networkManager.configHandler->config, async_server);

void setupServer() {
log_d("[SETUP]: Starting API Server");

// Add a custom handler
// This handler will return a custom JSON response when a POST request is
// made to /api/customJson
async_server.server.addHandler(new AsyncCallbackJsonWebHandler(
"/api/customJson",
[&](AsyncWebServerRequest* request, JsonVariant& json) {
JsonDocument doc;
doc["hello"] = "world";
doc["number"] = 42;

AsyncJsonResponse* response = new AsyncJsonResponse();
response->addHeader("EasyNetworkManager", "1.0");
auto root = response->getRoot();
root["deviceData"].set(doc);
response->setLength();
request->send(response);
}));

api.begin();
log_d("[SETUP]: API Server Started");
}

void setup() {
Serial.begin(115200);
pinMode(4, OUTPUT);
Serial.println("\nHello, EasyNetworkManager!");
networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getName()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
}

void loop() {}
5 changes: 1 addition & 4 deletions NetworkManager/examples/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EasyNetworkManager networkManager("easynetwork", MDNS_HOSTNAME, WIFI_SSID,
* @param command_path The path to the command handler
*/
AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
"/wifimanager", "/mycommands");
"/wifimanager", "/mycommands", "/json");

/**
* @brief Setup the API Server Instance
Expand All @@ -43,9 +43,6 @@ AsyncServer_t async_server(80, networkManager.configHandler->config, "/api",
*/
APIServer api(networkManager.configHandler->config, async_server);

// Note: Not required if you are going to use the AsyncOTA feature
// OTA ota(networkManager.configHandler->config);

// Note: Here is a function that can be used to handle custom API requests
void grabParams(AsyncWebServerRequest* request) {
int params = request->params();
Expand Down
Loading

0 comments on commit 8c425c9

Please sign in to comment.