#include <WiFi.h>
#include <ThingSpeak.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = ""; // your network password
// ThingSpeak information
unsigned long myChannelNumber = 2305284;
const char * myWriteAPIKey = "UJO9HGT5RQM8CQMU";
const char * server = "api.thingspeak.com";
WiFiClient client;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
pinMode(13, INPUT);
pinMode(2, OUTPUT);
}
void loop() {
int value = analogRead(13);
int intensity = map(value, 0, 1023, 0, 255);
analogWrite(2, intensity);
Serial.println(intensity);
ThingSpeak.setField(1, intensity);
// Write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // Wait a bit before the next update
}