WebServerLed.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. WiFiEsp example: WebServerLed
  3. A simple web server that lets you turn on and of an LED via a web page.
  4. This sketch will print the IP address of your ESP8266 module (once connected)
  5. to the Serial monitor. From there, you can open that address in a web browser
  6. to turn on and off the LED on pin 13.
  7. For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
  8. */
  9. #include "WiFiEsp.h"
  10. // Emulate Serial1 on pins 6/7 if not present
  11. #ifndef HAVE_HWSERIAL1
  12. #include "SoftwareSerial.h"
  13. SoftwareSerial Serial1(6, 7); // RX, TX
  14. #endif
  15. char ssid[] = "Twim"; // your network SSID (name)
  16. char pass[] = "12345678"; // your network password
  17. int status = WL_IDLE_STATUS;
  18. int ledStatus = LOW;
  19. WiFiEspServer server(80);
  20. // use a ring buffer to increase speed and reduce memory allocation
  21. RingBuffer buf(8);
  22. void setup()
  23. {
  24. pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
  25. Serial.begin(115200); // initialize serial for debugging
  26. Serial1.begin(9600); // initialize serial for ESP module
  27. WiFi.init(&Serial1); // initialize ESP module
  28. // check for the presence of the shield
  29. if (WiFi.status() == WL_NO_SHIELD) {
  30. Serial.println("WiFi shield not present");
  31. // don't continue
  32. while (true);
  33. }
  34. // attempt to connect to WiFi network
  35. while (status != WL_CONNECTED) {
  36. Serial.print("Attempting to connect to WPA SSID: ");
  37. Serial.println(ssid);
  38. // Connect to WPA/WPA2 network
  39. status = WiFi.begin(ssid, pass);
  40. }
  41. Serial.println("You're connected to the network");
  42. printWifiStatus();
  43. // start the web server on port 80
  44. server.begin();
  45. }
  46. void loop()
  47. {
  48. WiFiEspClient client = server.available(); // listen for incoming clients
  49. if (client) { // if you get a client,
  50. Serial.println("New client"); // print a message out the serial port
  51. buf.init(); // initialize the circular buffer
  52. while (client.connected()) { // loop while the client's connected
  53. if (client.available()) { // if there's bytes to read from the client,
  54. char c = client.read(); // read a byte, then
  55. buf.push(c); // push it to the ring buffer
  56. // printing the stream to the serial monitor will slow down
  57. // the receiving of data from the ESP filling the serial buffer
  58. //Serial.write(c);
  59. // you got two newline characters in a row
  60. // that's the end of the HTTP request, so send a response
  61. if (buf.endsWith("\r\n\r\n")) {
  62. sendHttpResponse(client);
  63. break;
  64. }
  65. // Check to see if the client request was "GET /H" or "GET /L":
  66. if (buf.endsWith("GET /H")) {
  67. Serial.println("Turn led ON");
  68. ledStatus = HIGH;
  69. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  70. }
  71. else if (buf.endsWith("GET /L")) {
  72. Serial.println("Turn led OFF");
  73. ledStatus = LOW;
  74. digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  75. }
  76. }
  77. }
  78. // close the connection
  79. client.stop();
  80. Serial.println("Client disconnected");
  81. }
  82. }
  83. void sendHttpResponse(WiFiEspClient client)
  84. {
  85. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  86. // and a content-type so the client knows what's coming, then a blank line:
  87. client.println("HTTP/1.1 200 OK");
  88. client.println("Content-type:text/html");
  89. client.println();
  90. // the content of the HTTP response follows the header:
  91. client.print("The LED is ");
  92. client.print(ledStatus);
  93. client.println("<br>");
  94. client.println("<br>");
  95. client.println("Click <a href=\"/H\">here</a> turn the LED on<br>");
  96. client.println("Click <a href=\"/L\">here</a> turn the LED off<br>");
  97. // The HTTP response ends with another blank line:
  98. client.println();
  99. }
  100. void printWifiStatus()
  101. {
  102. // print the SSID of the network you're attached to
  103. Serial.print("SSID: ");
  104. Serial.println(WiFi.SSID());
  105. // print your WiFi shield's IP address
  106. IPAddress ip = WiFi.localIP();
  107. Serial.print("IP Address: ");
  108. Serial.println(ip);
  109. // print where to go in the browser
  110. Serial.println();
  111. Serial.print("To see this page in action, open a browser to http://");
  112. Serial.println(ip);
  113. Serial.println();
  114. }