#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "ssid";
const char* password = "pass";
ESP8266WebServer server(80);
const int led = D6;
const String content = "<html>\
<body>\
<button onmousedown='send(1)' onmouseup='send(0)' onmouseout='send(0)' >gpioOn</button>\
<script>\
function send(val) {\
var xhr = new XMLHttpRequest();\
xhr.open('POST', '/gpioOn?val='+val, true);\
xhr.send();\
}\
</script>\
</body>\
</html>\
";
void handleRoot() {
server.send(200, "text/html", content);
}
void gpioOn() {
int val = server.arg("val").toInt();
digitalWrite(led,val );
server.send(200);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
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("/gpioOn", gpioOn);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}