Last active
August 10, 2024 18:57
-
-
Save jaythomas/c6a8850c13ec2fddc878c8dadebfae91 to your computer and use it in GitHub Desktop.
Wemos ESP8266 D1 Mini wi-fi example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
const char* ssid = "YourWifiSSID"; | |
const char* password = "YourWifiPassword"; | |
// Set a user ID we want to fetch information from | |
uint16_t user_id = 1; | |
void setup () { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.print("Connecting.."); | |
} | |
Serial.println("success!"); | |
Serial.print("IP Address is: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
// Check WiFi connection status | |
if (WiFi.status() == WL_CONNECTED) { | |
// Declare an object of class HTTPClient | |
HTTPClient http; | |
// Specify request destination | |
String url = "http://jsonplaceholder.typicode.com/users/" + String(user_id); | |
http.begin(url); | |
// Send the request | |
Serial.println("Request URL:" + url); | |
int httpCode = http.GET(); | |
// Check the returning code | |
if (httpCode > 0) { | |
// Get the request response payload | |
String payload = http.getString(); | |
// Print the response payload | |
Serial.println(payload); | |
// Increment user ID for next request | |
user_id += 1; | |
} else { | |
Serial.print("Error: status code "); | |
Serial.println(httpCode); | |
} | |
// Close connection | |
http.end(); | |
} | |
// Send a request every 30 seconds | |
delay(30000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment