2018年4月22日 星期日

How To Use the ESP8266 and Arduino as a Webserver

How To Use the ESP8266 and Arduino as a Webserver

by MIGUEL on DECEMBER 30, 2014
in ESP8266

Video Tutorial: How The Code Works

The video below shows you how the ESP8266 works as a webserver with the Arduino using the code below.

Arduino Webserver Code/Sketch For ESP8266

The code handles the ESP8266’s initialization in the setup() function: it resets the module, configures it as an access point, prints out the module’s ip address, configures for multiple connections, configures as a server on port 80.
When there is data available if the string +IPD is in the serial data then the HTTP response is sent to the browser or device requesting it.
  1. #include <SoftwareSerial.h>
  2.  
  3. #define DEBUG true
  4.  
  5. SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
  6. // This means that you need to connect the TX line from the esp to the Arduino's pin 2
  7. // and the RX line from the esp to the Arduino's pin 3
  8. void setup()
  9. {
  10. Serial.begin(9600);
  11. esp8266.begin(9600); // your esp's baud rate might be different
  12. sendData("AT+RST\r\n",2000,DEBUG); // reset module
  13. sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
  14. sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  15. sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  16. sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
  17. }
  18.  
  19. void loop()
  20. {
  21. if(esp8266.available()) // check if the esp is sending a message
  22. {
  23. /*
  24. while(esp8266.available())
  25. {
  26. // The esp has data so display its output to the serial window
  27. char c = esp8266.read(); // read the next character.
  28. Serial.write(c);
  29. } */
  30. if(esp8266.find("+IPD,"))
  31. {
  32. delay(1000);
  33.  
  34. int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
  35. // the ASCII decimal value and 0 (the first decimal number) starts at 48
  36. String webpage = "<h1>Hello</h1>&lth2>World!</h2><button>LED1</button>";
  37.  
  38. String cipSend = "AT+CIPSEND=";
  39. cipSend += connectionId;
  40. cipSend += ",";
  41. cipSend +=webpage.length();
  42. cipSend +="\r\n";
  43. sendData(cipsend,1000,DEBUG);
  44. sendData(webpage,1000,DEBUG);
  45. webpage="<button>LED2</button>";
  46. cipSend = "AT+CIPSEND=";
  47. cipSend += connectionId;
  48. cipSend += ",";
  49. cipSend +=webpage.length();
  50. cipSend +="\r\n";
  51. sendData(cipsend,1000,DEBUG);
  52. sendData(webpage,1000,DEBUG);
  53.  
  54. String closeCommand = "AT+CIPCLOSE=";
  55. closeCommand+=connectionId; // append connection id
  56. closeCommand+="\r\n";
  57. sendData(closeCommand,3000,DEBUG);
  58. }
  59. }
  60. }
  61.  
  62.  
  63. String sendData(String command, const int timeout, boolean debug)
  64. {
  65. String response = "";
  66. esp8266.print(command); // send the read character to the esp8266
  67. long int time = millis();
  68. while( (time+timeout) > millis())
  69. {
  70. while(esp8266.available())
  71. {
  72. // The esp has data so display its output to the serial window
  73. char c = esp8266.read(); // read the next character.
  74. response+=c;
  75. }
  76. }
  77. if(debug)
  78. {
  79. Serial.print(response);
  80. }
  81. return response;
  82. }
  83.  

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...