Example

Example List

Examples

This example is ..........

On / Off Button 20160330

This Example is to control ESP-01 gpio 0 and 1 to on and off by Server client request

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Server + GPIO On/OFF + USART Display

This example is ..........

PWM SAMPLE

This example is ..........

Examples - Send AT Command to Change ESP8266 SETUP

This example is ..........

      1. /* * SANKI 2016 03 31 * * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * http://server_ip/gpio0 will set the GPIO0 high/low, * http://server_ip/gpio2 will set the GPIO2 high/low * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <SoftwareSerial.h> //SoftwareSerial mySerial(0, 2); // RX, TX const char* ssid = "<SSID>"; const char* password = "<Password>"; ESP8266WebServer server(1102); const int led = 0; void handleNotFound(){ digitalWrite(LED_BUILTIN , 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(LED_BUILTIN , 0); } void handleMessage(int gpio0, int gpio2){ digitalWrite(LED_BUILTIN , 1); // Prepare the response //String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html"; String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"; //s += "<body style=\"background-color:lightgrey;\">"; // Color Only s += "<head>"; s += "<style>"; // ------------------------------ Button s += ".button {"; //s += " background-color: #4CAF50; /* Green */"; s += " border: none;"; s += " color: white;"; s += " padding: 15px 32px;"; s += " text-align: center;"; s += " text-decoration: none;"; s += " display: inline-block;"; s += " font-size: 16px;"; s += " margin: 4px 2px;"; s += " cursor: pointer;"; s += " -webkit-transition-duration: 0.4s; /* Safari */"; s += " transition-duration: 0.4s;"; s += "}"; s += ""; s += ".button1 {"; s += " background-color: #4CAF50; font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);"; s += "}"; s += ""; s += ".button2 {"; s += " background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);"; s += "}"; s += ".reseton {"; s += " background-color: #4CAF50; border-radius: 12px;"; s += "}"; s += ".resetoff {"; s += " background-color: #f44336; border-radius: 12px;"; s += "}"; s += " #wrap{"; s += "width:1000px;"; s += "margin:0 auto;"; s += "}"; s += "body {"; s += " background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");"; s += " background-repeat: no-repeat;"; s += " background-position: center top;"; s += " background-color: lightgrey"; s += " margin-right: 20px;"; s += " margin-left: 20px;"; s += " margin-top: 180px;"; s += "}"; s += "h1 {"; s += " background-color: green;"; s += "}"; s += ""; s += "div {"; s += " background-color: lightblue;"; s += "}"; s += ""; s += "p {"; s += " background-color: yellow;"; s += " font-size: 48px;"; s += "}"; s += " p.ridge {border-style: ridge;}"; s += "</style>"; s += "</head>"; s += "<body align=center>"; s += ""; // s += " <div class=\"wrap\">"; /* s += "<h1>GPIO2 is now "; s += (val)?"high":"low"; s += "</h1>"; */ s += "<p>ESP-01</p>"; if (gpio0 == 0) s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button2\">OFF</button></a></p>"; else s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button1\">ON</button></a></p>"; if (gpio2 == 0) s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button2\">OFF</button></a></p>"; else s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button1\">ON</button></a></p>"; // ----------------------------- Reset All gpio s += "<p class=\"ridge\">Reset : <a href=\"\\reseton\"><button class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button class=\"button resetoff\">All OFF</button></a></p>"; // --------------------------- USART Display by Forms s += "<form action=\"\\usart\" method=\"get\">"; //s += "First name: <input type="text" name="FirstName" value="Mickey"><br>"; //s += "Last name: <input type="text" name="LastName" value="Mouse"><br>"; s += "<p>Page Number : <select id=\"pageno\" name=\"pageno\">"; s += " <option value=\"0\">0. Main Page</option>"; s += " <option value=\"1\">1. Device List</option>"; s += " <option value=\"2\">2. Others</option>"; s += "</select>"; s += "</p>"; s += "<input type=\"submit\" value=\"Submit\">"; s += "</form>"; //s += m + "\n"; s += "</body></html>\n"; server.send(200, "text/html", s); digitalWrite(LED_BUILTIN , 0); } byte x = 255; void USARTsend(String comm) { Serial.print(comm); Serial.write(x); Serial.write(x); Serial.write(x); delay(100); } void handleRoot() { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); handleMessage(digitalRead(0), digitalRead(2)); digitalWrite(LED_BUILTIN , 0); } void handleReset(int onoff) { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); if (onoff == 0) { digitalWrite(0, 0); digitalWrite(2, 0); handleMessage(0 ,0); } if (onoff == 1) { digitalWrite(0, 1); digitalWrite(2, 1); handleMessage(1, 1); } digitalWrite(LED_BUILTIN , 0); } void setup(void){ pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); // USART MUst be 9600 and control by USART Command! Serial.begin(9600); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } server.begin(); Serial.println("HTTP server started"); Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); pinMode(0, OUTPUT); digitalWrite(0, 0); server.on("/", handleRoot); // ---------------------------- Control USART Display server.on("/usart", []() { String state=server.arg("pageno"); if (state == "0") USARTsend("page 0"); if (state == "1") USARTsend("page 1"); if (state == "2") USARTsend("page 2"); //server.send(200, "text/plain", "Led is now " + state); handleRoot(); }); server.on("/reseton", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(1); }); server.on("/resetoff", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(0); }); server.on("/gpio2", [](){ String m = "GPIO 2 : "; int val = digitalRead(2); if (val == 0) { val = 1; m += "ON"; } else { val = 0; m += "OFF"; } digitalWrite(2, val); //server.send(200, "text/plain", m); handleMessage(digitalRead(0), val); }); server.on("/gpio0", [](){ String m = "GPIO 0 : "; int val = digitalRead(0); if (val == 0) { val = 1; digitalWrite(0, 1); m += "ON"; } else { val = 0; digitalWrite(0, 0); m += "OFF"; } //digitalWrite(led, val); //server.send(200, "text/plain", m); handleMessage(val, digitalRead(2)); }); /* server.on("/gpio2/0", [](){ server.send(200, "text/plain", "Turn OFF GPIO 2"); digitalWrite(2, 0); }); */ server.onNotFound(handleNotFound); } void loop(void){ server.handleClient(); }

      2. /* * SANKI 2016 03 31 * * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * http://server_ip/gpio0 will set the GPIO0 high/low, * http://server_ip/gpio2 will set the GPIO2 high/low * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <SoftwareSerial.h> //SoftwareSerial mySerial(0, 2); // RX, TX const char* ssid = "<SSID>"; const char* password = "<Password>"; ESP8266WebServer server(1104); const int led = 0; String sMessage = ""; void handleNotFound(){ digitalWrite(LED_BUILTIN , 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(LED_BUILTIN , 0); } void handleMessage(int gpio0, int gpio2){ digitalWrite(LED_BUILTIN , 1); // Prepare the response //String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html"; String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"; //s += "<body style=\"background-color:lightgrey;\">"; // Color Only s += "<head>"; s += "<style>"; //------------------------------- Select s += " select{"; s += " width: 20%;"; s += " height: 100%;"; s += " padding: 5px;"; s += " font-size:24px;"; s += " }"; s += " select option { color: black; }"; s += " select option:first-child"; s += " {"; s += " color: green;"; s += " // font-size:25px;"; s += " }"; // ------------------------------ Button s += ".button {"; //s += " background-color: #4CAF50; /* Green */"; s += " border: none;"; s += " color: white;"; s += " padding: 15px 32px;"; s += " text-align: center;"; s += " text-decoration: none;"; s += " display: inline-block;"; s += " font-size: 16px;"; s += " margin: 4px 2px;"; s += " cursor: pointer;"; s += " -webkit-transition-duration: 0.4s; /* Safari */"; s += " transition-duration: 0.4s;"; s += "}"; s += ""; s += ".button1 {"; s += " background-color: #4CAF50; font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);"; s += "}"; s += ""; s += ".button2 {"; s += " background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);"; s += "}"; s += ".reseton {"; s += " background-color: #4CAF50; border-radius: 12px;"; s += "}"; s += ".resetoff {"; s += " background-color: #f44336; border-radius: 12px;"; s += "}"; s += " #wrap{"; s += "width:1000px;"; s += "margin:0 auto;"; s += "}"; s += "body {"; s += " background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");"; s += " background-repeat: no-repeat;"; s += " background-position: center top;"; s += " background-color: lightgrey"; s += " margin-right: 20px;"; s += " margin-left: 20px;"; s += " margin-top: 180px;"; s += "}"; s += "h1 {"; s += " background-color: green;"; s += "}"; s += ""; s += "div {"; s += " background-color: lightblue;"; s += "}"; s += ""; s += "p {"; s += " background-color: yellow;"; s += " font-size: 48px;"; s += "}"; s += " p.ridge {border-style: ridge;}"; s += "</style>"; s += "</head>"; s += "<body align=center>"; s += ""; // s += " <div class=\"wrap\">"; /* s += "<h1>GPIO2 is now "; s += (val)?"high":"low"; s += "</h1>"; */ s += "PWN TEST : " + sMessage + ""; if (gpio0 == 0) s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button2\">OFF</button></a></p>"; else s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button1\">ON</button></a></p>"; s += "<form action=\"\\analogWrite\" method=\"get\">"; //s += "First name: <input type="text" name="FirstName" value="Mickey"><br>"; //s += "Last name: <input type="text" name="LastName" value="Mouse"><br>"; s += "<p>Analog Write to Pin 2 : <select id=\"pwm\" name=\"pwm\">"; s += " <option value=\"0\">analogWrite 0</option>"; s += " <option value=\"256\">analogWrite 256</option>"; s += " <option value=\"512\">analogWrite 512</option>"; s += " <option value=\"768\">analogWrite 768</option>"; s += " <option value=\"1023\">analogWrite 1024</option>"; s += "</select>"; s += "</p>"; s += "<input type=\"submit\" value=\"Submit\">"; s += "</form>"; /* if (gpio2 == 0) s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button2\">OFF</button></a></p>"; else s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button1\">ON</button></a></p>"; */ // ----------------------------- Reset All gpio s += "<p class=\"ridge\">Reset : <a href=\"\\reseton\"><button class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button class=\"button resetoff\">All OFF</button></a></p>"; // --------------------------- PWM Display by Forms //s += m + "\n"; s += "</body></html>\n"; server.send(200, "text/html", s); digitalWrite(LED_BUILTIN , 0); } String ipToString(IPAddress ip){ String s=""; for (int i=0; i<4; i++) s += i ? "." + String(ip[i]) : String(ip[i]); return s; } //- See more at: http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf void handleRoot() { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); handleMessage(digitalRead(0), digitalRead(2)); digitalWrite(LED_BUILTIN , 0); } void handleReset(int onoff) { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); if (onoff == 0) { digitalWrite(0, 0); digitalWrite(2, 0); handleMessage(0 ,0); } if (onoff == 1) { digitalWrite(0, 1); digitalWrite(2, 1); handleMessage(1, 1); } digitalWrite(LED_BUILTIN , 0); } void setup(void){ pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); // USART MUst be 9600 and control by USART Command! Serial.begin(9600); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } server.begin(); Serial.println("HTTP server started"); Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } uint8_t MAC_array[6]; char MAC_char[18]; WiFi.macAddress(MAC_array); sprintf(MAC_char, "%02x:%02x:%02x:%02x:%02x:%02x", MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]); Serial.print("MAC Address : "); Serial.println(MAC_char); sMessage += "[SSID : " + String(ssid) + "] [Server IP : " + ipToString(WiFi.localIP()) + "] [MAC : " + MAC_char + "]"; // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); pinMode(0, OUTPUT); digitalWrite(0, 0); server.on("/", handleRoot); // ---------------------------- Control USART Display server.on("/analogWrite", []() { String state=server.arg("pwm"); analogWrite(2, state.toInt()); //if (state == "1") USARTsend("page 1"); //if (state == "2") USARTsend("page 2"); //server.send(200, "text/plain", "Led is now " + state); handleRoot(); }); server.on("/reseton", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(1); }); server.on("/resetoff", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(0); }); server.on("/gpio2", [](){ String m = "GPIO 2 : "; int val = digitalRead(2); if (val == 0) { val = 1; m += "ON"; } else { val = 0; m += "OFF"; } digitalWrite(2, val); //server.send(200, "text/plain", m); handleMessage(digitalRead(0), val); }); server.on("/gpio0", [](){ String m = "GPIO 0 : "; int val = digitalRead(0); if (val == 0) { val = 1; digitalWrite(0, 1); m += "ON"; } else { val = 0; digitalWrite(0, 0); m += "OFF"; } //digitalWrite(led, val); //server.send(200, "text/plain", m); handleMessage(val, digitalRead(2)); }); /* server.on("/gpio2/0", [](){ server.send(200, "text/plain", "Turn OFF GPIO 2"); digitalWrite(2, 0); }); */ server.onNotFound(handleNotFound); } void loop(void){ server.handleClient(); }

        1. // Programa: Web Server com modulo ESP8266

        2. // Alteracoes e adaptacoes: FILIPEFLOP

        3. #include <SoftwareSerial.h>

        4. //RX pino 2, TX pino 3

        5. SoftwareSerial esp8266(2, 3);

        6. #define DEBUG true

        7. void setup()

        8. {

        9. Serial.begin(9600);

        10. esp8266.begin(19200);

        11. sendData("AT+RST\r\n", 2000, DEBUG); // rst

        12. // Conecta a rede wireless

        13. sendData("AT+CWJAP=\"SSID\",\"SENHA\"\r\n", 2000, DEBUG);

        14. delay(3000);

        15. sendData("AT+CWMODE=1\r\n", 1000, DEBUG);

        16. // Mostra o endereco IP

        17. sendData("AT+CIFSR\r\n", 1000, DEBUG);

        18. // Configura para multiplas conexoes

        19. sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);

        20. // Inicia o web server na porta 80

        21. sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);

        22. }

        23. void loop()

        24. {

        25. // Verifica se o ESP8266 esta enviando dados

        26. if (esp8266.available())

        27. {

        28. if (esp8266.find("+IPD,"))

        29. {

        30. delay(300);

        31. int connectionId = esp8266.read() - 48;

        32. String webpage = "<head><meta http-equiv=""refresh"" content=""3"">";

        33. webpage += "</head><h1><u>ESP8266 - Web Server</u></h1><h2>Porta";

        34. webpage += "Digital 8: ";

        35. int a = digitalRead(8);

        36. webpage += a;

        37. webpage += "<h2>Porta Digital 9: ";

        38. int b = digitalRead(9);

        39. webpage += b;

        40. webpage += "</h2>";

        41. String cipSend = "AT+CIPSEND=";

        42. cipSend += connectionId;

        43. cipSend += ",";

        44. cipSend += webpage.length();

        45. cipSend += "\r\n";

        46. sendData(cipSend, 1000, DEBUG);

        47. sendData(webpage, 1000, DEBUG);

        48. String closeCommand = "AT+CIPCLOSE=";

        49. closeCommand += connectionId; // append connection id

        50. closeCommand += "\r\n";

        51. sendData(closeCommand, 3000, DEBUG);

        52. }

        53. }

        54. }

        55. String sendData(String command, const int timeout, boolean debug)

        56. {

        57. // Envio dos comandos AT para o modulo

        58. String response = "";

        59. esp8266.print(command);

        60. long int time = millis();

        61. while ( (time + timeout) > millis())

        62. {

        63. while (esp8266.available())

        64. {

        65. // The esp has data so display its output to the serial window

        66. char c = esp8266.read(); // read the next character.

        67. response += c;

        68. }

        69. }

        70. if (debug)

        71. {

        72. Serial.print(response);

        73. }

        74. return response;

        75. }

        1. /* * SANKI 2016 03 31 * * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * http://server_ip/gpio0 will set the GPIO0 high/low, * http://server_ip/gpio2 will set the GPIO2 high/low * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <SoftwareSerial.h> const char* ssid = "<SSID>"; const char* password = "<Password>"; ESP8266WebServer server(1104); const int led = 0; String sMessage = ""; void handleNotFound(){ digitalWrite(LED_BUILTIN , 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(LED_BUILTIN , 0); } void handleMessage(int gpio0, int gpio2){ digitalWrite(LED_BUILTIN , 1); // Prepare the response //String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html"; String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"; //s += "<body style=\"background-color:lightgrey;\">"; // Color Only s += "<head>"; s += "<style>"; // ------------------------------ Button s += ".button {"; //s += " background-color: #4CAF50; /* Green */"; s += " border: none;"; s += " color: white;"; s += " padding: 15px 32px;"; s += " text-align: center;"; s += " text-decoration: none;"; s += " display: inline-block;"; s += " font-size: 16px;"; s += " margin: 4px 2px;"; s += " cursor: pointer;"; s += " -webkit-transition-duration: 0.4s; /* Safari */"; s += " transition-duration: 0.4s;"; s += "}"; s += ""; s += ".button1 {"; s += " background-color: #4CAF50; font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);"; s += "}"; s += ""; s += ".button2 {"; s += " background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);"; s += "}"; s += ".reseton {"; s += " background-color: #4CAF50; border-radius: 12px;"; s += "}"; s += ".resetoff {"; s += " background-color: #f44336; border-radius: 12px;"; s += "}"; s += " #wrap{"; s += "width:1000px;"; s += "margin:0 auto;"; s += "}"; s += "body {"; s += " background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");"; s += " background-repeat: no-repeat;"; s += " background-position: center top;"; s += " background-color: lightgrey"; s += " margin-right: 20px;"; s += " margin-left: 20px;"; s += " margin-top: 180px;"; s += "}"; s += "h1 {"; s += " background-color: green;"; s += "}"; s += ""; s += "div {"; s += " background-color: lightblue;"; s += "}"; s += ""; s += "p {"; s += " background-color: yellow;"; s += " font-size: 48px;"; s += "}"; s += " p.ridge {border-style: ridge;}"; s += "</style>"; s += "</head>"; s += "<body align=center>"; s += ""; // s += " <div class=\"wrap\">"; /* s += "<h1>GPIO2 is now "; s += (val)?"high":"low"; s += "</h1>"; */ s += "" + sMessage + ""; if (gpio0 == 0) s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button2\">OFF</button></a></p>"; else s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button1\">ON</button></a></p>"; if (gpio2 == 0) s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button2\">OFF</button></a></p>"; else s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button1\">ON</button></a></p>"; s += "<p class=\"ridge\">Reset : <a href=\"\\reseton\"><button class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button class=\"button resetoff\">All OFF</button></a></p>"; //s += "</div>"; //s += m + "\n"; s += "</body></html>\n"; server.send(200, "text/html", s); digitalWrite(LED_BUILTIN , 0); } void handleRoot() { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); handleMessage(digitalRead(0), digitalRead(2)); digitalWrite(LED_BUILTIN , 0); } void handleReset(int onoff) { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); if (onoff == 0) { digitalWrite(0, 0); digitalWrite(2, 0); handleMessage(0 ,0); } if (onoff == 1) { digitalWrite(0, 1); digitalWrite(2, 1); handleMessage(1, 1); } digitalWrite(LED_BUILTIN , 0); } String ipToString(IPAddress ip){ String s=""; for (int i=0; i<4; i++) s += i ? "." + String(ip[i]) : String(ip[i]); return s; } //- See more at: http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf void setup(void){ pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); Serial.begin(115200); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } server.begin(); Serial.println("HTTP server started"); Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } uint8_t MAC_array[6]; char MAC_char[18]; WiFi.macAddress(MAC_array); sprintf(MAC_char, "%02x:%02x:%02x:%02x:%02x:%02x", MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]); Serial.print("MAC Address : "); Serial.println(MAC_char); sMessage += "[SSID : " + String(ssid) + "] [Server IP : " + ipToString(WiFi.localIP()) + "] [MAC : " + MAC_char + "]"; // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); pinMode(0, OUTPUT); digitalWrite(0, 0); server.on("/", handleRoot); server.on("/reseton", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(1); }); server.on("/resetoff", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(0); }); server.on("/gpio2", [](){ String m = "GPIO 2 : "; int val = digitalRead(2); if (val == 0) { val = 1; m += "ON"; } else { val = 0; m += "OFF"; } digitalWrite(2, val); //server.send(200, "text/plain", m); handleMessage(digitalRead(0), val); }); server.on("/gpio0", [](){ String m = "GPIO 0 : "; int val = digitalRead(0); if (val == 0) { val = 1; digitalWrite(0, 1); m += "ON"; } else { val = 0; digitalWrite(0, 0); m += "OFF"; } //digitalWrite(led, val); //server.send(200, "text/plain", m); handleMessage(val, digitalRead(2)); }); /* server.on("/gpio2/0", [](){ server.send(200, "text/plain", "Turn OFF GPIO 2"); digitalWrite(2, 0); }); */ server.onNotFound(handleNotFound); } void loop(void){ server.handleClient(); }