@@ -16,6 +16,8 @@ NfcAdapter nfc = NfcAdapter(&mfrc522);
16
16
TapToLaunchApi client;
17
17
AudioOutputI2S* out;
18
18
boolean wifiEnabled = false ;
19
+ String lastId=" " ;
20
+ int idLoop = 0 ;
19
21
20
22
void setup () {
21
23
Serial.begin (115200 );
@@ -130,6 +132,23 @@ bool sendTapTo(String& gamePath){
130
132
return code == 0 ;
131
133
}
132
134
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
+
133
152
void initWiFi () {
134
153
WiFi.mode (WIFI_STA);
135
154
WiFi.begin (ssid, password);
@@ -150,31 +169,50 @@ void initWiFi() {
150
169
}
151
170
152
171
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\t uid=" + toSend+ " \n " );
208
+ Serial.flush ();
209
+ successActions ();
210
+ idLoop = 0 ;
211
+ delay (900 );
178
212
}
213
+ nfc.haltTag ();
179
214
}
215
+ }
216
+ }
217
+ delay (100 );
180
218
}
0 commit comments