WebServer.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. WiFiEsp example: WebServer
  3. A simple web server that shows the value of the analog input
  4. pins via a web page using an ESP8266 module.
  5. This sketch will print the IP address of your ESP8266 module (once connected)
  6. to the Serial monitor. From there, you can open that address in a web browser
  7. to display the web page.
  8. The web page will be automatically refreshed each 20 seconds.
  9. For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
  10. */
  11. #include "WiFiEsp.h"
  12. // Emulate Serial1 on pins 6/7 if not present
  13. #ifndef HAVE_HWSERIAL1
  14. #include "SoftwareSerial.h"
  15. SoftwareSerial Serial1(6, 7); // RX, TX
  16. #endif
  17. char ssid[] = "Twim"; // your network SSID (name)
  18. char pass[] = "12345678"; // your network password
  19. int status = WL_IDLE_STATUS; // the Wifi radio's status
  20. int reqCount = 0; // number of requests received
  21. WiFiEspServer server(80);
  22. void setup()
  23. {
  24. // initialize serial for debugging
  25. Serial.begin(115200);
  26. // initialize serial for ESP module
  27. Serial1.begin(9600);
  28. // initialize ESP module
  29. WiFi.init(&Serial1);
  30. // check for the presence of the shield
  31. if (WiFi.status() == WL_NO_SHIELD) {
  32. Serial.println("WiFi shield not present");
  33. // don't continue
  34. while (true);
  35. }
  36. // attempt to connect to WiFi network
  37. while ( status != WL_CONNECTED) {
  38. Serial.print("Attempting to connect to WPA SSID: ");
  39. Serial.println(ssid);
  40. // Connect to WPA/WPA2 network
  41. status = WiFi.begin(ssid, pass);
  42. }
  43. Serial.println("You're connected to the network");
  44. printWifiStatus();
  45. // start the web server on port 80
  46. server.begin();
  47. }
  48. void loop()
  49. {
  50. // listen for incoming clients
  51. WiFiEspClient client = server.available();
  52. if (client) {
  53. Serial.println("New client");
  54. // an http request ends with a blank line
  55. boolean currentLineIsBlank = true;
  56. while (client.connected()) {
  57. if (client.available()) {
  58. char c = client.read();
  59. Serial.write(c);
  60. // if you've gotten to the end of the line (received a newline
  61. // character) and the line is blank, the http request has ended,
  62. // so you can send a reply
  63. if (c == '\n' && currentLineIsBlank) {
  64. Serial.println("Sending response");
  65. // send a standard http response header
  66. // use \r\n instead of many println statements to speedup data send
  67. client.print(
  68. "HTTP/1.1 200 OK\r\n"
  69. "Content-Type: text/html\r\n"
  70. "Connection: close\r\n" // the connection will be closed after completion of the response
  71. "Refresh: 20\r\n" // refresh the page automatically every 20 sec
  72. "\r\n");
  73. client.print("<!DOCTYPE HTML>\r\n");
  74. client.print("<html>\r\n");
  75. client.print("<h1>Hello World!</h1>\r\n");
  76. client.print("Requests received: ");
  77. client.print(++reqCount);
  78. client.print("<br>\r\n");
  79. client.print("Analog input A0: ");
  80. client.print(analogRead(0));
  81. client.print("<br>\r\n");
  82. client.print("</html>\r\n");
  83. break;
  84. }
  85. if (c == '\n') {
  86. // you're starting a new line
  87. currentLineIsBlank = true;
  88. }
  89. else if (c != '\r') {
  90. // you've gotten a character on the current line
  91. currentLineIsBlank = false;
  92. }
  93. }
  94. }
  95. // give the web browser time to receive the data
  96. delay(10);
  97. // close the connection:
  98. client.stop();
  99. Serial.println("Client disconnected");
  100. }
  101. }
  102. void printWifiStatus()
  103. {
  104. // print the SSID of the network you're attached to
  105. Serial.print("SSID: ");
  106. Serial.println(WiFi.SSID());
  107. // print your WiFi shield's IP address
  108. IPAddress ip = WiFi.localIP();
  109. Serial.print("IP Address: ");
  110. Serial.println(ip);
  111. // print where to go in the browser
  112. Serial.println();
  113. Serial.print("To see this page in action, open a browser to http://");
  114. Serial.println(ip);
  115. Serial.println();
  116. }