-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-status-signal.ino
145 lines (108 loc) · 2.85 KB
/
arduino-status-signal.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
ESP8266WebServer server(80);
WiFiClient client;
char* STATUS_URL = "http://fixme.ch/status.json";
int RED = D5;
int YELLOW = D6;
int GREEN = D7;
bool isOpen = false;
void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.print("Setting soft-AP ... ");
WiFi.begin("FIXME-NAT", "hs1337_FIXME");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
Serial.println(WiFi.localIP());
launchServer();
}
void launchServer() {
Serial.println("Lauching web server...");
// Routes
server.on("/", sendOK);
server.on("/open", setOpen);
server.on("/unknown", setUnknown);
server.on("/close", setClose);
server.begin();
Serial.println("Web server ready!");
}
void getRequest(char* url) {
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("Response code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payloadStr = http.getString();
// DEBUG: Serial.println(payloadStr);
int len = payloadStr.length() + 1;
char payload[len];
payloadStr.toCharArray(payload, len);
// JSON Decode
DynamicJsonBuffer jsonBuffer(2000);
// StaticJsonBuffer<2000> jsonBuffer;
JsonObject& status = jsonBuffer.parseObject(payload);
if (!status.success()) {
Serial.println("parseObject() failed");
return;
}
isOpen = status["state"]["open"];
Serial.print("isOpen: ");
Serial.println((isOpen) ? "true" : "false");
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void setOpen() {
Serial.println("Set status: open");
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, HIGH);
digitalWrite(GREEN, LOW);
sendOK();
}
void setUnknown() {
Serial.println("Set status: unknown");
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, HIGH);
sendOK();
}
void setClose() {
Serial.println("Set status: close");
digitalWrite(RED, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(GREEN, HIGH);
sendOK();
}
void sendOK() {
server.send(200, "text/plain", "OK");
}
void updateStatus() {
getRequest(STATUS_URL);
if (isOpen) {
setOpen();
} else {
setClose();
}
}
void loop() {
delay(5000);
Serial.println("--------");
server.handleClient();
updateStatus();
}