-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
104 lines (89 loc) · 2.68 KB
/
main.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
#include "M5StickCPlus.h"
#include <HTTPClient.h>
#include <WiFi.h>
#include <ArduinoJson.h> // Needs to be installed via Arduino library manager
// Configuration
const char* SSID = "<YOUR_WIFI_SSID>";
const char* PASSWORD = "<YOUR_WIFI_PASSKEY>";
const char* WEBHOOK_URL = "https://<VPS_IP>:<VPS_PORT>/<TELEGRAM_BOT_TOKEN>/";
const char* LOCATION_NAME = "Bedroom Door";
const int POLLING_PERIOD = 1000; // Polling period in milliseconds between sensor updates 1000 = 1 second
// Function to setup the device on the wireless network
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
M5.Lcd.println();
M5.Lcd.print("Connecting to ");
M5.Lcd.println(SSID);
//WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
// Print "." until the network is connected for better UX
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
M5.Lcd.print(".");
}
// Print the local IP of the device once connected to the network
M5.Lcd.println("");
M5.Lcd.println("WiFi connected");
M5.Lcd.println("IP address: ");
M5.Lcd.println(WiFi.localIP());
}
bool checkWifiConnection() {
if (WiFi.status() != WL_CONNECTED) {
M5.Lcd.println("Reconnecting to WiFi...");
setup_wifi();
return WiFi.status() == WL_CONNECTED;
}
return true;
}
void sendPostRequest(const char* sensorStatus) {
if (checkWifiConnection()) {
HTTPClient http;
http.begin(WEBHOOK_URL); // Webhook URL
http.addHeader("Content-Type", "application/json");
StaticJsonDocument<200> doc;
doc["action"] = "sensor_event";
doc["timestamp"] = (int)time(NULL); // Current timestamp
doc["location"] = LOCATION_NAME;
doc["sensor_status"] = sensorStatus;
String payload;
serializeJson(doc, payload);
int httpResponseCode = http.POST(payload);
// Print the response on the LCD
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
if(httpResponseCode > 0) {
String response = http.getString();
M5.Lcd.println(response);
} else {
M5.Lcd.println("POST Error: " + String(httpResponseCode));
}
http.end();
} else {
M5.Lcd.println("Failed to Connect WiFi");
}
}
void setup() {
M5.begin();
pinMode(26, INPUT_PULLUP);
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(2);
setup_wifi();
}
void loop() {
static int lastState = -1; // To store the last state
int sensorState = digitalRead(26);
if (sensorState != lastState) {
lastState = sensorState;
if (sensorState == HIGH) {
M5.Lcd.println("Door Open");
sendPostRequest("OPEN");
} else {
M5.Lcd.println("Door Closed");
sendPostRequest("CLOSED");
}
}
delay(POLLING_PERIOD);
}