/* NB this is the Sonoff version with no radio remote control components * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pins depending on the request * NB! set fixed WIFI address * sonoff header 1 - vcc 3v3 2 - rx 3 - tx 4 - gnd 5 - gpio 14 esp8266 connections gpio 0 - button gpio 12 - relay gpio 13 - green led - active low gpio 14 - connect via 1 kohm to the red led in the two colour led (the bare led wire nearest the antenna end of the board? */ #include // from a terminal or bash script send one of - // curl "http://192.168.0.104/(00)/" //gpio12relay off (pin low) // curl "http://192.168.0.104/(01)/" //gpio12relay on (pin high) // curl "http://192.168.0.104/(20)/" //gpio13greenled on (pin low) // curl "http://192.168.0.104/(21)/" //gpio13greenled off (pin high) // curl "http://192.168.0.104/(30)/" //gpio14redled on (pin low) // curl "http://192.168.0.104/(31)/" //gpio14redled off (pin high) // - or just place http://192.168.0.104/(00)/ in the address box of a browser // - Lines below like "if (req.indexOf("(01)") != -1)" could detect more descriptive texts) const char* ssid = "SSID"; const char* password = "password"; int gpio12relay_pin = 12; int gpio13greenled_pin = 13; int gpio14redled_pin = 14; int val0 = 0; int val2 = 0; int val3 = 0; int old_val0 = 0; int old_val2 = 0; int old_val3 = 0; //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' for Interrupt volatile int state = LOW; // so we can toggle the relay const byte BUTTON = 0; // push the button (on GPIO 0) to toggle the relay // Interrupt Service Routine (ISR) void switchPressed () { state = !state; digitalWrite (gpio12relay_pin, state); } //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup start void setup() { Serial.begin(115200); delay(10); // prepare GPIO12 pinMode(gpio12relay_pin, OUTPUT); digitalWrite(gpio12relay_pin, 0); // prepare GPIO13 pinMode(gpio13greenled_pin, OUTPUT); digitalWrite(gpio13greenled_pin, 0); // prepare GPIO14 pinMode(gpio14redled_pin, OUTPUT); digitalWrite(gpio14redled_pin, 0); //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' for Interrupt digitalWrite (BUTTON, HIGH); // internal pull-up resistor attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, FALLING); // attach interrupt handler //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // config static IP IPAddress ip(192, 168, 0, 104); // the desired IPAddress IPAddress gateway(192, 168, 0, 230); // set gateway to match your network Serial.print(F("Setting static ip to : ")); Serial.println(ip); IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network WiFi.config(ip, gateway, subnet); // Print the IP address //Serial.println(WiFi.localIP()); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup end //======================================================== loop start void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request // first digit is led/relay code number second digit is state requested if (req.indexOf("(00)") != -1) //not equal to -1 means it was present {val0 = 0; val2 = old_val2; val3 = old_val3;} // gpio12relay turns off, gpio13greenled does not change, gpio14redled_pin does not change if (req.indexOf("(01)") != -1) {val0 = 1; val2 = old_val2; val3 = old_val3;} // gpio12relay turns on, gpio13greenled does not change, gpio14redled_pin does not change if (req.indexOf("(20)") != -1) {val0 = old_val0; val2 = 0; val3 = old_val3;} // gpio12relay does not change, gpio13greenled turns off, gpio14redled_pin does not change if (req.indexOf("(21)") != -1) {val0 = old_val0; val2 = 1; val3 = old_val3;} // gpio12relay does not change, gpio13greenled turns on, gpio14redled_pin does not change if (req.indexOf("(30)") != -1) {val0 = old_val0; val2 = old_val2; val3 = 0;} // gpio12relay does not change, gpio13greenled does not change, gpio14redled_pin turns off if (req.indexOf("(31)") != -1) {val0 = old_val0; val2 = old_val2; val3 = 1;} // gpio12relay does not change, gpio13greenled does not change, gpio14redled_pin turns on Serial.print("old_val0 = "); Serial.println(old_val0); Serial.print("old_val2 = "); Serial.println(old_val2); Serial.print("val0 = "); Serial.println(val0); Serial.print("val2 = "); Serial.println(val2); old_val0 = val0; // relay old_val2 = val2; // green led old_val3 = val3; // red led //else { //Serial.println("invalid request"); //client.stop(); //return; //} if (val0 == 1) { state = HIGH; } else { state = LOW; } // a button press will toggle this value // Set GPIO according to the request digitalWrite(gpio12relay_pin, state); digitalWrite(gpio13greenled_pin, val2); digitalWrite(gpio14redled_pin, val3); client.flush(); // Prepare the response String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nThe power relay is now "; s += (val0)?"on":"off"; s += "
"; // Send the response to the client // just a test - build a decent web page with buttons etc here client.print(s); if (val2 == 0) { client.print("the green LED is on
");} else { client.print("the green LED is off
");} if (val3 == 0) { client.print("the red LED is on
");} else { client.print("the red LED is off
");} delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is destroyed } //=================================================== loop end