Skip to content

Commit

Permalink
feat: total project refactor
Browse files Browse the repository at this point in the history
- better organizing of the library.

BREAKING CHANGES

- simplify the api A LOT
- setup the state machine to follow the observer pattern for easier use
- create a wrapper for updating the state
- update examples
  • Loading branch information
ZanzyTHEbar committed Dec 21, 2023
1 parent b0f5839 commit 0f70bec
Show file tree
Hide file tree
Showing 26 changed files with 715 additions and 693 deletions.
3 changes: 2 additions & 1 deletion EasyNetworkManager.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"variant": "cpp"
}
}
}
187 changes: 83 additions & 104 deletions NetworkManager/examples/captivePortal.cpp
Original file line number Diff line number Diff line change
@@ -1,82 +1,55 @@
#include <Arduino.h>

// Note: Here is a list of all the library header files - required ones are
// marked
// with an asterisk (*)

//! This is a Captive Portal Example. Please see the customHTML.cpp for an
//! example of how to implement custom HTML files to use instead of the default
//! wifimanager.

//! Optional header files
#include <DNSServer.h> // (*) used for captive portal
#include <network/mdns/mdns_manager.hpp>
#include <network/ota/basic_ota.hpp>
#include <utilities/network_utilities.hpp> // various network utilities
// (unique_ptr) to create unique objects
// #include <utilities/Observer.hpp>
// #include <utilities/api_utilities.hpp>
// #include <utilities/enuminheritance.hpp> // used for extending enums with new
// values
// #include <utilities/makeunique.hpp> // used with smart pointers (unique_ptr)
// to create unique objects
// #include <utilities/helpers.hpp> // various helper functions

//! Required header files
#include <EasyNetworkManager.h> // (*)

// Note: The Project Config Manager is used to store and retrieve the
// configuration
// data ? The config manager constructor takes two (optional) parameters: ? 1.
// The name of the project (used to create the config file name) ? 2. The
// hostname for your device on the network(used for mDNS, OTA, etc.)
ConfigHandler configHandler("easynetwork", MDNS_HOSTNAME);

// Note: The WiFi Handler is used to manage the WiFi connection
// Note: The WiFi Handler constructor takes four parameters:
// Note: 1. A pointer to the config manager
// Note: 2. A pointer to the WiFi State Manager
// Note: 3. The SSID of the WiFi network to connect to
// Note: 4. The password of the WiFi network to connect to
WiFiHandler network(configHandler.config, WIFI_SSID, WIFI_PASSWORD, 1);
#include <EasyNetworkManager.h>
//! Only needed for BLOCKING OTA. AsyncOTA is recommended and builtin
// #include <network/ota/basic_ota.hpp>

/**
* @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);

DNSServer dnsServer;

// Note: The API Server is used to create a web server that can be used to send
// commands to the device ? The API Server constructor takes five parameters:
// Note: 1. The port number to use for the web server
// Note: 2. A pointer to the config manager
// Note: 3. The root path for the API
// Note: 4. The path for the WiFi Manager html page
// Note: 5. The root path for the user commands for the API
// Note: This file example will create a web server on port 80, with the root
// path:
// http://easynetwork.local/api/mycommands/command/helloWorld
// http://easynetwork.local/api/mycommands/command/blink
// http://easynetwork.local/api/mycommands/command/params?Axes1=1&Axes2=2

AsyncServer_t async_server(80, configHandler.config, "/api", "/wifimanager",
"/mycommands");

APIServer server(configHandler.config, async_server);
/**
* @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");

/**
* @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);

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

// Note: The mDNS Manager is used to create a mDNS service for the device
// Note: The mDNS Manager constructor takes seven parameters:
// Note: 1. A pointer to the mDNS State Manager
// Note: 2. A pointer to the config manager
// Note: 3. The service name
// Note: 4. The service instance name
// Note: 5. The service protocol
// Note: 6. The service description
// Note: 7. The service port

//! service name and service protocol have to be
//! lowercase and begin with an underscore
MDNSHandler mDNS(configHandler.config, "_easynetwork", "test", "_tcp",
"_api_port", "80");
// OTA ota(networkManager.configHandler->config);

class CustomConfig : public CustomConfigInterface {
void save() override {
Expand Down Expand Up @@ -158,21 +131,15 @@ 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");

// Enabled the captive portal handler
server.setupCaptivePortal(true);

// Create a custom REST API for our application
server.addAPICommand("blink", blink);
server.addAPICommand("helloWorld", printHelloWorld);
server.addAPICommand("params", grabParams);
server.addAPICommand("paramsClass", [&](AsyncWebServerRequest* request) {
api.setupCaptivePortal(true);
api.addAPICommand("blink", blink);
api.addAPICommand("helloWorld", printHelloWorld);
api.addAPICommand("params", grabParams);
api.addAPICommand("paramsClass", [&](AsyncWebServerRequest* request) {
Temp t;
t.grabParams(request);
});

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

Expand All @@ -181,31 +148,43 @@ void setup() {
pinMode(4, OUTPUT);
Serial.println("\nHello, EasyNetworkManager!");

configHandler.config.attach(configHandler);
//* Optionally register a custom config - this will be saved and loaded
configHandler.config.registerUserConfig(&customConfig);
configHandler.config.attach(network);
configHandler.config.attach(
mDNS); // attach the config manager to the mdns object - this will
// update the config when mdns hostname changes

configHandler.begin(); // load the config from flash

//* Begin the network manager
network.begin(); // setup wifi connection

//* Begin the dns server for the captive portal
//* Port, domain name - wildcard to capture all
networkManager.configHandler->config.registerUserConfig(&customConfig);
networkManager.begin();
dnsServer.start(53, "*", WiFi.softAPIP());

mDNS.begin(); // start mDNS service (optional)

setupServer(); // setup the API server

ota.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;
}
}
});
// ota.begin();
}

void loop() {
dnsServer.processNextRequest();
ota.handleOTAUpdate();
// ota.handleOTAUpdate();
}
Loading

0 comments on commit 0f70bec

Please sign in to comment.