-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSuperDuck.ino
118 lines (92 loc) · 2.44 KB
/
SuperDuck.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
#include <WiFi.h>
#include <WebServer.h>
#include <pthread.h>
// Configure your WiFi here:
const char* ssid = ""; // Enter SSID here
const char* password = ""; //Enter Password here
// declare an object of WebServer library
WebServer server(80);
// Variable to store the HTTP request
String header;
// Assign variables
const byte ledPins[] = {4, 5, 21, 22, 13, 27, 33};
unsigned long timer = 200; // the higher the number, the slower the timing.
// Set up a thread
pthread_t threads[1];
bool busy = false;
void setup() {
Serial.begin(115200);
unsigned long startmillis = millis();
while ((millis() - startmillis) < 100) {
yield();
}
//initialize ledPins array as outputs
for (int i = 0; i< (sizeof(ledPins) / sizeof(ledPins[0])); i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(BUILTIN_LED, OUTPUT);
}
//we start by connecting to a WiFi network
Serial.print("Connecting to "); Serial.println(ssid);
//connect WiFi to local network
WiFi.begin(ssid, password);
//check WiFi status
while (WiFi.status() != WL_CONNECTED) {
unsigned long startmillis = millis();
while ((millis() - startmillis) < 200) {
yield();
}
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
yield();
server.on("/", handle_OnConnect);
server.on("/alert", handle_alert);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
server.send(200);
}
void handle_alert() {
if (not busy) {
pthread_create(&threads[0], NULL, alert, (void *)0);
}
}
void *alert(void *threadid) {
Serial.println("Alerting");
busy = true;
server.send(200);
int repeat = 0;
unsigned long startmillis;
while (repeat < 20) {
for (int i = 0; i< (sizeof(ledPins) / sizeof(ledPins[0])); i++) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(BUILTIN_LED, HIGH);
yield();
}
startmillis = millis();
while ((millis() - startmillis) < timer) {
yield();
}
for (int i = 0; i< (sizeof(ledPins) / sizeof(ledPins[0])); i++) {
digitalWrite(ledPins[i], LOW);
digitalWrite(BUILTIN_LED, LOW);
yield();
}
startmillis = millis();
while ((millis() - startmillis) < timer) {
yield();
}
repeat++;
busy = false;
}
}
void handle_NotFound(){
server.send(404);
}