-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger_test.ino
64 lines (53 loc) · 1.75 KB
/
trigger_test.ino
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
#include "ESP8266WiFi.h"
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include "ArduinoJson.h"
#include "AsyncJson.h"
const char* ssid = "ssid";
const char* password = "pass";
char* iot_kit_key;
int device_key;
int run_time_mins;
AsyncWebServer server(80);
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println(WiFi.localIP());
server.on("/trigger_command", HTTP_PUT, [](AsyncWebServerRequest *request){
//nothing and dont remove it
}, NULL, [](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((const char*)data);
if (root.success()) {
Serial.println("");
if (root.containsKey("iot_kit_key")) {
Serial.print("iot_kit_key: ");
Serial.println(root["iot_kit_key"].asString());
}
if (root.containsKey("device_key")) {
Serial.print("device_key: ");
Serial.println(root["device_key"].asString());
}
if (root.containsKey("run_time_mins")) {
Serial.print("run_time_mins: ");
Serial.println(root["run_time_mins"].asString());
}
/*JSON Response Stream*/
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["command_executed"] = true;
root["message"] = "Command executed successfully";
root.printTo(*response);
request->send(response);
} else {
request->send(404, "text/plain", "Error!!");
}
});
server.begin();
}
void loop(){}