Skip to content

Commit 75cd400

Browse files
authored
Update for uid support
1 parent 7065021 commit 75cd400

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

TapToEsp32.ino

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ NfcAdapter nfc = NfcAdapter(&mfrc522);
1616
TapToLaunchApi client;
1717
AudioOutputI2S* out;
1818
boolean wifiEnabled = false;
19+
String lastId="";
20+
int idLoop = 0;
1921

2022
void setup() {
2123
Serial.begin(115200);
@@ -130,6 +132,23 @@ bool sendTapTo(String& gamePath){
130132
return code == 0;
131133
}
132134

135+
bool sendTapToUid(String& uid){
136+
if(!wifiEnabled) return true;
137+
int code = client.launchUid(uid);
138+
if(code > 0){
139+
expressError(code);
140+
}
141+
return code == 0;
142+
}
143+
144+
void successActions(){
145+
launchLedOn(0);
146+
motorOn(0);
147+
playAudio();
148+
motorOff(0);
149+
launchLedOff();
150+
}
151+
133152
void initWiFi() {
134153
WiFi.mode(WIFI_STA);
135154
WiFi.begin(ssid, password);
@@ -150,31 +169,50 @@ void initWiFi() {
150169
}
151170

152171
void loop(void) {
153-
if (nfc.tagPresent()) {
154-
NfcTag tag = nfc.read();
155-
if(tag.hasNdefMessage()){
156-
NdefMessage message = tag.getNdefMessage();
157-
int recordCount = message.getRecordCount();
158-
NdefRecord record = message.getRecord(0);
159-
int payloadLength = record.getPayloadLength();
160-
const byte *payload = record.getPayload();
161-
String payloadAsString = "";
162-
for (int i = 3; i < payloadLength; i++) {
163-
payloadAsString += (char)payload[i];
164-
}
165-
if(!payloadAsString.equalsIgnoreCase("")){
166-
if(sendTapTo(payloadAsString)){
167-
Serial.print("SCAN\t" + payloadAsString + "\n");
168-
Serial.flush();
169-
launchLedOn(0);
170-
motorOn(0);
171-
playAudio();
172-
motorOff(0);
173-
launchLedOff();
174-
}
175-
nfc.haltTag();
176-
delay(1000);
177-
}
172+
if (nfc.tagPresent()) {
173+
NfcTag tag = nfc.read();
174+
String id = tag.getUidString();
175+
if(id.equalsIgnoreCase(lastId)){
176+
idLoop++;
177+
}
178+
lastId = id;
179+
bool foundMessage = false;
180+
if(tag.hasNdefMessage()){
181+
NdefMessage message = tag.getNdefMessage();
182+
int recordCount = message.getRecordCount();
183+
NdefRecord record = message.getRecord(0);
184+
int payloadLength = record.getPayloadLength();
185+
const byte *payload = record.getPayload();
186+
String payloadAsString = "";
187+
for (int i = 3; i < payloadLength; i++) {
188+
payloadAsString += (char)payload[i];
189+
}
190+
foundMessage = !payloadAsString.equalsIgnoreCase("");
191+
if(foundMessage){
192+
if(sendTapTo(payloadAsString)){
193+
Serial.print("SCAN\t" + payloadAsString + "\n");
194+
Serial.flush();
195+
successActions();
196+
}
197+
nfc.haltTag();
198+
delay(900);
199+
}
200+
}
201+
if(!foundMessage){
202+
if(idLoop > 3){
203+
String toSend = id;
204+
toSend.replace(" ", "");
205+
toSend.toLowerCase();
206+
if(sendTapToUid(toSend)){
207+
Serial.print("SCAN\tuid=" + toSend+ "\n");
208+
Serial.flush();
209+
successActions();
210+
idLoop = 0;
211+
delay(900);
178212
}
213+
nfc.haltTag();
179214
}
215+
}
216+
}
217+
delay(100);
180218
}

0 commit comments

Comments
 (0)