nfc-tools / libnfc

Platform independent Near Field Communication (NFC) library
http://nfc-tools.org
GNU Lesser General Public License v3.0
1.64k stars 436 forks source link

MRFC522 with painlessMesh - problem with mfrc522.PCD_Init(); (ESP32 DevKitV1) #697

Closed kzkmwa1951 closed 8 months ago

kzkmwa1951 commented 1 year ago

Hello everyone, I have a problem initialising the MFRC522 library. I am writing a program for a node in a mesh network using the painlessMesh library. This node contains an IR beam break sensor with an RFID reader. When there is a low signal on the sensor, the program is to recognise the vehicle by using the RFID reader. Individually, the codes work. When I want to combine them together, strange content pops up on the serial port monitor from which I understand nothing.

Code:

#include <SPI.h>
#include <MFRC522.h>

#include <painlessMesh.h>

#define RST_PIN 22
#define SS_PIN 11

#define SENSORPIN 2

int sensorState = 0, lastState = 0;

#define   MESH_SSID       "essa"
#define   MESH_PASSWORD   "essa"
#define   MESH_PORT       5555

String carName = "";
String nodeName = "Bramka1";
Scheduler userScheduler;
painlessMesh mesh;
MFRC522 mfrc522 (SS_PIN, RST_PIN);

void sendMessage() ; // callback func
Task sendTask( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );

// everthying should be done in callback func
void sendMessage(){

  digitalWrite(SENSORPIN, HIGH); // turn on the pullup

  // Serialize the message
  DynamicJsonDocument doc(1024);
 doc["Sensor"] = "IR_Break_Beam";
 doc["Wezel"] = nodeName;
  if (sensorState == HIGH ) {
   doc["Stan"] = "nie przekroczono"; 
  }
  if (sensorState == LOW){
   doc["Stan"] = "przekroczono"; 
  }
 doc["Pojazd"] = carName;
  String msg ;
  serializeJson(doc, msg); //
  mesh.sendBroadcast( msg );
  Serial.println("");
  Serial.println(msg);

}

void receivedCallback(uint32_t from, String &msg) {
Serial.println("Message ="); Serial.println(msg);
  String json = msg.c_str();;
  DynamicJsonDocument doc(1024);
}
void newConnectionCallback(uint32_t nodeId) {
  // Nowe połączenie
}

void changedConnectionCallback() {
  // Zmiana połączenia
}

void setup() {
  Serial.begin(115200);

  pinMode(SENSORPIN, INPUT);

  mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION);
  mesh.init(MESH_SSID, MESH_PASSWORD, &userScheduler, MESH_PORT);
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.setContainsRoot(true);
  mfrc522.PCD_Init();              // Init MFRC522
  SPI.begin();                             // Init SPI bus
  //delay(4);
  mfrc522.PCD_DumpVersionToSerial();      // Show details of PCD - MFRC522 Card Reader details

  userScheduler.addTask( sendTask );
  sendTask.enable();

}

void loop() {

  /*Show UID for Card/Tag on serial monitor*/
  byte letter;
  String content= "";

  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }

  content.toUpperCase();

  if (content.substring(1) == "DC 60 B6 79") /*UID for the Card/Tag we want to give access Replace with your card UID*/
  {
    carName = "WV";
  }
  //if (content.substring(1) == "76 F4 EF 2B") /*UID for the Card/Tag we want to give access Replace with your card UID*/
  //{
  //  carName = "Audi";
  //}
 else   {
    carName = "nie rozpoznano";
  }
  sensorState = digitalRead(SENSORPIN);

  lastState = sensorState;

  mesh.update();
}

Serial port monitor:

19:06:47.081 -> 19:06:47.081 -> setLogLevel: ERROR | STARTUP | CONNECTION | 19:06:47.081 -> STARTUP: init(): 0 19:06:47.160 -> STARTUP: AP tcp server established on port 5555 19:06:48.094 -> ets Jun 8 2016 00:22:57 19:06:48.094 -> 19:06:48.094 -> rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) 19:06:48.094 -> configsip: 0, SPIWP:0xee 19:06:48.094 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 19:06:48.094 -> mode:DIO, clock div:1 19:06:48.094 -> load:0x3fff0030,len:1184 19:06:48.094 -> load:0x40078000,len:13132 19:06:48.094 -> load:0x40080400,len:3036 19:06:48.094 -> entry 0x400805e4

neomilium commented 8 months ago

Wrong library.