#include <WiFi.h>
#define ESP_ID 1
#define IsDebug 1
#if ESP_ID == 1
// contenu du fichier ID_MAC.h
//#include "ID_MAC.h"
#ifndef ID_MAC_H
#define ID_MAC_H
//#include <stdint.h>
typedef struct {
uint8_t id;
uint8_t mac[6];
} Node;
static const Node nodes[] = {
// {1, {0xE8, 0x3D, 0xC1, 0x94, 0x7E, 0xB4}}, // com 10
{1, {0x24, 0x0A, 0xC4, 0x00, 0x01, 0x10}}, // test WokWi
{2, {0xE8, 0x3D, 0xC1, 0x94, 0x81, 0x8C}}, // com 11
{3, {0xE8, 0x3D, 0xC1, 0x94, 0x6C, 0x08}}, // com 12
{4, {0xE8, 0x3D, 0xC1, 0x95, 0x93, 0x4C}}, // com 14
{5, {0xE8, 0x3D, 0xC1, 0x93, 0x3A, 0x84}}, // com 15
{6, {0xE8, 0x3D, 0xC1, 0x94, 0x6F, 0x3C}}, // com 16
{7, {0xE8, 0x3D, 0xC1, 0x94, 0x95, 0xE0}}, // com 17
{8, {0xE8, 0x3D, 0xC1, 0x94, 0x89, 0xD0}}, // com 18
{9, {0xE8, 0x3D, 0xC1, 0x94, 0x82, 0x18}} // com 12
};
static const int nodeCount = sizeof(nodes) / sizeof(nodes[0]);
#endif
// fin du contenu du fichier ID_MAC.h
#endif
/* LES CONNECTEURS */
const int pinLitEtatN = 2; // entrée optocoupleur
//const int pinDCC = 3; // entrée DCC
const int pinLedSignalVerte = 7;
const int pinLedSignalRouge = 6;
const int pinLedSignalOrange = 5;
const int pinTemoinVert = 8;
const int pinTemoinRouge = 10;
const int pinRelaisBM1_N = 1;
/* ANTI-REBOND POLLING */
bool ancienEtatLecture = HIGH;
bool etatStable = HIGH;
unsigned long timerAntiRebond = 0;
const unsigned long tempoAntiRebond = 50;
/* PROTOTYPES */
void printMAC(const uint8_t *mac);
const uint8_t* getMACfromID(uint8_t id);
void setup() {
Serial.begin(230400);
delay(3000);
// "amorçage" du port USB (chatGPT)
//for (int i = 0; i < 10; i++) {
//Serial.println("SYNC...");
// delay(100);
//} // Fin de for (int i = 0; i < 10; i++)
Serial.println(F("start"));
/* =========== Sorties =========== */
pinMode(pinLitEtatN, INPUT_PULLUP);
pinMode(pinLedSignalVerte, OUTPUT);
pinMode(pinLedSignalRouge, OUTPUT);
pinMode(pinLedSignalOrange, OUTPUT);
pinMode(pinTemoinRouge, OUTPUT);
pinMode(pinTemoinVert, OUTPUT);
pinMode(pinRelaisBM1_N, OUTPUT);
/* ===== Nombre d'entrées ===== */
#if IsDebug
Serial.print(F("Nombre de nodes : ")); Serial.println(nodeCount);
/* ===== Affichage table complète ===== */
for (int i = 0; i < nodeCount; i++) {
Serial.print(F("ID ")); Serial.print(nodes[i].id);
Serial.print(F(" -> ")); printMAC(nodes[i].mac);
Serial.println(F(""));
} // Fin de for (int i = 0; i < nodeCount; i++)
#endif
/* ===== MAC depuis ID ===== */
const uint8_t* macTable = getMACfromID(ESP_ID);
if (macTable == nullptr) {
//Serial.println(F("ID non trouvé"));
return;
} // Fin de if (macTable == nullptr)
Serial.print(F("MAC table ID ")); Serial.print(ESP_ID); Serial.print(F(" : ")); printMAC(macTable);
Serial.println(F(""));
/* ===== MAC réelle ESP ===== */
WiFi.mode(WIFI_STA);
uint8_t macHW[6];
WiFi.macAddress(macHW);
Serial.print(F("MAC ESP : ")); printMAC(macHW);
Serial.println(F(""));
/* ===== Comparaison ===== */
bool okMAC = true;
for (int i = 0; i < 6; i++) {
if (macHW[i] != macTable[i]) {
okMAC = false;
break;
} // Fin de if (macHW[i] != macTable[i])
} // Fin de for (int i = 0; i < 6; i++)
if (okMAC) {
Serial.println(F("MAC OK"));
} else {
Serial.println(F("MAC DIFFERENTE"));
while (true) {
// Code bloquant si incohérence des MAC
digitalWrite(pinTemoinRouge, HIGH);
delay(300);
digitalWrite(pinTemoinRouge, LOW);
delay(300);
} // Fin de while (true)
} // Fin de if (okMAC)
// ===== Initialisation état des LED selon l'entrée =====
bool etatInitial = digitalRead(pinLitEtatN);
ancienEtatLecture = etatInitial;
etatStable = etatInitial;
if (etatInitial == LOW) {
// D2 à GND → convoi présent
digitalWrite(pinTemoinRouge, HIGH);
digitalWrite(pinTemoinVert, LOW);
#if IsDebug
Serial.println(F("INIT : OCCUPE"));
#endif
} else {
// D2 au repos
digitalWrite(pinTemoinRouge, LOW);
digitalWrite(pinTemoinVert, HIGH);
#if IsDebug
Serial.println(F("INIT : LIBRE"));
#endif
} // Fin de if (etatInitial == LOW)
} // Fin de setup()
/* =========================
LOOP
========================= */
void loop() {
/* ===== Lecture entrée ===== */
bool lecture = digitalRead(pinLitEtatN);
/* ===== Détection changement brut ===== */
if (lecture != ancienEtatLecture) {
timerAntiRebond = millis();
ancienEtatLecture = lecture;
} // Fin de if (lecture != ancienEtatLecture)
/* ===== Validation état stable ===== */
if ((millis() - timerAntiRebond) > tempoAntiRebond) {
if (lecture != etatStable) {
etatStable = lecture;
if (etatStable == LOW) {
// D2 à GND → convoi présent
digitalWrite(pinTemoinRouge, HIGH);
digitalWrite(pinTemoinVert, LOW);
#if IsDebug
Serial.println(F("OCCUPE"));
#endif
} else {
// D2 au repos
digitalWrite(pinTemoinRouge, LOW);
digitalWrite(pinTemoinVert, HIGH);
#if IsDebug
Serial.println(F("LIBRE"));
#endif
} // Fin de if (etatStable == LOW)
} // Fin de if (lecture != etatStable)
} // Fin de if ((millis() - timerAntiRebond) > tempoAntiRebond)
} // Fin de loop()
/* =========================
AFFICHAGE MAC
========================= */
void printMAC(const uint8_t *mac) {
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) {
Serial.print(F("0"));
} // Fin de if (mac[i] < 16)
Serial.print(mac[i], HEX);
if (i < 5) {
Serial.print(F(":"));
} // Fin de if (i < 5)
} // Fin de for (int i = 0; i < 6; i++)
} // Fin de procédure printMAC(const uint8_t *mac)
/* =========================
RETOUR MAC PAR ID
========================= */
const uint8_t* getMACfromID(uint8_t id) {
for (int i = 0; i < nodeCount; i++) {
if (nodes[i].id == id) {
return nodes[i].mac;
} // Fin de if (nodes[i].id == id)
} // Fin de for (int i = 0; i < nodeCount; i++)
return nullptr;
} // Fin de procédure getMACfromID(uint8_t id)