/
lovebox.ino
197 lines (162 loc) Β· 4.91 KB
/
lovebox.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "variables.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>
// Default: 5 seconds
// Configured on startup by service
unsigned long pollingDelay = 5000;
unsigned long lastPollTime = 0;
unsigned long timerDebounceButton = 5000;
unsigned long lastTimeButton = 0;
const int ledPin = 2;
const int buttonPin = 4;
void setup()
{
Serial.begin(9600);
Serial.println();
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
pinMode(ledPin, OUTPUT); // LED
pinMode(buttonPin, INPUT); // Button
// Provision
if (!provision())
{
Serial.println("[-] Provisioning failed");
blinkLEDForever();
}
}
bool provision()
{
WiFiClient client;
HTTPClient http;
http.begin(client, provisionMeAddress.c_str());
// Auth with something like Azure Functions
// http.setAuthorization("user", "password");
// Send HTTP POST request
int httpResponseCode = http.GET();
Serial.print("PROVISION ");
Serial.println(httpResponseCode);
if (httpResponseCode > 0)
{
String payload = http.getString();
JSONVar myObject = JSON.parse(payload);
Serial.print("Provisioning payload: ");
Serial.println(myObject);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("[-] Parsing input failed!");
return false;
}
int pollingIntervalInSeconds = myObject["pollingIntervalInSeconds"];
pollingDelay = pollingIntervalInSeconds * 1000;
Serial.print("pollingDelay: ");
Serial.println(pollingDelay);
}
else
{
Serial.println("[-] Parse error");
}
http.end();
return httpResponseCode == 200;
}
void blinkLEDForever()
{
while (true)
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
}
void loop()
{
//// Optional: Check for button press
//// POSTs message to server, enabling the LED of the peer device :')
// int buttonState = digitalRead(buttonPin);
// if (buttonState == LOW && (millis() - lastTimeButton) > timerDebounceButton)
// {
// Serial.println("[!] Button press detected");
// if (WiFi.status() == WL_CONNECTED)
// {
// WiFiClient client;
// HTTPClient http;
// // Your Domain name with URL path or IP address with path
// http.begin(client, postPeerAddress.c_str());
// // http.setAuthorization("user", "password");
// // Send HTTP POST request
// int httpResponseCode = http.POST("from=" + me);
// Serial.print("POST ");
// Serial.println(httpResponseCode);
// http.end();
// lastTimeButton = millis();
// }
// else
// {
// Serial.println("[-] WiFi Disconnected");
// }
// }
// Get state from server
if ((millis() - lastPollTime) > pollingDelay)
{
Serial.println("[+] Polling server...");
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED)
{
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, getMeAddress.c_str());
// // Auth with something like Azure Functions
// http.setAuthorization("username", "password");
// Send HTTP GET request
int httpResponseCode = http.GET();
Serial.print("GET ");
Serial.println(httpResponseCode);
if (httpResponseCode > 0)
{
String payload = http.getString();
// Parse payload as JSON (get 'enable' bool value)
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("[-] Parsing input failed!");
return;
}
// Get the value of the "enable" element
int enable = myObject["enable"];
if (enable)
{
// Turn on LED
digitalWrite(2, HIGH);
}
else
{
// Turn off LED
digitalWrite(2, LOW);
}
}
else
{
Serial.print("[-] Error code: ");
Serial.println(httpResponseCode);
}
// Free
http.end();
}
else
{
Serial.println("[-] WiFi Disconnected");
}
lastPollTime = millis();
}
}