#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST"; // Use "Wokwi-GUEST" for simulation
const char* password = ""; // No password needed for Wokwi Wi-Fi simulation
// ThingSpeak API details
const char* server = "http://api.thingspeak.com/update";
String apiKey = "1VBD86KE8ESLDZVS"; // Replace with actual API Key (for real-world use)
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
Serial.println("ESP32 IP Address: " + WiFi.localIP().toString());
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { // Check if Wi-Fi is connected
HTTPClient http;
// Generate random sensor data for simulation
int temperature = random(20, 35);
int humidity = random(40, 80);
// Construct the request URL
String url = String(server) + "?api_key=" + apiKey + "&field1=" + String(temperature) +
"&field2=" + String(humidity);
http.begin(url);
int httpResponseCode = http.GET(); // Send GET request
http.end(); // Close connection
// Print data to Serial Monitor
Serial.print("Data Sent to ThingSpeak - Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
} else {
Serial.println("Wi-Fi Disconnected. Reconnecting...");
WiFi.disconnect();
WiFi.reconnect();
}
delay(15000); // Wait 15 seconds before sending the next data
}
Loading
esp32-devkit-v1
esp32-devkit-v1
Loading
ssd1306
ssd1306