96 lines
1.7 KiB
Arduino
96 lines
1.7 KiB
Arduino
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WebServer.h>
|
|
|
|
|
|
const int optocoupler = D7;
|
|
const char* ssid = "LeWe";
|
|
const char* password = "5275274365464843";
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
int lightStatus = 0; // 0 off, 1 pulse, 2 on
|
|
|
|
//const int inputPin = D3;
|
|
//int val = 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(inputPin, INPUT);
|
|
|
|
// 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("/light", handle_OnConnect);
|
|
server.onNotFound(handle_NotFound);
|
|
|
|
server.begin();
|
|
Serial.println("HTTP server started");
|
|
}
|
|
|
|
void loop() {
|
|
server.handleClient();
|
|
|
|
/*
|
|
val = digitalRead(inputPin);
|
|
digitalWrite(BUILTIN_LED, val);
|
|
*/
|
|
}
|
|
|
|
void cycleStatus() {
|
|
digitalWrite(optocoupler, HIGH);
|
|
delay(250);
|
|
digitalWrite(optocoupler, LOW);
|
|
delay(50);
|
|
|
|
if (lightStatus < 2) {
|
|
lightStatus++;
|
|
}
|
|
else {
|
|
lightStatus = 0;
|
|
}
|
|
|
|
Serial.println("click");
|
|
}
|
|
|
|
void handle_OnConnect() {
|
|
if (server.hasArg("status")) {
|
|
|
|
int targetStatus = server.arg("status").toInt();
|
|
|
|
if (targetStatus >= 0 && targetStatus <= 2) {
|
|
while (lightStatus != targetStatus) {
|
|
cycleStatus();
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
cycleStatus();
|
|
}
|
|
|
|
server.send(200, "text/plain", "status set to " + lightStatus);
|
|
}
|
|
void handle_NotFound(){
|
|
server.send(404, "text/plain", "Not found");
|
|
}
|