#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long channelID = 2592587; // Replace with your channel ID
const char* writeAPIKey = "99M051VYOYFHQU1I"; // Replace with your write API key
const int ledPin1 = 2; // GPIO2 on ESP32
const int ledPin2 = 4; // GPIO4 on ESP32
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
ThingSpeak.begin(client);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
int status = ThingSpeak.writeField(channelID, 1, random(0, 100), writeAPIKey);
if (status == 200) {
Serial.println("Channel update successful!");
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(1000);
} else {
Serial.print("Problem updating channel. HTTP error code: ");
Serial.println(status);
}
delay(20000); // Wait 20 seconds before sending the next update
}