#include <WiFi.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>
// WiFi credentials
//const char* ssid = "your_wifi_ssid";
//const char* password = "your_wifi_password";
// Define the number of GPIOs and their pins
const int numGpios = 5;
const int gpioPins[numGpios] = {2, 4, 5, 12, 13}; // Change pins according to your setup
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
}
void loop() {
// Read GPIO statuses from CSV file
File file = SPIFFS.open("/gpio_statuses.csv", "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
// Parse JSON from file
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, file);
if (error) {
Serial.println("Failed to parse file");
file.close();
return;
}
// Close the file
file.close();
// Turn on/off GPIOs based on CSV content
for (int i = 0; i < numGpios; i++) {
String gpioKey = "gpio_" + String(i);
if (doc.containsKey(gpioKey)) {
int gpioStatus = doc[gpioKey];
digitalWrite(gpioPins[i], gpioStatus);
}
}
}