-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectric-checker.ino
71 lines (59 loc) · 1.38 KB
/
electric-checker.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char *ssid = "wifi-name";
const char *password = "wifi-password";
const char *serverUrl = "backend-url";
void setup()
{
Serial.begin(9600);
connectToWiFi();
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{ // if wifi connection exists
sendPostRequest();
delay(14400000); // delay 4 hours
}
else
{
Serial.println("There is no WiFi connection, trying to connect again...");
connectToWiFi();
}
}
void connectToWiFi()
{
WiFi.begin(ssid, password);
Serial.println("Connecting to the WiFi...");
while (WiFi.status() != WL_CONNECTED)
{ // Try until connect
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi!");
}
void sendPostRequest()
{
if (WiFi.status() == WL_CONNECTED)
{ // If WiFi connection exists
HTTPClient http;
WiFiClient client;
http.begin(client, serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("accept", "application/json");
String postData = "{\"field\": \"value\"}"; // Payload
int httpCode = http.POST(postData);
if (httpCode > 0)
{ // If request succeed
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else
{
Serial.println("Request Operation Failed!");
}
http.end();
}
}