#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String SUPABASE_URL = "https://your-project.supabase.co/rest/v1/button_status";
const String SUPABASE_API_KEY = "your-anon-key";
const int buttonPin = 13; // Button to GPIO13
const int redLED = 12; // Red LED to GPIO12
const int greenLED = 14; // Green LED to GPIO14
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}
void loop() {
int buttonState = digitalRead(buttonPin);
String status;
if (buttonState == LOW) {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
status = "completed";
} else {
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
status = "error";
}
sendToSupabase(status);
delay(5000); // Avoid spamming Supabase
}
void sendToSupabase(String status) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(SUPABASE_URL);
http.addHeader("Content-Type", "application/json");
http.addHeader("apikey", SUPABASE_API_KEY);
http.addHeader("Authorization", "Bearer " + SUPABASE_API_KEY);
String json = "{\"status\": \"" + status + "\"}";
int httpResponseCode = http.POST(json);
Serial.print("HTTP Response: ");
Serial.println(httpResponseCode);
http.end();
}
}