WebClientRepeating.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. WiFiEsp example: WebClientRepeating
  3. This sketch connects to a web server and makes an HTTP request
  4. using an Arduino ESP8266 module.
  5. It repeats the HTTP call each 10 seconds.
  6. For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
  7. */
  8. #include "WiFiEsp.h"
  9. // Emulate Serial1 on pins 6/7 if not present
  10. #ifndef HAVE_HWSERIAL1
  11. #include "SoftwareSerial.h"
  12. SoftwareSerial Serial1(6, 7); // RX, TX
  13. #endif
  14. char ssid[] = "Twim"; // your network SSID (name)
  15. char pass[] = "12345678"; // your network password
  16. int status = WL_IDLE_STATUS; // the Wifi radio's status
  17. char server[] = "arduino.cc";
  18. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  19. const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
  20. // Initialize the Ethernet client object
  21. WiFiEspClient client;
  22. void setup()
  23. {
  24. // initialize serial for debugging
  25. Serial.begin(115200);
  26. // initialize serial for ESP module
  27. Serial1.begin(9600);
  28. // initialize ESP module
  29. WiFi.init(&Serial1);
  30. // check for the presence of the shield
  31. if (WiFi.status() == WL_NO_SHIELD) {
  32. Serial.println("WiFi shield not present");
  33. // don't continue
  34. while (true);
  35. }
  36. // attempt to connect to WiFi network
  37. while ( status != WL_CONNECTED) {
  38. Serial.print("Attempting to connect to WPA SSID: ");
  39. Serial.println(ssid);
  40. // Connect to WPA/WPA2 network
  41. status = WiFi.begin(ssid, pass);
  42. }
  43. Serial.println("You're connected to the network");
  44. printWifiStatus();
  45. }
  46. void loop()
  47. {
  48. // if there's incoming data from the net connection send it out the serial port
  49. // this is for debugging purposes only
  50. while (client.available()) {
  51. char c = client.read();
  52. Serial.write(c);
  53. }
  54. // if 10 seconds have passed since your last connection,
  55. // then connect again and send data
  56. if (millis() - lastConnectionTime > postingInterval) {
  57. httpRequest();
  58. }
  59. }
  60. // this method makes a HTTP connection to the server
  61. void httpRequest()
  62. {
  63. Serial.println();
  64. // close any connection before send a new request
  65. // this will free the socket on the WiFi shield
  66. client.stop();
  67. // if there's a successful connection
  68. if (client.connect(server, 80)) {
  69. Serial.println("Connecting...");
  70. // send the HTTP PUT request
  71. client.println(F("GET /asciilogo.txt HTTP/1.1"));
  72. client.println(F("Host: arduino.cc"));
  73. client.println("Connection: close");
  74. client.println();
  75. // note the time that the connection was made
  76. lastConnectionTime = millis();
  77. }
  78. else {
  79. // if you couldn't make a connection
  80. Serial.println("Connection failed");
  81. }
  82. }
  83. void printWifiStatus()
  84. {
  85. // print the SSID of the network you're attached to
  86. Serial.print("SSID: ");
  87. Serial.println(WiFi.SSID());
  88. // print your WiFi shield's IP address
  89. IPAddress ip = WiFi.localIP();
  90. Serial.print("IP Address: ");
  91. Serial.println(ip);
  92. // print the received signal strength
  93. long rssi = WiFi.RSSI();
  94. Serial.print("Signal strength (RSSI):");
  95. Serial.print(rssi);
  96. Serial.println(" dBm");
  97. }