#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* supabaseUrl = "https://ilvghlnjoczpmhwyxyxi.supabase.co/rest/v1/led_control?id=eq.1";
const char* supabaseApiKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlsdmdobG5qb2N6cG1od3l4eXhpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mjc0Mzc1OTIsImV4cCI6MjA0MzAxMzU5Mn0.8G5jcHbXxvgJNjw0wqxvkELhX4-kDARM-pBp0zOB4og";
int ledPin = 26; // LED connected to GPIO 26
int numattempt=1;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..."+numattempt);
numattempt+=1;
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(supabaseUrl);
http.addHeader("apikey", supabaseApiKey);
http.addHeader("Authorization", "OrvU9tjxw3cJ/vVKr25ETzWEYPU9SJx7x3Gch7Wz/EYnm76dI6C7GsiqfQEVrxwYFYMx8jWlKx7qj8cl4TZLxg==");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response);
// Parse the response JSON
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
// Extract the value of is_led_on
bool isLedOn = doc[0]["is_led_on"];
// Control the LED
if (isLedOn) {
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(ledPin, LOW); // Turn LED off
}
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
}
http.end();
}
}