Advertisement
Guest User

esp8266 example

a guest
Nov 11th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. #define SSID "myaccesspointssid"
  2. #define PASS "1234"
  3. #define DEST_HOST "10.0.1.11"
  4. #define DEST_IP "10.0.1.11"
  5. #define TIMEOUT 5000 // mS
  6. #define CONTINUE false
  7. #define HALT true
  8.  
  9. // #define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor
  10.  
  11. // Print error message and loop stop.
  12. void errorHalt(String msg)
  13. {
  14. Serial.println(msg);
  15. Serial.println("HALT");
  16. while(true){};
  17. }
  18.  
  19. // Read characters from WiFi module and echo to serial until keyword occurs or timeout.
  20. boolean echoFind(String keyword)
  21. {
  22. byte current_char = 0;
  23. byte keyword_length = keyword.length();
  24.  
  25. // Fail if the target string has not been sent by deadline.
  26. long deadline = millis() + TIMEOUT;
  27. while(millis() < deadline)
  28. {
  29. if (Serial1.available())
  30. {
  31. char ch = Serial1.read();
  32. Serial.write(ch);
  33. if (ch == keyword[current_char])
  34. if (++current_char == keyword_length)
  35. {
  36. Serial.println();
  37. return true;
  38. }
  39. }
  40. }
  41. return false; // Timed out
  42. }
  43.  
  44. // Read and echo all available module output.
  45. // (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
  46. void echoFlush()
  47. {while(Serial1.available()) Serial.write(Serial1.read());}
  48.  
  49. // Echo module output until 3 newlines encountered.
  50. // (Used when we're indifferent to "OK" vs. "no change" responses.)
  51. void echoSkip()
  52. {
  53. echoFind("\n"); // Search for nl at end of command echo
  54. echoFind("\n"); // Search for 2nd nl at end of response.
  55. echoFind("\n"); // Search for 3rd nl at end of blank line.
  56. }
  57.  
  58. // Send a command to the module and wait for acknowledgement string
  59. // (or flush module output if no ack specified).
  60. // Echoes all data received to the serial monitor.
  61. boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
  62. {
  63. Serial1.println(cmd);
  64. #ifdef ECHO_COMMANDS
  65. Serial.print("--"); Serial.println(cmd);
  66. #endif
  67.  
  68. // If no ack response specified, skip all available module output.
  69. if (ack == "")
  70. echoSkip();
  71. else
  72. // Otherwise wait for ack.
  73. if (!echoFind(ack)) // timed out waiting for ack string
  74. if (halt_on_fail)
  75. errorHalt(cmd+" failed");// Critical failure halt.
  76. else
  77. return false; // Let the caller handle it.
  78. return true; // ack blank or ack found
  79. }
  80.  
  81. // Connect to the specified wireless network.
  82. boolean connectWiFi()
  83. {
  84. String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\"";
  85. if (echoCommand(cmd, "OK", CONTINUE)) // Join Access Point
  86. {
  87. Serial.println("Connected to WiFi.");
  88. return true;
  89. }
  90. else
  91. {
  92. Serial.println("Connection to WiFi failed.");
  93. return false;
  94. }
  95. }
  96.  
  97. // ******** SETUP ********
  98. void setup()
  99. {
  100. Serial.begin(115200); // Communication with PC monitor via USB
  101. Serial1.begin(9600); // Communication with ESP8266 via 5V/3.3V level shifter
  102.  
  103. Serial1.setTimeout(TIMEOUT);
  104. Serial.println("ESP8266 Demo");
  105.  
  106. delay(2000);
  107.  
  108. echoCommand("AT+RST", "ready", HALT); // Reset & test if the module is ready
  109. Serial.println("Module is ready.");
  110. echoCommand("AT+GMR", "OK", CONTINUE); // Retrieves the firmware ID (version number) of the module.
  111. echoCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode.
  112.  
  113. echoCommand("AT+CWLAP", "OK", CONTINUE); // List available access points - DOESN't WORK FOR ME
  114.  
  115. echoCommand("AT+CWMODE=1", "", HALT); // Station mode
  116. echoCommand("AT+CIPMUX=1", "", HALT); // Allow multiple connections (we'll only use the first).
  117.  
  118. //connect to the wifi
  119. boolean connection_established = false;
  120. for(int i=0;i<5;i++)
  121. {
  122. if(connectWiFi())
  123. {
  124. connection_established = true;
  125. break;
  126. }
  127. }
  128. if (!connection_established) errorHalt("Connection failed");
  129.  
  130. delay(5000);
  131.  
  132. //echoCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection
  133. echoCommand("AT+CIFSR", "", HALT); // Echo IP address. (Firmware bug - should return "OK".)
  134. //echoCommand("AT+CIPMUX=0", "", HALT); // Set single connection mode
  135. }
  136.  
  137. // ******** LOOP ********
  138. void loop()
  139. {
  140. // Establish TCP connection
  141. String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += DEST_IP; cmd += "\",80";
  142. if (!echoCommand(cmd, "OK", CONTINUE)) return;
  143. delay(2000);
  144.  
  145. // Get connection status
  146. if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE)) return;
  147.  
  148. // Build HTTP request.
  149. cmd = "GET / HTTP/1.1\r\nHost: "; cmd += DEST_HOST; cmd += ":80\r\n\r\n";
  150.  
  151. // Ready the module to receive raw data
  152. if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE))
  153. {
  154. echoCommand("AT+CIPCLOSE", "", CONTINUE);
  155. Serial.println("Connection timeout.");
  156. return;
  157. }
  158.  
  159. // Send the raw HTTP request
  160. echoCommand(cmd, "OK", CONTINUE); // GET
  161.  
  162. // Loop forever echoing data received from destination server.
  163. while(true)
  164. while (Serial1.available())
  165. Serial.write(Serial1.read());
  166.  
  167. errorHalt("ONCE ONLY");
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement