-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP32.ino
More file actions
284 lines (249 loc) · 8.77 KB
/
Copy pathESP32.ino
File metadata and controls
284 lines (249 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Adafruit_NeoPixel.h>
/* ======== Config WiFi ======== */
const char* ssid = "AutoNet";
const char* password = "12345678";
WiFiUDP udp;
const int UDP_PORT = 4210;
IPAddress broadcastIP(192, 168, 4, 255);
/* ======== RC522 (lector NFC) ======== */
#define SS_PIN 5
#define RST_PIN 22
MFRC522 rfid(SS_PIN, RST_PIN);
/* ======== Pines ======== */
const int PIN_TO_GIGA = 25;
#define LED_PIN 32
#define NUM_LEDS 11 // 🔹 Ahora 10 LEDs en la banda
Adafruit_NeoPixel tira(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
/* ======== UIDs de tarjetas ======== */
const byte UID_S1_OPEN[4] = {0x1B, 0x6B, 0x6A, 0x7E};
const byte UID_S1_CLOSE[4] = {0x3B, 0xF5, 0x7D, 0x7E};
const byte UID_S2_OPEN[4] = {0x1B, 0x43, 0x6B, 0x7E};
const byte UID_S2_CLOSE[4] = {0x49, 0x4B, 0xF6, 0x03};
const byte UID_LIGHTS[4] = {0x4B, 0x99, 0x73, 0x7E};
/* ======== Dodano======== */
// Щоб дізнатись свої коди: прикладай картку до зчитувача і дивись Serial Monitor
const byte UID_C1[4] = {0x74, 0x4F, 0x04, 0x5B}; // картка 1 — вимкнути ЛІВИЙ датчик
const byte UID_C2[4] = {0xA4, 0x55, 0xFE, 0x5B}; // картка 2 — вимкнути ОБИДВА + їхати прямо
const byte UID_C3[4] = {0x8B, 0xBC, 0x90, 0x54}; // картка 3 — увімкнути ЛІВИЙ датчик
const byte UID_C4[4] = {0x8B, 0x5B, 0x25, 0x54}; // картка 4 — увімкнути ОБИДВА датчики
const byte UID_C5[4] = {0xAB, 0x2C, 0x54, 0x54}; // картка 5 — вимкнути ПРАВИЙ датчик
const byte UID_C6[4] = {0xAB, 0x8D, 0x1C, 0x54}; // картка 6 — вимкнути ОБИДВА + їхати прямо
const byte UID_C7[4] = {0x9B, 0x66, 0xA9, 0x54}; // картка 7 — увімкнути ПРАВИЙ датчик
const byte UID_C8[4] = {0x9B, 0x87, 0xF1, 0x54}; // картка 8 — увімкнути ОБИДВА датчики
/*------------------*/
/* ======== Variables ======== */
unsigned long lastCheck = 0;
const unsigned long CHECK_INTERVAL_MS = 500;
bool waitingForDoor = false;
unsigned long doorWaitStart = 0;
/* ======== Secuencia de LEDs (barrido progresivo) ======== */
unsigned long lastLedChange = 0;
const unsigned long LED_INTERVAL_MS = 50; // velocidad del barrido
int ledActual = 0;
bool apagando = false;
/* ======== Funciones auxiliares ======== */
bool compareUID(const byte* uidA, const byte* uidB, byte size) {
for (byte i = 0; i < size; i++) {
if (uidA[i] != uidB[i]) return false;
}
return true;
}
void sendUDP(const String& msg) {
udp.beginPacket(broadcastIP, UDP_PORT);
udp.print(msg);
udp.endPacket();
Serial.print("📤 Enviado UDP → ");
Serial.println(msg);
}
/* ======== Actualizador de LEDs ======== */
void actualizarLeds() {
unsigned long ahora = millis();
if (ahora - lastLedChange >= LED_INTERVAL_MS) {
lastLedChange = ahora;
if (!apagando) {
// Encender el siguiente LED
tira.setPixelColor(ledActual, tira.Color(255, 255, 255)); // rojo
tira.show();
ledActual++;
// Si ya se encendieron todos → cambiar a fase de apagado
if (ledActual >= NUM_LEDS) {
apagando = true;
ledActual = NUM_LEDS - 1;
}
} else {
// Apagar todos los LEDs y reiniciar ciclo
tira.clear();
tira.show();
apagando = false;
ledActual = 0;
}
}
}
/* ==================== SETUP ==================== */
void setup() {
Serial.begin(115200);
delay(200);
Serial.println("\n🚗 Iniciando ESP32_inCar...");
Serial.println("====================================");
Serial2.begin(115200);
Serial.println("UART a Arduino listo");
pinMode(PIN_TO_GIGA, OUTPUT);
digitalWrite(PIN_TO_GIGA, HIGH);
Serial.println("✅ Pin hacia GIGA inicializado en HIGH (marcha)");
tira.begin();
tira.show();
Serial.println("✅ Banda LED inicializada");
SPI.begin();
rfid.PCD_Init();
delay(100);
byte v = rfid.PCD_ReadRegister(MFRC522::VersionReg);
if (v == 0x00 || v == 0xFF) {
Serial.println("❌ ERROR: No se detecta el lector RC522. Verifica conexiones SPI.");
while (true) delay(1000);
} else {
Serial.print("✅ RC522 detectado (versión 0x");
Serial.print(v, HEX);
Serial.println(")");
}
WiFi.begin(ssid, password);
Serial.print("🌐 Conectando a WiFi ");
Serial.print(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(".");
}
Serial.println("\n✅ Conectado a WiFi");
Serial.print("📡 IP local: "); Serial.println(WiFi.localIP());
udp.begin(UDP_PORT);
Serial.println("📡 Escuchando UDP...");
Serial.println("====================================");
}
/* ==================== LOOP ==================== */
void loop() {
unsigned long now = millis();
// 🔹 Secuencia de LEDs
actualizarLeds();
// --- RFID ---
if (!waitingForDoor && (now - lastCheck > CHECK_INTERVAL_MS)) {
lastCheck = now;
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.print("💳 Tarjeta detectada: ");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i], HEX);
Serial.print(" ");
}
Serial.println();
String msg = "";
bool detenerCoche = false;
if (compareUID(rfid.uid.uidByte, UID_S1_OPEN, 4)) {
msg = "abre puerta 1"; detenerCoche = true;
}
else if (compareUID(rfid.uid.uidByte, UID_S1_CLOSE, 4)) {
msg = "cierra puerta 1";
}
else if (compareUID(rfid.uid.uidByte, UID_S2_OPEN, 4)) {
msg = "abre puerta 2"; detenerCoche = true;
}
else if (compareUID(rfid.uid.uidByte, UID_S2_CLOSE, 4)) {
msg = "cierra puerta 2";
}
else if (compareUID(rfid.uid.uidByte, UID_LIGHTS, 4)) {
msg = "luces";
// animación azul corta
tira.fill(tira.Color(0, 0, 255));
tira.show();
delay(500);
tira.clear();
tira.show();
} else if (compareUID(rfid.uid.uidByte, UID_C1, 4)) {
Serial.println("Hall: LEFT OFF");
Serial2.println("sensor left off");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C2, 4)) {
Serial.println("Hall: BOTH OFF");
Serial2.println("sensor both off");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C3, 4)) {
Serial.println("Hall: LEFT ON");
Serial2.println("sensor left on");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C4, 4)) {
Serial.println("Hall: BOTH ON");
Serial2.println("sensor both on");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C5, 4)) {
Serial.println("Hall: RIGHT OFF");
Serial2.println("sensor right off");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C6, 4)) {
Serial.println("Hall: BOTH OFF");
Serial2.println("sensor both off");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C7, 4)) {
Serial.println("Hall: RIGHT ON");
Serial2.println("sensor right on");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
} else if (compareUID(rfid.uid.uidByte, UID_C8, 4)) {
Serial.println("Hall: BOTH ON");
Serial2.println("sensor both on");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
}
else {
Serial.println("🚫 Tarjeta no registrada");
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
}
if (detenerCoche) {
digitalWrite(PIN_TO_GIGA, LOW);
delay(5000);
digitalWrite(PIN_TO_GIGA, HIGH);
}
sendUDP(msg);
waitingForDoor = true;
doorWaitStart = now;
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
}
// --- UDP ---
int packetSize = udp.parsePacket();
if (packetSize) {
char buffer[256];
int len = udp.read(buffer, sizeof(buffer) - 1);
if (len > 0) buffer[len] = '\0';
String msg = String(buffer);
Serial.println("📨 Mensaje UDP recibido: " + msg);
if (msg.equalsIgnoreCase("parar coche")) {
digitalWrite(PIN_TO_GIGA, LOW);
waitingForDoor = false;
} else if (msg.equalsIgnoreCase("reanudar coche")) {
digitalWrite(PIN_TO_GIGA, HIGH);
waitingForDoor = false;
}
}
// --- Timeout ---
if (waitingForDoor && (now - doorWaitStart >= 5000)) {
digitalWrite(PIN_TO_GIGA, HIGH);
waitingForDoor = false;
}
}