-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ino
101 lines (83 loc) · 1.87 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
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "secrets.h"
int soundDetectedPin = 14;
int soundDetectedVal = HIGH;
boolean bAlarm = false;
unsigned long lastSoundDetectTime;
int soundAlarmTime = 25;
void setup()
{
Serial.begin(115200);
pinMode(soundDetectedPin, INPUT);
wifiStart();
Serial.println("=== START");
}
void loop()
{
digitalRead(soundDetectedPin) == LOW ? maybeStartAlarm() : maybeStopAlarm();
}
void maybeStopAlarm()
{
unsigned long diff = millis() - lastSoundDetectTime;
if ((diff > soundAlarmTime) && bAlarm)
{
Serial.println("=== EXIT ALARM STATE");
bAlarm = false;
}
}
void maybeStartAlarm()
{
lastSoundDetectTime = millis();
if (!bAlarm)
{
Serial.println("=== ENTER ALARM STATE");
sendHttpAlert();
bAlarm = true;
}
}
void wifiStart()
{
delay(10);
Serial.print("Connecting to ");
Serial.print(WOOF_WIFI_SSID);
#if defined(WOOF_WIFI_PASSWORD)
WiFi.begin(WOOF_WIFI_SSID, WOOF_WIFI_PASSWORD);
#else
Serial.println("...with no password.");
WiFi.begin(WOOF_WIFI_SSID);
#endif
while (WiFi.status() != WL_CONNECTED)
{ // Wait for the Wi-Fi to connect
delay(500);
Serial.print('.');
}
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}
void sendHttpAlert()
{
if (WiFi.status() == WL_CONNECTED)
{ //Check WiFi connection status
HTTPClient http;
http.begin(WOOF_POST_URL);
http.addHeader("Content-Type", "text/plain");
int httpResponseCode = http.POST("Woof!");
if (httpResponseCode > 0)
{
Serial.println(http.getString()); //Show response in terminal (debug)
}
else
{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi FAILURE");
}
}