Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
COBOL 2.67 KB | None | 0 0
  1. /*
  2.  WiFiEsp example: WebClient
  3.  
  4.  This sketch connects to google website using an ESP8266 module to
  5.  perform a simple web search.
  6.  
  7.  For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
  8. */
  9.  
  10. #include "WiFiEsp.h"
  11.  
  12. // Emulate Serial1 on pins 6/7 if not present
  13. #ifndef HAVE_HWSERIAL1
  14. #include "SoftwareSerial.h"
  15. SoftwareSerial Serial1(2, 3); // RX, TX
  16. #endif
  17.  
  18. char ssid[] = "arduinotest";            // your network SSID (name)
  19. char pass[] = "123456789";        // your network password
  20. int status = WL_IDLE_STATUS;     // the Wifi radio's status
  21.  
  22. char server[] = "arduino.cc";
  23.  
  24. // Initialize the Ethernet client object
  25. WiFiEspClient client;
  26.  
  27. void setup()
  28. {
  29.  // initialize serial for debugging
  30.  Serial.begin(115200);
  31.  // initialize serial for ESP module
  32.  Serial1.begin(9600);
  33.  // initialize ESP module
  34.  WiFi.init(&Serial1);
  35.  
  36.  // check for the presence of the shield
  37.  if (WiFi.status() == WL_NO_SHIELD) {
  38.    Serial.println("WiFi shield not present");
  39.    // don't continue
  40.     while (true);
  41.   }
  42.  
  43.   // attempt to connect to WiFi network
  44.   while ( status != WL_CONNECTED) {
  45.     Serial.print("Attempting to connect to WPA SSID: ");
  46.     Serial.println(ssid);
  47.     // Connect to WPA/WPA2 network
  48.     status = WiFi.begin(ssid, pass);
  49.   }
  50.  
  51.   // you're connected now, so print out the data
  52.  Serial.println("You're connected to the network");
  53.  
  54.  printWifiStatus();
  55.  
  56.  Serial.println();
  57.  Serial.println("Starting connection to server...");
  58.  // if you get a connection, report back via serial
  59.  if (client.connect(server, 80)) {
  60.    Serial.println("Connected to server");
  61.    // Make a HTTP request
  62.    client.println("GET /asciilogo.txt HTTP/1.1");
  63.    client.println("Host: arduino.cc");
  64.    client.println("Connection: close");
  65.    client.println();
  66.  }
  67. }
  68.  
  69. void loop()
  70. {
  71.  // if there are incoming bytes available
  72.  // from the server, read them and print them
  73.  while (client.available()) {
  74.    char c = client.read();
  75.    Serial.write(c);
  76.  }
  77.  
  78.  // if the server's disconnected, stop the client
  79.  if (!client.connected()) {
  80.    Serial.println();
  81.    Serial.println("Disconnecting from server...");
  82.    client.stop();
  83.  
  84.    // do nothing forevermore
  85.    while (true);
  86.  }
  87. }
  88.  
  89.  
  90. void printWifiStatus()
  91. {
  92.  // print the SSID of the network you're attached to
  93.  Serial.print("SSID: ");
  94.  Serial.println(WiFi.SSID());
  95.  
  96.  // print your WiFi shield's IP address
  97.  IPAddress ip = WiFi.localIP();
  98.  Serial.print("IP Address: ");
  99.  Serial.println(ip);
  100.  
  101.  // print the received signal strength
  102.  long rssi = WiFi.RSSI();
  103.  Serial.print("Signal strength (RSSI):");
  104.  Serial.print(rssi);
  105.  Serial.println(" dBm");
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement