Files
microcontroller/D1 mini/On_Air/On_Air.ino
T
2022-02-06 12:26:05 +01:00

143 lines
3.4 KiB
Arduino

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const int optocoupler = D7;
const int togglePin = D3;
const char* ssid = "LeWe";
const char* password = "5275274365464843";
ESP8266WebServer server(80);
int lightStatus = 0; // 0 off, 1 on, 2 pulse
int toggleStatus = 0;
void setup() {
Serial.begin(9600);
pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output
digitalWrite(BUILTIN_LED, HIGH);
pinMode(optocoupler, OUTPUT);
digitalWrite(optocoupler, LOW);
pinMode(togglePin, OUTPUT);
digitalWrite(togglePin, LOW);
// Connect WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname("OnAir");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/click", handle_click);
server.on("/light", handle_light);
server.onNotFound(handle_NotFound);
server.on("/toggle", handle_toggle);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void cycleStatus() {
digitalWrite(optocoupler, HIGH);
delay(350);
digitalWrite(optocoupler, LOW);
delay(150);
if (lightStatus < 2) {
lightStatus++;
}
else {
lightStatus = 0;
}
Serial.print("click --> ");
Serial.println(lightStatus);
}
void handle_click() {
if (server.hasArg("status")) {
int targetStatus = server.arg("status").toInt();
Serial.print("targetStatus: ");
Serial.println(targetStatus);
if (targetStatus >= 0 && targetStatus <= 2) {
while (lightStatus != targetStatus) {
cycleStatus();
}
}
}
else {
cycleStatus();
}
String response = "Status set to ";
response.concat(lightStatus);
server.send(200, "text/plain", response);
}
void handle_light(){
if (server.hasArg("status")) {
int targetStatus = server.arg("status").toInt();
Serial.print("targetStatus: ");
Serial.println(targetStatus);
if (targetStatus >= 0 && targetStatus <= 2) {
while (lightStatus != targetStatus) {
cycleStatus();
}
}
}
String response = "<!DOCTYPE html><html>";
response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
response += "<link rel=\"icon\" href=\"data:,\">";
response += "<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; } ";
response += ".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
response += "</style></head>";
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=1\"><button class=\"button\">On</button></a></p>";
response += "<p><a href=\"/light?status=2\"><button class=\"button\">Pulse</button></a></p>";
response += "<p>" + String(lightStatus) + "</p>";
response += "</body></html>";
server.send(200, "text/html", response);
}
void handle_NotFound(){
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));
}