#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
extern "C" {
#include "user_interface.h"
}
const char* ssid = "****";
const char* password = "****";
ESP8266WebServer server(80);
const int Relay1 = 12;
const int Relay2 = 13;
int stat = 1;
void handleRoot() {
String s = "<!DOCTYPE HTML>\r\n<html><title>Audio Switch</title><style>body {text-decoration:none;color:#FFF;background-color: #541f1f;max-width: 800px;margin: 0 auto;}a {color:#FFF;text-decoration:none;padding: 5px 5px;}.active {color:red;}.button{text-align: center;margin: 0 auto;padding: 24% 10%;background-color: #192121;width: 67%;font-size: 50px;}</style>";
s += "<body>";
s += " <a href='/stat/1'><div class='";
if(stat == 1){
s += "active ";
}
s += "button' >PC</div></a>";
s += " <a href='/stat/2' ><div class='";
if(stat == 2){
s += "active ";
}
s += "button' >Bluetooth</div></a>";
s += " <a href='/stat/0' ><div class='";
// if(stat == 0){
// s += "active ";
//}
// s += "button'>OFF</div></a>";
s += "</body>";
s += "</html>\n";
server.send(200, "text/html", s);
Serial.print("END SEND");
}
void setup(void){
pinMode(Relay2, OUTPUT);
pinMode(Relay1, OUTPUT);
digitalWrite(Relay2, 1);
digitalWrite(Relay1, 1);
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA);
WiFi.hostname("AudioSwitch");
wifi_set_sleep_type(NONE_SLEEP_T);
wifi_station_set_reconnect_policy(true);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/stat/1", [](){
stat = 1;
digitalWrite(Relay2, 1);
digitalWrite(Relay1, 1);
handleRoot();
});
server.on("/stat/2", [](){
stat = 2;
digitalWrite(Relay2, 0);
digitalWrite(Relay1, 0);
handleRoot();
});
server.on("/hidden/2", [](){
stat = 2;
digitalWrite(Relay2, 0);
digitalWrite(Relay1, 0);
});
server.on("/hidden/1", [](){
stat = 1;
digitalWrite(Relay2, 1);
digitalWrite(Relay1, 1);
});
server.on("/status", [](){
server.send(200, "text/plain", String(stat));
});
server.onNotFound(handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}