I have a simple script. nothing much to say about it it just sends serial data by "Serial.print".
I plug the board into my computer and open a serial monitor program. It doesnt work until I press the reset button on the board, then it gets the serial data. WTH? I wanna built a device that's installed permanently and I don't wanna press the reset button everytime it gets powered on. On Arduino that works just fine.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN D8
#define RST_PIN D3
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup() {
pinMode(D1, OUTPUT);
digitalWrite(D1, LOW);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
rfid.PCD_SetAntennaGain(rfid.RxGain_max);
Serial.println("B");
digitalWrite(D1, HIGH);
}
void loop() {
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
char str[32] = "";
array_to_string(rfid.uid.uidByte, 4, str);
Serial.println(str);
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void array_to_string(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}