-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32-Bell-Lock.ino
220 lines (191 loc) · 6.6 KB
/
ESP32-Bell-Lock.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
*
*
*/
#include <WiFi.h>
#include "time.h"
#include <HTTPClient.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
// Global definitions
//Wifi Definitions
const char* ssid = "****";
const char* password = "****";
IPAddress local_IP(*, *, *, *); //Set fixed local IP
IPAddress gateway(*, *, *, *); //Router
IPAddress subnet(*, *, *, *);
IPAddress primaryDNS(*, *, *, *);
IPAddress secondaryDNS(*, *, *, *);
//Sonos server data
const char* SonosHost = "*.*.*.*"; //Sonos Server IP
const uint16_t SonosPort = *; //Sonos service port
#define ROOM_CLIP "/room/clip/clip1.mp3" //optional clips see Sonos API
#define HOME_CLIP "/clipall/clip2.mp3" //another optional clips see Sonos API
const String SoundUri = ROOM_CLIP; //Selected clip to play
//Local Server for Gate Open requests
AsyncWebServer server(80);
#define GATE_OPEN_URI "/open"
//Logger Remote Server
const char* LoggerHost = "*.*.*.*"; //Sonos Server IP
const uint16_t LoggerPort = *; //Sonos service port
const String LogUri = "/Door-Bell/log"; //Simple logger service definition
//NTP server and time zone settings
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7200;
const int daylightOffset_sec = 3600;
//Digital pins defintions
//Bell digital pin
#define BELL_PIN 2 //Bell Digital Pin
byte BellPin = BELL_PIN;
#define BELL_DELAY 5000 //Bell delay between presses
unsigned long bell_time = 0; //time counter to set delay
bool Bell = false; //Bell flag
//Gate open digital Pin
#define OPEN_PIN 4 //Gate OPen pin
byte OpenPin = OPEN_PIN;
bool OpenGate = false; //Gate open flag
//Generic utils
//Wifi event manager
void WiFiEvent(WiFiEvent_t event)
{
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection...Restart ESP");
logit("WiFi lost connection...Restart ESP");
ESP.restart();
break;
}
}
//Simple wrapper for HTTP Requests (get/post)
void HttpRequest(String HttpServer,uint16_t ServerPort,String ReqUri,String PostData = "") { //If post data is "" its a get request
bool httpget = (PostData == "");
HTTPClient http;
http.begin(HttpServer,ServerPort,ReqUri);
int httpCode;
if (httpget) {
httpCode = http.GET(); //Make the request
} else {
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
httpCode = http.POST(String(PostData) + "\r\n"); //Send the actual POST request
}
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
Serial.println(httpCode);
}
http.end(); //Free the resources
}
//Get Local Time Time
String LocalTime()
{
struct tm timeinfo;
char lt[20];
int n;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return "Time Error";
}
strftime(lt, 20, "%F %H:%M:%S",&timeinfo);
return String(lt);
//Serial.println(&timeinfo, "%F %H:%M:%S");
}
//Log data to logger server
void logit(String message) {
HttpRequest(LoggerHost,LoggerPort,LogUri,LocalTime() + "-" + message);
}
//Application utils
// Event handler fo Bell press detection
void BellHandler()
{
//Resong for only raising flag it to keep handler short and loose all unhandled conncutive interrupts
noInterrupts(); //block interrupts and only raise global flag here
Bell =true;
interrupts();
}
//Wrapper to Sonos play command
void play_sonos(String play){
HttpRequest(SonosHost,SonosPort,play,"");
}
//Wrapper to open gate command
void open_gate()
{
digitalWrite(OPEN_PIN, HIGH); // Set gate relay pin HIGH
//Wait for 0.5 sec (we dont mind blocking the loop since no need to handle bell events while we are openenig the gate
delay(500);
digitalWrite(OPEN_PIN, LOW);
OpenGate = false;
}
void bell_pressed() //manage bell pressed
{
if(millis() - bell_time > BELL_DELAY) { //check if we passed allowed delay between bells
play_sonos(SoundUri); //Send Sonos play request
bell_time = millis(); //save the time
} else { // the request is not valid delay not passed
Serial.println("Bell delay not passed");
}
noInterrupts();
Bell = false; //reset ball event flag
interrupts();
}
void setup()
{
//initialize serial terminal
Serial.begin(115200);
// Connect to a WiFi network
//Configure wifi STA
if (!WiFi.config(local_IP, gateway, subnet,primaryDNS,secondaryDNS)) {
Serial.println("STA Failed to configure");
}
WiFi.onEvent(WiFiEvent); //Define event handler
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //Start wifi STA
while (WiFi.status() != WL_CONNECTED) { //1 second delay loop until connect
delay(1000);
Serial.print(".");
}
//Initializing digital pins
pinMode(BellPin, INPUT_PULLUP);
pinMode(OpenPin, OUTPUT);
//NTP time configuration (get NTP time offsetted to local time zone)
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
//Define Server response event
server.on(GATE_OPEN_URI, HTTP_GET, [](AsyncWebServerRequest *request){ //We set the URI to "/open" but can be any
request->send(200, "text/plain", "Ok Open Relay Sent...."); //first respond
OpenGate = true; //raise flag
});
//Start local server to handle open gate requests
Serial.println("Starting local server on port 80");
Serial.println("Send http://Local Server IP/open to open gate");
Serial.println("Go To http://Logger Server IP:PORT to see logs");
server.begin();
// set bell pin interrupt handler
attachInterrupt(digitalPinToInterrupt(BellPin), BellHandler, CHANGE);
}
void loop()
{
//Test if Bell pressed
if (Bell) {
Serial.println("Bell Pressed...Playing Sonos Clip");
logit("Bell Pressed...Play Sonos Clip");
bell_pressed();
}
//Test is OPen Gate reques
if (OpenGate) {
Serial.println("Open-Gate Request...Activating Gate Relay....");
logit("Open-Gate Request...Activating Gate Relay....");
open_gate();
}
}