-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialRelay.ino
65 lines (59 loc) · 1.49 KB
/
SerialRelay.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
65
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>
#include <ESPmDNS.h>
#include "SerialRelay.hpp"
WebServer server(WEB_PORT);
void handlePostRequest() {
if (server.method() == HTTP_POST) {
StaticJsonDocument<200> jsonDoc;
DeserializationError error = deserializeJson(jsonDoc, server.arg("plain"));
if (error) {
server.send(400, "application/json", "{\"error\":\"Invalid JSON\"}");
return;
}
int cmdDelay = jsonDoc["delay"] | 0;
String ending = jsonDoc["ending"] | "";
JsonArray commands = jsonDoc["commands"];
if (!commands.isNull()) {
for (String cmd : commands) {
Serial.print(cmd);
if(ending.length() > 0){
Serial.print(ending);
}
Serial.flush();
if(cmdDelay > 0){
delay(cmdDelay);
}
}
}
server.send(200, "text/plain", "Complete");
} else {
server.send(405, "text/plain", "Method Not Allowed");
}
}
void setup() {
Serial.begin(BAUD_RATE);
initWiFi();
server.on("/", handlePostRequest);
server.begin();
}
void loop() {
server.handleClient();
delay(100);
}
void initWiFi() {
WiFi.disconnect(true, true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(hostname);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.println(WiFi.localIP());
MDNS.begin(hostname);
}