#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
// Replace with your local network IP address or ngrok public URL
const char* serverUrl = "http://jsonplaceholder.typicode.com/todos/1";
// Define the LED pin
const int ledPin = 2;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is off initially
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi Connected!");
Serial.println(WiFi.localIP());
// Check the server and turn on the LED if any valid response is received
checkServerAndTurnOnLed();
}
void loop() {
// Empty loop, as the LED turning on logic is in the setup function for this example
}
void checkServerAndTurnOnLed() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println(httpResponseCode);
Serial.println(payload);
// Turn on the LED for any valid response
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("LED ON");
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
Serial.println(http.errorToString(httpResponseCode).c_str()); // Print the error string for more details
}
http.end();
} else {
Serial.println("Wi-Fi not connected");
}
}