Answer the question
In order to leave comments, you need to log in
Why does ESP32 freeze with RC522?
I made a reader for ACS (it is spinning on the server) using ESP32 and RC522. Everything works as it should, only occasionally the device freezes (it can work for several days, and then freeze, and sometimes it freezes several times a day), does not respond to RFID tags. At the same time, the Wi-Fi connection remains active in the router. Before that, I assembled the same device on Arduino + W550 + RC522 - the symptoms are exactly the same, everything works, but periodically hangs. I tried several instances of controllers and sensors, with all such a story. I suspect there is some problem with the RC522. Tell me, please, how can I solve this problem?
Here is the code:
#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include "esp32-hal-ledc.h"
#define RST_PIN 22 // Configurable, see typical pin layout above
#define SS_PIN 21 // Configurable, see typical pin layout above
const char* ssid = "TP-LINK";
const char* password = "password";
const char* host = "site.com";
const char* idBranch = "1";
const char* rfid = "123";
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
unsigned long uidDec, uidDecTemp; // для храниения номера метки в десятичном формате
//Реле
int in1 = 5;
//Синий. Готовность к работе.
int inputReady = 13;
//Зеленый. Есть доступ.
int inputAccessAllowed = 2;
//Красный. Нет доступа.
int inputAccessDenied = 4;
//Динамик
int buzzer = 12;
void setup() {
SPI.begin(); // инициализация SPI / Init SPI bus.
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
mfrc522.PCD_Init(); // инициализация MFRC522 / Init MFRC522 card.
//Реле
pinMode(in1, OUTPUT);
digitalWrite(in1, LOW);
//Синий. Готовность к работе.
pinMode(inputReady, OUTPUT);
//Зеленый. Есть доступ.
pinMode(inputAccessAllowed, OUTPUT);
//Красный. Нет доступа.
pinMode(inputAccessDenied, OUTPUT);
//Динамик
pinMode(buzzer, OUTPUT);
ledcSetup(0, 50, 8);
ledcAttachPin(buzzer, 0);
digitalWrite(inputReady, HIGH);
}
void loop() {
//Если Wi-Fi отвалился, то перезагружаемся
if (WiFi.status() != WL_CONNECTED) {
ESP.restart();
}
// Поиск метки
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Считывание метки
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
uidDec = 0;
// Выдача серийного номера метки.
for (byte i = (mfrc522.uid.size); i > 0; i--)
{
uidDecTemp = mfrc522.uid.uidByte[i-1];
uidDec = uidDec * 256 + uidDecTemp;
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
return;
}
String url = "/person/visit";
url += "?id=";
url += idBranch;
url += "&rfid=";
url += uidDec;
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
client.stop();
return;
}
}
String line;
while(client.available()) {
line = client.readStringUntil('\n');
}
if(line=="1"){
digitalWrite(in1, HIGH);
digitalWrite(inputAccessAllowed, HIGH);
delay(500);
digitalWrite(in1, LOW);
digitalWrite(inputAccessAllowed, LOW);
}
else{
digitalWrite(inputAccessDenied, HIGH);
ledcWrite(0, 255);
//Чтоб часто не слать
if(uidDec){
delay(1000);
}
digitalWrite(inputAccessDenied, LOW);
ledcWrite(0, 0);
}
client.stop();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question