Skip to content

Instantly share code, notes, and snippets.

@todbot
Created February 26, 2024 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todbot/690da609a6a034bd98f6bfa62f402360 to your computer and use it in GitHub Desktop.
Save todbot/690da609a6a034bd98f6bfa62f402360 to your computer and use it in GitHub Desktop.
Arduino WiFiWebClient, verified to work on PicoW with arduino-pico core
// from:
// https://docs.arduino.cc/retired/library-examples/wifi-library/WiFiWebClient/
#include <WiFi.h>
// Enter your WiFi SSID and password
char ssid[] = "todbot"; // your network SSID (name)
char pass[] = "testtesttest"; // your network password (use for WPA, or use as key for WEP)
char server[] = "wifitest.adafruit.com"; // host name address for adafruit test
char path[] = "/testwifi/index.html"; // path part of URL to fetch
WiFiClient client;
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(115200);
//Initialize serial and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
int rc = WiFi.begin(ssid, pass);
Serial.printf("rc:%d\n", rc);
while (rc != WL_CONNECTED) {
rc = WiFi.status();
Serial.printf("%ld: status: %d\n ", millis(), rc);
delay(100);
}
Serial.println("");
Serial.println("Connected to WiFi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.print("GET ");
client.print(path);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
// do nothing forevermore:
while (true)
;
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment