-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhodor.ino
More file actions
90 lines (83 loc) · 3.12 KB
/
hodor.ino
File metadata and controls
90 lines (83 loc) · 3.12 KB
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
#include <WiFi.h>
#include "secrets.h"
WiFiServer server(80);
String header;
const int DOOR_PIN = D10;
// How long the latch signal will be send to the controller in ms.
const long LATCH_TIME = 100;
// Request timeout in ms.
const long TIMEOUT = 2000;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
void setup() {
Serial.begin(115200);
pinMode(DOOR_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
initWiFi();
server.begin();
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= TIMEOUT) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /" + secretKey) >= 0) {
Serial.println("HODOR!");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(DOOR_PIN, HIGH);
delay(LATCH_TIME);
digitalWrite(DOOR_PIN, LOW);
digitalWrite(LED_BUILTIN, LOW);
}
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(" Done!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
}