#include <Arduino.h>
#include <WiFi.h>
#define ESP_ID 1
#define IsDebug 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
/* ============================
CONFIG
============================ */
const int pinLitEtatN = 2; // entrée optocoupleur
//const int pinDCC = 3; // entrée DCC
const int pinLedSignalVerte = 4;
const int pinLedSignalRouge = 5;
const int pinLedSignalOrange= 6;
const int pinTemoinVert = 7;
const int pinTemoinRouge = 8;
const int pinRelaisBM1_N = 9;
volatile bool detecteurDeclenche = false;
void printMAC(const uint8_t *mac) {
/* =========================
AFFICHAGE MAC
========================= */
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) Serial.print("0");
Serial.print(mac[i], HEX);
if (i < 5) {Serial.print(":");}
} // 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)
/* ======================== Interruption sur D2 ======================== */
void IRAM_ATTR onDetecteur() {
detecteurDeclenche = true;
} // Fin de procédure IRAM_ATTR onDetecteur()
/* =========================
SETUP
========================= */
void setup() {
Serial.begin(115200);
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);
attachInterrupt(digitalPinToInterrupt(pinLitEtatN), onDetecteur, CHANGE);
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("Nombre de nodes : ");
Serial.println(nodeCount);
/* ===== Affichage table complète ===== */
for (int i = 0; i < nodeCount; i++) {
Serial.print("ID ");
Serial.print(nodes[i].id);
Serial.print(" -> ");
printMAC(nodes[i].mac);
Serial.println();
} // 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("ID non trouve");
return;
} // Fin de if (macTable == nullptr)
Serial.print("MAC table ID ");
Serial.print(ESP_ID);
Serial.print(" : ");
printMAC(macTable);
Serial.println();
/* ===== MAC réelle ESP ===== */
WiFi.mode(WIFI_STA);
uint8_t macHW[6];
WiFi.macAddress(macHW);
Serial.print("MAC ESP : ");
printMAC(macHW);
Serial.println();
/* ===== 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("MAC OK");
} else {
Serial.println("MAC DIFFERENTE");
while (true) {
// Code bloquant si incohérence des MAC
digitalWrite(pinTemoinRouge, HIGH);
delay(300);
digitalWrite(pinTemoinRouge, LOW);
delay(300);
}
} // Fin de if (okMAC)
// ===== Initialisation état des LED selon l'entrée =====
bool etatInitial = digitalRead(pinLitEtatN);
if (etatInitial == LOW) {
// D2 à GND → convoi présent
digitalWrite(pinTemoinRouge, HIGH);
digitalWrite(pinTemoinVert, LOW);
#if IsDebug
Serial.println("INIT : OCCUPE");
#endif
} else {
// D2 au repos
digitalWrite(pinTemoinRouge, LOW);
digitalWrite(pinTemoinVert, HIGH);
#if IsDebug
Serial.println("INIT : LIBRE");
#endif
} // Fin de if (etatInitial == LOW)
} // Fin de setup()
/* =========================
LOOP
========================= */
void loop() {
if (detecteurDeclenche) {
detecteurDeclenche = false;
bool etat = digitalRead(pinLitEtatN);
if (etat == LOW) {
// D2 à GND → convoi présent
digitalWrite(pinTemoinRouge, HIGH);
digitalWrite(pinTemoinVert, LOW);
#if IsDebug
Serial.println("OCCUPE");
#endif
} else {
// D2 au repos
digitalWrite(pinTemoinRouge, LOW);
digitalWrite(pinTemoinVert, HIGH);
#if IsDebug
Serial.println("LIBRE");
#endif
} // Fin de if (etat == LOW)
} // Fin de if (detecteurDeclenche)
} // Fin de loop()Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1