-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIoTNetworkAlarmSketch.ino
82 lines (70 loc) · 1.51 KB
/
IoTNetworkAlarmSketch.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
#include <ESP8266WiFi.h>
#include <Pinger.h>
const char* ssid = "your_ssid";
const char* password = "your_password";
const char* hostIP = "ip_of_host_you_are_monitoring";
#define D5 14
int SpeakerPin = 14;
Pinger pinger;
bool success = false;
void connectToWiFi()
{
Serial.print("\n\nConnecting...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("Connected to router on IP address: ");
Serial.println(WiFi.localIP());
}
void Alarm()
{
digitalWrite(SpeakerPin, HIGH);
delay(100);
digitalWrite(SpeakerPin, LOW);
delay(100);
digitalWrite(SpeakerPin, HIGH);
delay(100);
digitalWrite(SpeakerPin, LOW);
delay(100);
digitalWrite(SpeakerPin, HIGH);
delay(100);
digitalWrite(SpeakerPin, LOW);
delay(100);
digitalWrite(SpeakerPin, HIGH);
delay(100);
digitalWrite(SpeakerPin, LOW);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(SpeakerPin, OUTPUT);
connectToWiFi();
pinger.OnEnd([](const PingerResponse& response)
{
if(response.TotalReceivedResponses > 0)
{
Serial.print("Host online\n");
success = true;
return true;
}
else
{
Serial.print("ERROR: Unable to reach host. Sounding alarm\n");
success = false;
return false;
}
});
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("pinging....\n");
pinger.Ping(hostIP);
if(!success)
{
Alarm();
}
delay(5000);
}