On Air 1.0
This commit is contained in:
+14
-26
@@ -2,8 +2,7 @@
|
|||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
|
|
||||||
const int optocoupler = D7;
|
const int optocoupler = D2;
|
||||||
const int togglePin = D3;
|
|
||||||
|
|
||||||
const char* ssid = "LeWe";
|
const char* ssid = "LeWe";
|
||||||
const char* password = "5275274365464843";
|
const char* password = "5275274365464843";
|
||||||
@@ -11,7 +10,6 @@ const char* password = "5275274365464843";
|
|||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
|
|
||||||
int lightStatus = 0; // 0 off, 1 on, 2 pulse
|
int lightStatus = 0; // 0 off, 1 on, 2 pulse
|
||||||
int toggleStatus = 0;
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@@ -23,9 +21,6 @@ void setup() {
|
|||||||
pinMode(optocoupler, OUTPUT);
|
pinMode(optocoupler, OUTPUT);
|
||||||
digitalWrite(optocoupler, LOW);
|
digitalWrite(optocoupler, LOW);
|
||||||
|
|
||||||
pinMode(togglePin, OUTPUT);
|
|
||||||
digitalWrite(togglePin, LOW);
|
|
||||||
|
|
||||||
// Connect WiFi
|
// Connect WiFi
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@@ -45,7 +40,6 @@ void setup() {
|
|||||||
server.on("/click", handle_click);
|
server.on("/click", handle_click);
|
||||||
server.on("/light", handle_light);
|
server.on("/light", handle_light);
|
||||||
server.onNotFound(handle_NotFound);
|
server.onNotFound(handle_NotFound);
|
||||||
server.on("/toggle", handle_toggle);
|
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
@@ -94,6 +88,13 @@ void handle_click() {
|
|||||||
server.send(200, "text/plain", response);
|
server.send(200, "text/plain", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String getButtonStatus(int button) {
|
||||||
|
if (lightStatus == button) {
|
||||||
|
return "button_on";
|
||||||
|
}
|
||||||
|
return "button_off";
|
||||||
|
}
|
||||||
|
|
||||||
void handle_light(){
|
void handle_light(){
|
||||||
if (server.hasArg("status")) {
|
if (server.hasArg("status")) {
|
||||||
|
|
||||||
@@ -111,14 +112,14 @@ void handle_light(){
|
|||||||
String response = "<!DOCTYPE html><html>";
|
String response = "<!DOCTYPE html><html>";
|
||||||
response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
|
response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
|
||||||
response += "<link rel=\"icon\" href=\"data:,\">";
|
response += "<link rel=\"icon\" href=\"data:,\">";
|
||||||
response += "<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; } ";
|
response += "<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; } \n";
|
||||||
response += ".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
|
response += ".button_on { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} \n";
|
||||||
|
response += ".button_off { background-color: #F0F0F0; border: none; color: darkgray; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} \n";
|
||||||
response += "</style></head>";
|
response += "</style></head>";
|
||||||
response += "<body><h1>On Air</h1>";
|
response += "<body><h1>On Air</h1>";
|
||||||
response += "<p><a href=\"/light?status=0\"><button class=\"button\">Off</button></a></p>";
|
response += "<p><a href=\"/light?status=0\"><button class=\"" + getButtonStatus(0) + "\">Off</button></a></p>";
|
||||||
response += "<p><a href=\"/light?status=1\"><button class=\"button\">On</button></a></p>";
|
response += "<p><a href=\"/light?status=1\"><button class=\"" + getButtonStatus(1) + "\">On</button></a></p>";
|
||||||
response += "<p><a href=\"/light?status=2\"><button class=\"button\">Pulse</button></a></p>";
|
response += "<p><a href=\"/light?status=2\"><button class=\"" + getButtonStatus(2) + "\">Pulse</button></a></p>";
|
||||||
response += "<p>" + String(lightStatus) + "</p>";
|
|
||||||
response += "</body></html>";
|
response += "</body></html>";
|
||||||
|
|
||||||
server.send(200, "text/html", response);
|
server.send(200, "text/html", response);
|
||||||
@@ -127,16 +128,3 @@ void handle_light(){
|
|||||||
void handle_NotFound(){
|
void handle_NotFound(){
|
||||||
server.send(404, "text/plain", "Not found");
|
server.send(404, "text/plain", "Not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_toggle() {
|
|
||||||
if (toggleStatus == 0) {
|
|
||||||
toggleStatus = 1;
|
|
||||||
digitalWrite(togglePin, HIGH);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
toggleStatus = 0;
|
|
||||||
digitalWrite(togglePin, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
server.send(200, "text/html", "D3 is set to " + String(toggleStatus));
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user