[关闭]
@kfihihc 2015-01-20T12:19:17.000000Z 字数 2868 阅读 104694

Tutorial for ESP8266 Serial WiFi Module

If you want to get any update about ESP8266 module, such as new ESP8266 WiFi product, manufacture and etc, please follow us at twitter.

Twitter: @kfihihc

In this tutorial, we'll use a seeeduino to control the ESP8266 WiFi module to request a static page from the internet. This is a basic use of TCP socket, for other usage, please refer to the AT command guide of the module.

Material list:

Note: We used a software serial to print some debugging information as there’s only one hardware serial on seeeduino board. But the limitation of software serial is that it can’t communicate in a higher baud rate than 19200. So part of the output from ESP module will be dropped because the baud rate 57600 of ESP module is higher than that of the software serial. If you have a board with more than one hardware serial (e.g. Arduino Mega 2560), the case will be easier.

Step-By-Step

Step 1: connect module as the following picture

Connection

Step 2: Programe the seeeduino board.

  1. Open Arduino IDE and create a new sketch;
  2. Parse the following code into the sketch editor (need modifying the SSID and PASS macros into your own situation);
  1. #include <SoftwareSerial.h>
  2. #define SSID "xxxxxxxx"
  3. #define PASS "xxxxxxxx"
  4. #define DST_IP "220.181.111.85" //baidu.com
  5. SoftwareSerial dbgSerial(10, 11); // RX, TX
  6. void setup()
  7. {
  8. // Open serial communications and wait for port to open:
  9. Serial.begin(57600);
  10. Serial.setTimeout(5000);
  11. dbgSerial.begin(9600); //can't be faster than 19200 for softserial
  12. dbgSerial.println("ESP8266 Demo");
  13. //test if the module is ready
  14. Serial.println("AT+RST");
  15. delay(1000);
  16. if(Serial.find("ready"))
  17. {
  18. dbgSerial.println("Module is ready");
  19. }
  20. else
  21. {
  22. dbgSerial.println("Module have no response.");
  23. while(1);
  24. }
  25. delay(1000);
  26. //connect to the wifi
  27. boolean connected=false;
  28. for(int i=0;i<5;i++)
  29. {
  30. if(connectWiFi())
  31. {
  32. connected = true;
  33. break;
  34. }
  35. }
  36. if (!connected){while(1);}
  37. delay(5000);
  38. //print the ip addr
  39. /*Serial.println("AT+CIFSR");
  40. dbgSerial.println("ip address:");
  41. while (Serial.available())
  42. dbgSerial.write(Serial.read());*/
  43. //set the single connection mode
  44. Serial.println("AT+CIPMUX=0");
  45. }
  46. void loop()
  47. {
  48. String cmd = "AT+CIPSTART=\"TCP\",\"";
  49. cmd += DST_IP;
  50. cmd += "\",80";
  51. Serial.println(cmd);
  52. dbgSerial.println(cmd);
  53. if(Serial.find("Error")) return;
  54. cmd = "GET / HTTP/1.0\r\n\r\n";
  55. Serial.print("AT+CIPSEND=");
  56. Serial.println(cmd.length());
  57. if(Serial.find(">"))
  58. {
  59. dbgSerial.print(">");
  60. }else
  61. {
  62. Serial.println("AT+CIPCLOSE");
  63. dbgSerial.println("connect timeout");
  64. delay(1000);
  65. return;
  66. }
  67. Serial.print(cmd);
  68. delay(2000);
  69. //Serial.find("+IPD");
  70. while (Serial.available())
  71. {
  72. char c = Serial.read();
  73. dbgSerial.write(c);
  74. if(c=='\r') dbgSerial.print('\n');
  75. }
  76. dbgSerial.println("====");
  77. delay(1000);
  78. }
  79. boolean connectWiFi()
  80. {
  81. Serial.println("AT+CWMODE=1");
  82. String cmd="AT+CWJAP=\"";
  83. cmd+=SSID;
  84. cmd+="\",\"";
  85. cmd+=PASS;
  86. cmd+="\"";
  87. dbgSerial.println(cmd);
  88. Serial.println(cmd);
  89. delay(2000);
  90. if(Serial.find("OK"))
  91. {
  92. dbgSerial.println("OK, Connected to WiFi.");
  93. return true;
  94. }else
  95. {
  96. dbgSerial.println("Can not connect to the WiFi.");
  97. return false;
  98. }
  99. }

Step 3: Open Serial Monitor and press the reset button of seeeduino board, you’ll see the output.

At last, Happy Hakcing! :)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注