/* This code is for directly programming an ESP8266 The handleRoot() function is probably all you need to look at. Currently, it is set to let you control a single relay or LED via a pin. The rest just configures the ESP8266 as a webserver that responds to root, "/". This code is based on... File -> Examples -> ESP8266WebServer -> HelloServer */ #include #include #include const char* ssid = "SkynetGlobalDefenseNetwork"; const char* password = "password"; // to control something like a relay int const pin = 2; // The current setting of 2 does not really work // because GPIO2 must not be held LOW during boot. // Either disconnect GPIO2 on boot or get an // ESP8266 module with more pins than an ESP-01. ESP8266WebServer server(80); bool state = false; void handleRoot() { if (server.args() > 2){ state = (server.arg(2) == "true"); } if (state) { Serial.println("on"); server.send(200, "text/plain", "on"); digitalWrite(pin,HIGH); } else { Serial.println("off"); server.send(200, "text/plain", "off"); digitalWrite(pin,LOW); } } void handleNotFound(){ server.send(200, "text/plain", "What are you doing?"); } void setup(void){ pinMode(pin,OUTPUT); digitalWrite(pin,LOW); Serial.begin(115200); WiFi.mode(WIFI_STA); 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.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop(void){ server.handleClient(); }