#include <WiFi.h>
#include <ThingSpeak.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak credentials
unsigned long myChannelNumber = 2383552; // Replace with your channel number
const char* myWriteAPIKey = "OU1PGNBTES730LFG"; // Replace with your ThingSpeak API key
const int sensorPin = 34; // Potentiometer connected to GPIO 34
const int ledPin = 5; // LED connected to GPIO 5
WiFiClient client;
void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(ledPin, OUTPUT); // Set the LED pin as output
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // Connect to WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read the value from the potentiometer
float energyConsumption = map(sensorValue, 0, 4095, 0, 100); // Map to 0-100%
Serial.print("Energy Consumption: ");
Serial.print(energyConsumption);
Serial.println("%");
// Check energy consumption level
if (energyConsumption >= 90) {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("High energy consumption detected. LED turned on.");
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Sending data to ThingSpeak
ThingSpeak.setField(1, energyConsumption);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000); // ThingSpeak update interval
}