Advertisement
Spits

WiFi Client

Jun 7th, 2021
2,554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.00 KB | None | 0 0
  1. /*
  2.     This sketch establishes a TCP connection to a "quote of the day" service.
  3.     It sends a "hello" message, and then prints received data.
  4. */
  5.  
  6. #include <ESP8266WiFi.h>
  7.  
  8. #ifndef STASSID
  9. #define STASSID "ssid"
  10. #define STAPSK  "password"
  11. #endif
  12.  
  13. const char* ssid     = STASSID;
  14. const char* password = STAPSK;
  15.  
  16. const char* host = "djxmmx.net";
  17. const uint16_t port = 17;
  18.  
  19. void setup() {
  20.   Serial.begin(115200);
  21.   Serial.setDebugOutput(true);
  22.  
  23.   // We start by connecting to a WiFi network
  24.  
  25.   Serial.println();
  26.   Serial.println();
  27.   Serial.print("Connecting to ");
  28.   Serial.println(ssid);
  29.  
  30.   /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
  31.      would try to act as both a client and an access-point and could cause
  32.      network-issues with your other WiFi-devices on your WiFi-network. */
  33.   WiFi.mode(WIFI_STA);
  34.   WiFi.hostname("NodeMCU");
  35.  
  36.   //IPAddress ip(172, 16, 2, 2);
  37.   //IPAddress dns(172, 16, 0, 1);
  38.   //IPAddress gw(172, 16, 0, 1);
  39.   //IPAddress sn(255, 255, 240, 0);
  40.   //WiFi.config(ip, dns, gw, sn);
  41.  
  42.   WiFi.begin(ssid, password);
  43.   int i = 1;
  44.   while (WiFi.status() != WL_CONNECTED) {
  45.     delay(500);
  46.  
  47.     Serial.print("Versuch: ");
  48.     Serial.print(i);
  49.     Serial.print(" // ");
  50.     Serial.print("WiFi.status(): ");
  51.     Serial.print(WiFi.status());
  52.     Serial.print(" // ");
  53.     Serial.print("wifi_station_get_connect_status(): ");
  54.     Serial.println(wifi_station_get_connect_status());
  55.  
  56.     //WiFi.printDiag(Serial);
  57.  
  58.     i++;
  59.   }
  60.  
  61.   Serial.println("");
  62.   Serial.println("WiFi connected");
  63.   Serial.println("IP address: ");
  64.   Serial.println(WiFi.localIP());
  65.  
  66.   Serial.println("");
  67.   WiFi.printDiag(Serial);
  68.   Serial.println("");
  69. }
  70.  
  71. void loop() {
  72.   static bool wait = false;
  73.  
  74.   Serial.print("connecting to ");
  75.   Serial.print(host);
  76.   Serial.print(':');
  77.   Serial.println(port);
  78.  
  79.   // Use WiFiClient class to create TCP connections
  80.   WiFiClient client;
  81.   if (!client.connect(host, port)) {
  82.     Serial.println("connection failed");
  83.     delay(5000);
  84.     return;
  85.   }
  86.  
  87.   // This will send a string to the server
  88.   Serial.println("sending data to server");
  89.   if (client.connected()) {
  90.     client.println("hello from ESP8266");
  91.   }
  92.  
  93.   // wait for data to be available
  94.   unsigned long timeout = millis();
  95.   while (client.available() == 0) {
  96.     if (millis() - timeout > 5000) {
  97.       Serial.println(">>> Client Timeout !");
  98.       client.stop();
  99.       delay(60000);
  100.       return;
  101.     }
  102.   }
  103.  
  104.   // Read all the lines of the reply from server and print them to Serial
  105.   Serial.println("receiving from remote server");
  106.   // not testing 'client.connected()' since we do not need to send data here
  107.   while (client.available()) {
  108.     char ch = static_cast<char>(client.read());
  109.     Serial.print(ch);
  110.   }
  111.  
  112.   // Close the connection
  113.   Serial.println();
  114.   Serial.println("closing connection");
  115.   client.stop();
  116.  
  117.   if (wait) {
  118.     delay(300000); // execute once every 5 minutes, don't flood remote service
  119.   }
  120.   wait = true;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement