Advertisement
m3k_p

Arduino weeEsp8266

Mar 1st, 2019
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ESP8266.h"
  2. #include <SoftwareSerial.h>
  3.  
  4. #define SSID        "********"
  5. #define PASSWORD    "********"
  6. #define HOST_NAME   "www.google.com"
  7. #define HOST_PORT   (80)
  8. #define ESP8266_USE_SOFTWARE_SERIAL
  9.  
  10. SoftwareSerial MySerial(3, 2); /* RX:D3, TX:D2 */
  11. ESP8266 wifi(MySerial);
  12.  
  13. void setup(void){
  14.     Serial.begin(9600);
  15.  
  16.      if (wifi.setOprToStationSoftAP()) {
  17.         Serial.print("to station + softap ok\r\n");
  18.     } else {
  19.         Serial.print("to station + softap err\r\n");
  20.     }
  21.  
  22.  
  23.     if (wifi.joinAP(SSID, PASSWORD)) {
  24.         Serial.print("Join AP success\r\n");
  25.  
  26.         Serial.print("IP:");
  27.         Serial.println( wifi.getLocalIP().c_str());      
  28.     } else {
  29.         Serial.print("Join AP failure\r\n");
  30.     }
  31.    
  32.     if (wifi.disableMUX()) {
  33.         Serial.print("single ok\r\n");
  34.     } else {
  35.         Serial.print("single err\r\n");
  36.     }
  37.    
  38.     Serial.print("setup end\r\n");
  39.  
  40. //fim void
  41. }
  42.  
  43. void lool(void){
  44.       uint8_t buffer[1024] = {0};
  45.  
  46.     if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
  47.         Serial.print("create tcp ok\r\n");
  48.     } else {
  49.         Serial.print("create tcp err\r\n");
  50.     }
  51.  
  52.     const char *hello = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
  53.     wifi.send((const uint8_t*)hello, strlen(hello));
  54.  
  55.     uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
  56.     if (len > 0) {
  57.         Serial.print("Received:[");
  58.         for(uint32_t i = 0; i < len; i++) {
  59.             Serial.print((char)buffer[i]);
  60.         }
  61.         Serial.print("]\r\n");
  62.     }
  63.  
  64.      if (wifi.releaseTCP()) {
  65.         Serial.print("release tcp ok\r\n");
  66.     } else {
  67.         Serial.print("release tcp err\r\n");
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement