-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomHandlers.cpp
80 lines (70 loc) · 2.86 KB
/
customHandlers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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();
setupServer();
}
void loop() {}