Skip to content

Instantly share code, notes, and snippets.

@ayancey
Last active Apr 13, 2018
Embed
What would you like to do?
#include <ESP8266WiFi.h>
#include <stdlib.h>
const char* ssid = "MAAQ";
const char* password = "";
WiFiServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D3, INPUT_PULLUP);
// Attempt to connect to WiFi, blink while connecting, solid while connected
WiFi.hostname("COFFEEMAKER");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
}
digitalWrite(LED_BUILTIN, LOW);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("");
if (request.indexOf("GET /led HTTP/1.1") != -1) {
client.println("{");
if (digitalRead(D3) == HIGH) {
client.println("\"led\": false,");
} else {
client.println("\"led\": true,");
}
client.println("\"success\": true");
client.println("}");
} else {
if (request.indexOf("POST /toggle HTTP/1.1") != -1) {
client.println("{");
digitalWrite(D1, HIGH);
delay(250);
digitalWrite(D1, LOW);
delay(250);
if (digitalRead(D3) == HIGH) {
client.println("\"led\": false,");
} else {
client.println("\"led\": true,");
}
client.println("\"success\": true");
client.println("}");
} else {
client.println("{");
client.println("\"success\": false");
client.println("}");
}
}
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment