WiFiSSLClient.ino
1 // SPDX-FileCopyrightText: 2015 Arturo Guadalupi 2 // SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries 3 // 4 // SPDX-License-Identifier: MIT 5 6 /* 7 This example creates a client object that connects and transfers 8 data using always SSL. 9 10 It is compatible with the methods normally related to plain 11 connections, like client.connect(host, port). 12 13 Written by Arturo Guadalupi 14 last revision November 2015 15 16 */ 17 18 #include <WiFiClientSecure.h> 19 20 // Enter your WiFi SSID and password 21 char ssid[] = "YOUR_SSID"; // your network SSID (name) 22 char pass[] = "YOUR_SSID_PASSWORD"; // your network password (use for WPA, or use as key for WEP) 23 int keyIndex = 0; // your network key Index number (needed only for WEP) 24 25 int status = WL_IDLE_STATUS; 26 // if you don't want to use DNS (and reduce your sketch size) 27 // use the numeric IP instead of the name for the server: 28 //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) 29 30 #define SERVER "cdn.syndication.twimg.com" 31 #define PATH "/widgets/followbutton/info.json?screen_names=adafruit" 32 33 // Initialize the SSL client library 34 // with the IP address and port of the server 35 // that you want to connect to (port 443 is default for HTTPS): 36 WiFiClientSecure client; 37 38 void setup() { 39 //Initialize serial and wait for port to open: 40 Serial.begin(115200); 41 while (!Serial) { 42 ; // wait for serial port to connect. Needed for native USB port only 43 } 44 45 // attempt to connect to Wifi network: 46 Serial.print("Attempting to connect to SSID: "); 47 Serial.println(ssid); 48 49 WiFi.begin(ssid, pass); 50 while (WiFi.status() != WL_CONNECTED) { 51 delay(500); 52 Serial.print("."); 53 } 54 55 Serial.println(""); 56 Serial.println("Connected to WiFi"); 57 printWifiStatus(); 58 59 client.setInsecure(); // don't use a root cert 60 61 Serial.println("\nStarting connection to server..."); 62 // if you get a connection, report back via serial: 63 if (client.connect(SERVER, 443)) { 64 Serial.println("connected to server"); 65 // Make a HTTP request: 66 client.println("GET " PATH " HTTP/1.1"); 67 client.println("Host: " SERVER); 68 client.println("Connection: close"); 69 client.println(); 70 } 71 } 72 73 uint32_t bytes = 0; 74 75 void loop() { 76 // if there are incoming bytes available 77 // from the server, read them and print them: 78 while (client.available()) { 79 char c = client.read(); 80 Serial.write(c); 81 bytes++; 82 } 83 84 // if the server's disconnected, stop the client: 85 if (!client.connected()) { 86 Serial.println(); 87 Serial.println("disconnecting from server."); 88 client.stop(); 89 Serial.print("Read "); Serial.print(bytes); Serial.println(" bytes"); 90 91 // do nothing forevermore: 92 while (true); 93 } 94 } 95 96 97 void printWifiStatus() { 98 // print the SSID of the network you're attached to: 99 Serial.print("SSID: "); 100 Serial.println(WiFi.SSID()); 101 102 // print your board's IP address: 103 IPAddress ip = WiFi.localIP(); 104 Serial.print("IP Address: "); 105 Serial.println(ip); 106 107 // print the received signal strength: 108 long rssi = WiFi.RSSI(); 109 Serial.print("signal strength (RSSI):"); 110 Serial.print(rssi); 111 Serial.println(" dBm"); 112 }