mirror of
https://gitlab.com/fabinfra/fabhardware/fabreader.git
synced 2025-03-12 22:51:43 +01:00
Test NFC Connection
This commit is contained in:
parent
844f04ec6a
commit
72c052fc59
5
src/FabReader_v2/.vscode/settings.json
vendored
Normal file
5
src/FabReader_v2/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"random": "cpp"
|
||||
}
|
||||
}
|
@ -13,23 +13,39 @@ NFC::NFC(int pin_ss, int pin_rst)
|
||||
|
||||
bool NFC::hasNewCard()
|
||||
{
|
||||
return rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial() && rfid->uid.sak == 0x20;
|
||||
if(rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial() && rfid->uid.sak == 0x20)
|
||||
{
|
||||
byte atqaLen = sizeof(atqa);
|
||||
MFRC522::StatusCode status = rfid->PICC_RequestA(atqa, &atqaLen);
|
||||
Serial.printf("PICC_RequestA: 0x%02x\n", status);
|
||||
if(atqa[0] == 0x44)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void NFC::selectCard()
|
||||
{
|
||||
rfid->PICC_Select(&(rfid->uid));
|
||||
MFRC522::StatusCode status = rfid->PICC_Select(&(rfid->uid));
|
||||
Serial.printf("PICC_Select: 0x%02x\n", status);
|
||||
uid = rfid->uid;
|
||||
cardSelected = true;
|
||||
Serial.println("Card Selected");
|
||||
}
|
||||
void NFC::deselectCard()
|
||||
{
|
||||
rfid->PICC_HaltA();
|
||||
MFRC522::StatusCode status = rfid->PICC_HaltA();
|
||||
Serial.printf("PICC_HaltA: 0x%02x\n", status);
|
||||
cardSelected = false;
|
||||
}
|
||||
|
||||
bool NFC::isCardLost()
|
||||
{
|
||||
Serial.println(rfid->PICC_ReadCardSerial());
|
||||
Serial.println(rfid->PICC_ReadCardSerial());
|
||||
Serial.println(rfid->PICC_ReadCardSerial());
|
||||
return rfid->PICC_ReadCardSerial() && rfid->uid.uidByte != uid.uidByte;
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,13 @@
|
||||
class NFC
|
||||
{
|
||||
private:
|
||||
MFRC522* rfid;
|
||||
|
||||
MFRC522::Uid uid;
|
||||
bool cardSelected;
|
||||
byte atqa[2];
|
||||
|
||||
public:
|
||||
MFRC522* rfid;
|
||||
NFC(int pin_ss, int pin_rst);
|
||||
bool hasNewCard();
|
||||
void selectCard();
|
||||
|
@ -21,7 +21,7 @@ void OTAProxy::startOTA()
|
||||
|
||||
char topic[] = "fabreader/00000/startOTA";
|
||||
sprintf(topic, "fabreader/%05d/startOTA", id);
|
||||
mqtt->publish(topic, "");
|
||||
mqtt->publish(topic, 0);
|
||||
}
|
||||
|
||||
void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
|
||||
|
@ -9,7 +9,7 @@
|
||||
class OTAProxy
|
||||
{
|
||||
private:
|
||||
bool activeOTA;
|
||||
bool activeOTA = false;
|
||||
PubSubClient* mqtt;
|
||||
NFC* nfc;
|
||||
int id;
|
||||
|
@ -25,7 +25,7 @@ OTAProxy* ota;
|
||||
void setup_wifi()
|
||||
{
|
||||
delay(10);
|
||||
Serial.println("Connecting Wifi");
|
||||
Serial.println("Connecting Wifi ...");
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(config.WLAN_SSID, config.WLAN_Password);
|
||||
@ -45,17 +45,22 @@ void reconnect()
|
||||
{
|
||||
String clientId = "FabReader_";
|
||||
clientId += String(config.ID);
|
||||
|
||||
Serial.println("Connecting MQTT ...");
|
||||
if (mqtt->connect(clientId.c_str()))
|
||||
{
|
||||
Serial.println("connected");
|
||||
Serial.println("MQTT connected");
|
||||
char id[6] = "00000";
|
||||
sprintf(id, "%05d", config.ID);
|
||||
mqtt->publish("fabreader", id);
|
||||
|
||||
char topic[] = "fabreader/00000/#";
|
||||
sprintf(topic, "fabreader/%05d/#", config.ID);
|
||||
char topic_requestOTA[] = "fabreader/00000/requestOTA";
|
||||
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", config.ID);
|
||||
mqtt->subscribe(topic_requestOTA);
|
||||
|
||||
mqtt->subscribe(topic);
|
||||
char topic_stopOTA[] = "fabreader/00000/stopOTA";
|
||||
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", config.ID);
|
||||
mqtt->subscribe(topic_stopOTA);
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(mqtt->state());
|
||||
@ -67,8 +72,10 @@ void reconnect()
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
Serial.println("Recive Message");
|
||||
if(ota->hasActiveOTA())
|
||||
{
|
||||
Serial.println("Continue OTA");
|
||||
ota->continueOTA(topic, payload, length);
|
||||
}
|
||||
}
|
||||
@ -89,8 +96,9 @@ void setup()
|
||||
pinMode(PIN_ETH_SPI_SS, OUTPUT);
|
||||
digitalWrite(PIN_ETH_SPI_SS, HIGH);
|
||||
|
||||
Serial.println("NFC ...");
|
||||
Serial.println("Connecting NFC ...");
|
||||
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
|
||||
Serial.println("NFC connected");
|
||||
|
||||
setup_wifi();
|
||||
|
||||
@ -111,10 +119,12 @@ void loop()
|
||||
|
||||
if(!ota->hasActiveOTA() && nfc->hasNewCard())
|
||||
{
|
||||
Serial.println("Start OTA");
|
||||
ota->startOTA();
|
||||
}
|
||||
if(ota->hasActiveOTA() && nfc->isCardLost())
|
||||
{
|
||||
Serial.println("Cancel OTA");
|
||||
ota->cancelOTA();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user