Skip to content

Instantly share code, notes, and snippets.

@sleemanj
Created September 7, 2022 22:53
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 sleemanj/03e466b0cb3216e60140acc4637bc584 to your computer and use it in GitHub Desktop.
Save sleemanj/03e466b0cb3216e60140acc4637bc584 to your computer and use it in GitHub Desktop.
A simple Arduino example of connecting an ESP32 to WIFI and requesting (HTTP GET) a page from the internet.
#include <WiFi.h>
#include <HTTPClient.h>
const String ssid = "PUT_YOUR_WIFI_NAME_HERE";
const String password = "PUT_YOUR_WIFI_PASS_HERE";
HTTPClient http;
String url = "http://sparks.gogo.co.nz/esp32/hello.html";
void setup() {
Serial.begin(115200);
Serial.print("Connecting to wifi " + ssid + "...");
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.print("OK\n\n");
Serial.print("ESP32 IP Address: ");
Serial.print( WiFi.localIP() );
Serial.print("\n\n");
delay(2000);
}
void loop() {
do
{
Serial.print("\nFetching " + url + "... ");
http.begin(url);
int httpResponseCode = http.GET();
// 200 is a normal response code
if (httpResponseCode == 200)
{
Serial.println("OK");
Serial.println("\n--- BEGIN CONTENT ---");
String payload = http.getString();
Serial.println(payload);
Serial.println("--- END CONTENT ---");
}
// Otherwise (eg 404) treat as an error
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
Serial.println("Repeats in 10 seconds...");
delay(10000);
} while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment