#include <WiFi.h>
#include <ThingSpeak.h>
// WiFi parameters
const char* ssid = "Wokwi-Guest";
const char* password = "";
// ThingSpeak parameters
unsigned long channelID = YOUR_CHANNEL_ID; // Replace with your ThingSpeak channel ID
const char* writeAPIKey = "your_write_API_key"; // Replace with your ThingSpeak write API Key
// Pin assignments
const int relay1Pin = 2; // Replace with the pin connected to Relay 1
const int relay2Pin = 3; // Replace with the pin connected to Relay 2
const int led1Pin = 4; // Replace with the pin connected to LED 1
const int led2Pin = 5; // Replace with the pin connected to LED 2
// Initialize the WiFi client
WiFiClient client;
void setup() {
// Initialize serial communication
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("Connected to WiFi");
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Set the relay and LED pins as output
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}
void loop() {
// Read the values from ThingSpeak
int relay1Value = ThingSpeak.readField(channelID, 1, writeAPIKey).toInt();
int relay2Value = ThingSpeak.readField(channelID, 2, writeAPIKey).toInt();
// Control the relays and LEDs
digitalWrite(relay1Pin, relay1Value);
digitalWrite(relay2Pin, relay2Value);
digitalWrite(led1Pin, relay1Value);
digitalWrite(led2Pin, relay2Value);
// Delay for 1 second
delay(1000);
}