WebServerAP.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. WiFiEsp example: WebServerAP
  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 start an access point and print the IP address of your
  6. ESP8266 module to the Serial monitor. From there, you can open
  7. that address in a web browser 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[] = "TwimEsp"; // 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. // use a ring buffer to increase speed and reduce memory allocation
  23. RingBuffer buf(8);
  24. void setup()
  25. {
  26. Serial.begin(115200); // initialize serial for debugging
  27. Serial1.begin(9600); // initialize serial for ESP module
  28. WiFi.init(&Serial1); // initialize ESP module
  29. // check for the presence of the shield
  30. if (WiFi.status() == WL_NO_SHIELD) {
  31. Serial.println("WiFi shield not present");
  32. while (true); // don't continue
  33. }
  34. Serial.print("Attempting to start AP ");
  35. Serial.println(ssid);
  36. // uncomment these two lines if you want to set the IP address of the AP
  37. //IPAddress localIp(192, 168, 111, 111);
  38. //WiFi.configAP(localIp);
  39. // start access point
  40. status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
  41. Serial.println("Access point started");
  42. printWifiStatus();
  43. // start the web server on port 80
  44. server.begin();
  45. Serial.println("Server started");
  46. }
  47. void loop()
  48. {
  49. WiFiEspClient client = server.available(); // listen for incoming clients
  50. if (client) { // if you get a client,
  51. Serial.println("New client"); // print a message out the serial port
  52. buf.init(); // initialize the circular buffer
  53. while (client.connected()) { // loop while the client's connected
  54. if (client.available()) { // if there's bytes to read from the client,
  55. char c = client.read(); // read a byte, then
  56. buf.push(c); // push it to the ring buffer
  57. // you got two newline characters in a row
  58. // that's the end of the HTTP request, so send a response
  59. if (buf.endsWith("\r\n\r\n")) {
  60. sendHttpResponse(client);
  61. break;
  62. }
  63. }
  64. }
  65. // give the web browser time to receive the data
  66. delay(10);
  67. // close the connection
  68. client.stop();
  69. Serial.println("Client disconnected");
  70. }
  71. }
  72. void sendHttpResponse(WiFiEspClient client)
  73. {
  74. client.print(
  75. "HTTP/1.1 200 OK\r\n"
  76. "Content-Type: text/html\r\n"
  77. "Connection: close\r\n" // the connection will be closed after completion of the response
  78. "Refresh: 20\r\n" // refresh the page automatically every 20 sec
  79. "\r\n");
  80. client.print("<!DOCTYPE HTML>\r\n");
  81. client.print("<html>\r\n");
  82. client.print("<h1>Hello World!</h1>\r\n");
  83. client.print("Requests received: ");
  84. client.print(++reqCount);
  85. client.print("<br>\r\n");
  86. client.print("Analog input A0: ");
  87. client.print(analogRead(0));
  88. client.print("<br>\r\n");
  89. client.print("</html>\r\n");
  90. }
  91. void printWifiStatus()
  92. {
  93. // print your WiFi shield's IP address
  94. IPAddress ip = WiFi.localIP();
  95. Serial.print("IP Address: ");
  96. Serial.println(ip);
  97. // print where to go in the browser
  98. Serial.println();
  99. Serial.print("To see this page in action, connect to ");
  100. Serial.print(ssid);
  101. Serial.print(" and open a browser to http://");
  102. Serial.println(ip);
  103. Serial.println();
  104. }