-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update NetworkManager configuration and API handlers
- Loading branch information
1 parent
797ab15
commit 8c425c9
Showing
10 changed files
with
277 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.