WebClient.ino 2.6 KB

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