#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "http://api.thingspeak.com/update";
String apiKey = "UJU3TD9RKR9BBWGZ";
int potPin = 34;
unsigned long lastSendTime =0;
const unsigned long sendInterval = 500;
void setup (){
Serial.begin(115200);
delay(10);
WiFi.begin(ssid,password);
Serial.print("Connecting to WiFi");
while (WiFi.status() !=WL_CONNECTED){
delay.print(".");
}
Serial.println(\nConnected to WiFi);
}
void loop(){
int sensorValue = analogRead(potPin);
Serial.print("Potentiometer Value");
Serial.println(sensorValue);
delay(2000);
if(millis()- lastSendTime>=sendInterval){
if (WiFi.status()==WL_CONNECTED){
HTTPClient http;
String url = String(Server) + "?api_key=" + apikey + "&field1" +String(sensorValue);
http.begin(url);
int respondCode = http.GET();
if (responseCode > 0){
Serial.print("ThingSpeak response code: ");
Serial.println(responseCode);
} else{
Serial.print("Error sending data. Code:");
Serial.println(responseCode);
}
http.end();
}
lastSendTime = millis();
}
delay(30);
}