#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "wc6wdb9dvn";
const char* server = "hhtp://api.thingspeak.com/update";
String apikey = "XP5CDGPG1R7KS2ZA";
int potPin = 34; //Potentiometer pin (ADC)
unsigned long lastSendTime = 0;
const unsigned long sendInterval = 5000; // Send to ThingSpeak every 5 s
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WIFI");
while(WiFi.status() !=WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
int sensorValue = analogRead(potPin); // 0 to 4095
// Show value in Serial Monitor
Serial.print("Potentiometer Value: ");
Serial.print(sensorValue);
delay(2000);
// Send to ThinSpeak every 5 seconds
if (millis() - lastSendTime >= sendInterval) {
if(WiFi.status() == WL_CONNECTED) {
HTTPCLIENT http;
String url= String(server) + "?api_key=" + apikey + "&feild1" + String(sensorValue);
http.begin(url);
int responseCode = http.GET();
if (responsecode > 0) {
Serial.print("Thingspeak response code: ");
Serial.print(responseCode);
} else {
Serial.print("Error sending data. Code: ");
Serial.println(responseCode);
}
http.end();
}
lastSendTime = millis();
}
delay(30); // small delay for smoother updates
}