/ ESP32_S2_WiFi_Tests / WiFiWebClient / WiFiWebClient.ino
WiFiWebClient.ino
  1  // SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  
  6  /*
  7    Web client
  8  
  9   This sketch connects to a website (wifitest.adafruit.com/testwifi/index.html)
 10   using the WiFi module.
 11  
 12   This example is written for a network using WPA encryption. For
 13   WEP or WPA, change the Wifi.begin() call accordingly.
 14  
 15   This example is written for a network using WPA encryption. For
 16   WEP or WPA, change the Wifi.begin() call accordingly.
 17  
 18   created 13 July 2010
 19   by dlf (Metodo2 srl)
 20   modified 31 May 2012
 21   by Tom Igoe
 22   */
 23  
 24  #include <WiFi.h>
 25  
 26  // Enter your WiFi SSID and password
 27  char ssid[] = "YOUR_SSID";             // your network SSID (name)
 28  char pass[] = "YOUR_SSID_PASSWORD";    // your network password (use for WPA, or use as key for WEP)
 29  int keyIndex = 0;                      // your network key Index number (needed only for WEP)
 30  
 31  int status = WL_IDLE_STATUS;
 32  // if you don't want to use DNS (and reduce your sketch size)
 33  // use the numeric IP instead of the name for the server:
 34  //IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
 35  
 36  char server[] = "wifitest.adafruit.com";    // name address for adafruit test
 37  char path[]   = "/testwifi/index.html";
 38  
 39  // Initialize the Ethernet client library
 40  // with the IP address and port of the server
 41  // that you want to connect to (port 80 is default for HTTP):
 42  WiFiClient client;
 43  
 44  void setup() {
 45    //Initialize serial and wait for port to open:
 46    Serial.begin(115200);
 47    while (!Serial) {
 48      ; // wait for serial port to connect. Needed for native USB port only
 49    }
 50  
 51    // attempt to connect to Wifi network:
 52    Serial.print("Attempting to connect to SSID: ");
 53    Serial.println(ssid);
 54  
 55    WiFi.begin(ssid, pass);
 56    while (WiFi.status() != WL_CONNECTED) {
 57        delay(500);
 58        Serial.print(".");
 59    }
 60  
 61    Serial.println("");
 62    Serial.println("Connected to WiFi");
 63    printWifiStatus();
 64  
 65    Serial.println("\nStarting connection to server...");
 66    // if you get a connection, report back via serial:
 67    if (client.connect(server, 80)) {
 68      Serial.println("connected to server");
 69      // Make a HTTP request:
 70      client.print("GET "); client.print(path); client.println(" HTTP/1.1");
 71      client.print("Host: "); client.println(server);
 72      client.println("Connection: close");
 73      client.println();
 74    }
 75  }
 76  
 77  void loop() {
 78    // if there are incoming bytes available
 79    // from the server, read them and print them:
 80    while (client.available()) {
 81      char c = client.read();
 82      Serial.write(c);
 83    }
 84  
 85    // if the server's disconnected, stop the client:
 86    if (!client.connected()) {
 87      Serial.println();
 88      Serial.println("disconnecting from server.");
 89      client.stop();
 90  
 91      // do nothing forevermore:
 92      while (true) {
 93        delay(100);
 94      }
 95    }
 96  }
 97  
 98  
 99  void printWifiStatus() {
100    // print the SSID of the network you're attached to:
101    Serial.print("SSID: ");
102    Serial.println(WiFi.SSID());
103  
104    // print your board's IP address:
105    IPAddress ip = WiFi.localIP();
106    Serial.print("IP Address: ");
107    Serial.println(ip);
108  
109    // print the received signal strength:
110    long rssi = WiFi.RSSI();
111    Serial.print("signal strength (RSSI):");
112    Serial.print(rssi);
113    Serial.println(" dBm");
114  }