-
Notifications
You must be signed in to change notification settings - Fork 5
/
wifi_power_button.ino
196 lines (156 loc) · 4.32 KB
/
wifi_power_button.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "wifi_power_button.h"
/////////////////////////////////////////////////////////////////////////////////
// Variable setup
/////////////////////////////////////////////////////////////////////////////////
// Variables defined in wifi_power_button.h
const char* ssid = USRSSID;
const char* password = USRPSK;
const int pin = POWER_PIN;
const int led = LED_PIN;
const int led_brightness = LED_BRIGHTNESS;
// Change these values if using static IP for project (see wifi_power_button.h)
#ifdef USE_STATIC_IP
const IPAddress ip(192, 168, 1, 110);
const IPAddress gateway(192, 168, 1, 1);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress dns(192, 168, 1, 1);
#endif
// Set LED to be on or off at start (default is off)
bool status = false;
// Create server instance
ESP8266WebServer server(80);
/////////////////////////////////////////////////////////////////////////////////
// Main functions
/////////////////////////////////////////////////////////////////////////////////
//
// Setup
//
void setup(){
// Prepare GPIO pins
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
pinMode(led, OUTPUT);
analogWrite(led, 1024); // off initially
// Start serial monitor
Serial.begin(115200);
delay(10);
Serial.println("===========================");
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Configure wifi and optionally the static IP
#ifdef USE_STATIC_IP
WiFi.config(ip, gateway, subnet, dns);
#endif
WiFi.begin(ssid, password);
// Connect to wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
// Setup mDNS if enabled
#ifdef USE_MDNS
if (MDNS.begin(MDNSNAME)) {
Serial.println("MDNS responder started");
Serial.print("Server accessable at: http://");
Serial.print(MDNSNAME);
Serial.println(".local");
}
#endif
// Setup server
server.on("/", handleRoot);
server.on("/power", handlePowerButtonPress);
server.on("/led", handleLEDButtonPress);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
Serial.println();
Serial.println("===========================");
}
//
// Loop
//
void loop(void) {
server.handleClient();
#ifdef USE_MDNS
MDNS.update();
#endif
}
/////////////////////////////////////////////////////////////////////////////////
// Server functions
/////////////////////////////////////////////////////////////////////////////////
//
// Root handler
//
void handleRoot() {
String s = MAIN_page;
server.send(200, "text/html", s); // use html defined in index.h
}
//
// Power button handler
//
void handlePowerButtonPress() {
triggerPower();
blinkLED();
server.sendHeader("Location", "/", true); // Redirect home
server.send(302, "text/plane","");
}
//
// LED button handler
//
void handleLEDButtonPress() {
toggleLED();
server.sendHeader("Location", "/", true); // Redirect home
server.send(302, "text/plane","");
}
//
// 404 handler
//
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
/////////////////////////////////////////////////////////////////////////////////
// Helper functions
/////////////////////////////////////////////////////////////////////////////////
//
// Pulls the pin to ground briefly to simulate a button push
//
void triggerPower() {
Serial.println("POWER BUTTON HIT\n");
digitalWrite(pin, LOW);
delay(150);
digitalWrite(pin, HIGH);
}
//
// Blinks the LED off or on depending on led status
//
void blinkLED() {
analogWrite(led, (status ? 1024 : led_brightness));
delay(150);
analogWrite(led, (status ? led_brightness : 1024));
}
//
// Turns LED on or off
//
void toggleLED() {
analogWrite(led, (status ? 1024 : led_brightness)); // OFF : ON
status = !status;
}