#include <WiFi.h> // For ESP32, or use ESP8266WiFi.h for ESP8266
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST"; // Default WiFi in Wokwi
const char* password = ""; // No password required
const char* apiKey = "U76AB7WUHQ3N799A"; // Replace with your ThingSpeak Write API Key
const char* server = "http://api.thingspeak.com/update";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey + "&field1=" + random(20, 30); // Example: Sending random temperature
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak: " + String(httpResponseCode));
} else {
Serial.println("Failed to send data!");
}
http.end();
} else {
Serial.println("WiFi Disconnected!");
}
delay(20000); // Send data every 20 seconds
}