#include <WiFi.h> // Include the Wi-Fi library for ESP32
#include <AdafruitIO_WiFi.h> // Include the Adafruit IO library for ESP32
// Replace with your Adafruit IO credentials
#define IO_USERNAME "Santoshrpps"
#define IO_KEY "aio_yVho54dbJ0NRJFREs38FPJwdrvtC"
// Replace with your Wi-Fi credentials
#define WIFI_SSID "Santosh"
#define WIFI_PASS "santosh2"
// Create an instance of the Adafruit IO client
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
// Create an instance of the Adafruit IO feed
AdafruitIO_Feed *ledFeed = io.feed("status");
// Define the pin where the LED is connected (adjust pin as per ESP32 GPIO layout)
const int ledPin = 23; // GPIO 23, modify based on your wiring
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to Wi-Fi and Adafruit IO
Serial.print("Connecting to Adafruit IO...");
io.connect();
// Wait for a connection to Adafruit IO
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("Connected to Adafruit IO!");
// Set up a callback for when data is received from the feed
ledFeed->onMessage(handleMessage);
}
// This function is called when a message is received on the feed
void handleMessage(AdafruitIO_Data *data) {
int ledState = data->toInt(); // Convert received data to integer
if (ledState == 1) {
digitalWrite(ledPin, HIGH); // Turn the LED on
Serial.println("LED ON");
} else if (ledState == 0) {
digitalWrite(ledPin, LOW); // Turn the LED off
Serial.println("LED OFF");
}
}
void loop() {
// Run the Adafruit IO client
io.run();
}