/*
* PROJET : PILOTAGE RELAIS ÉPHÉMÉRIDE
* PLATEFORME : ESP32
* LOCALISATION : LA RÉUNION
*/
#include <WiFi.h>
#include <RTClib.h>
#include <Dusk2Dawn.h>
// --- CONFIGURATION RÉSEAU ---
const char* nom_wifi = "Wokwi-GUEST";
const char* mdp_wifi = "";
// --- CONFIGURATION ÉPHÉMÉRIDE ---
// Coordonnées pour Saint-Denis (Latitude, Longitude, Fuseau UTC)
Dusk2Dawn reunion(-20.882, 55.450, 4);
// --- BROCHES ---
const int BROCHE_RELAIS = 18;
const int BROCHES_DIP[] = {13, 12, 14, 27};
RTC_DS3231 rtc;
void setup() {
Serial.begin(115200);
pinMode(BROCHE_RELAIS, OUTPUT);
digitalWrite(BROCHE_RELAIS, LOW);
// Configuration des DIP switches (Entrées avec Pull-up)
for (int i = 0; i < 4; i++) {
pinMode(BROCHES_DIP[i], INPUT_PULLUP);
}
if (!rtc.begin()) {
Serial.println("Erreur : RTC DS3231 introuvable !");
while (1);
}
// Synchronisation temporelle initiale via WiFi
Serial.print("Connexion WiFi...");
WiFi.begin(nom_wifi, mdp_wifi);
int tentatives = 0;
while (WiFi.status() != WL_CONNECTED && tentatives < 15) {
delay(500);
Serial.print(".");
tentatives++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWiFi OK. Synchronisation NTP...");
configTime(4 * 3600, 0, "pool.ntp.org");
struct tm infos_temps;
if (getLocalTime(&infos_temps)) {
rtc.adjust(DateTime(infos_temps.tm_year + 1900, infos_temps.tm_mon + 1, infos_temps.tm_mday,
infos_temps.tm_hour, infos_temps.tm_min, infos_temps.tm_sec));
Serial.println("Horloge mise à jour.");
}
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
} else {
Serial.println("\nWiFi échec. Mode autonome activé.");
}
}
void loop() {
DateTime maintenant = rtc.now();
// 1. Lecture de l'offset configuré (Binaire)
int valeur_binaire = 0;
for (int i = 0; i < 4; i++) {
if (digitalRead(BROCHES_DIP[i]) == LOW) { // Switch ON = Masse
valeur_binaire |= (1 << i);
}
}
int minutes_decalage = valeur_binaire * 15;
// 2. Calcul des éphémérides
int lever_soleil = reunion.sunrise(maintenant.year(), maintenant.month(), maintenant.day(), false);
int coucher_soleil = reunion.sunset(maintenant.year(), maintenant.month(), maintenant.day(), false);
int minutes_du_jour = (maintenant.hour() * 60) + maintenant.minute();
int seuil_allumage = coucher_soleil - minutes_decalage;
// 3. Logique de pilotage
bool etat_final = false;
if (minutes_du_jour >= seuil_allumage || minutes_du_jour < lever_soleil) {
etat_final = true;
}
digitalWrite(BROCHE_RELAIS, etat_final ? HIGH : LOW);
// Diagnostic
Serial.printf("[%02d:%02d] Offset: %dmin | Coucher: %02dh%02d | Relais: %s\n",
maintenant.hour(), maintenant.minute(), minutes_decalage,
coucher_soleil/60, coucher_soleil%60, etat_final ? "ON" : "OFF");
delay(30000);
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1