1
+ #include < MFRC522v2.h>
2
+ #include < MFRC522DriverSPI.h>
3
+ // #include <MFRC522DriverI2C.h>
4
+ #include < MFRC522DriverPinSimple.h>
5
+ #include < MFRC522Debug.h>
6
+ #include " EspMQTTClient.h"
7
+
8
+ MFRC522DriverPinSimple ss_pin (5 ); // Configurable, see typical pin layout above.
9
+
10
+ MFRC522DriverSPI driver{ss_pin}; // Create SPI driver.
11
+ // MFRC522DriverI2C driver{}; // Create I2C driver.
12
+ MFRC522 mfrc522{driver}; // Create MFRC522 instance.
13
+
14
+ uint32_t chipId = 0 ;
15
+ EspMQTTClient client (
16
+ " DigitalAGclub" , // WiFi SSID at the maker space
17
+ " username" , // WiFi password
18
+ " 192.168.0.100" // MQTT Broker server IP
19
+ );
20
+
21
+ void onConnectionEstablished ()
22
+ // This function activate when connection is established with the broker
23
+ {
24
+ Serial.printf (" Connection Established\n " );
25
+ }
26
+
27
+ void setup () {
28
+ Serial.begin (115200 ); // Initialize serial communications with the PC for debugging.
29
+ while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4).
30
+ mfrc522.PCD_Init (); // Init MFRC522 board.
31
+ MFRC522Debug::PCD_DumpVersionToSerial (mfrc522, Serial); // Show details of PCD - MFRC522 Card Reader details.
32
+ mfrc522.PCD_SetAntennaGain (0x70 );
33
+ Serial.println (F (" Scan PICC to see UID, SAK, type, and data blocks..." ));
34
+ Serial.print (mfrc522.PCD_GetAntennaGain ());
35
+ client.setWifiReconnectionAttemptDelay (5000 );
36
+ }
37
+
38
+ void loop () {
39
+ client.loop ();
40
+ if (!client.isConnected ()) {
41
+ Serial.printf (" WiFi Connection: %d\n " , client.isWifiConnected ());
42
+ Serial.printf (" MQTT Connection: %d\n " , client.isMqttConnected ());
43
+ delay (1000 );
44
+ }
45
+
46
+
47
+ // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
48
+ if ( !mfrc522.PICC_IsNewCardPresent ()) {
49
+ return ;
50
+ }
51
+
52
+ // Select one of the cards.
53
+ if ( !mfrc522.PICC_ReadCardSerial ()) {
54
+ return ;
55
+ }
56
+
57
+ // Dump debug info about the card; PICC_HaltA() is automatically called.
58
+ // MFRC522Debug::PICC_DumpToSerial(mfrc522, Serial, &(mfrc522.uid));
59
+
60
+ Serial.print (F (" Card UID:" ));
61
+ dump_byte_array (mfrc522.uid .uidByte , mfrc522.uid .size );
62
+ Serial.println ();
63
+ delay (1000 );
64
+
65
+ }
66
+
67
+ /* *
68
+ * Helper routine to dump a byte array as hex values to Serial.
69
+ */
70
+ void dump_byte_array (byte *buffer, byte bufferSize) {
71
+ for (byte i = 0 ; i < bufferSize; i++) {
72
+ Serial.print (buffer[i] < 0x10 ? " 0" : " " );
73
+ Serial.print (buffer[i], HEX);
74
+ }
75
+
76
+ client.publish (" purdue-dac/carrot" , " Hello" , false );
77
+ }
0 commit comments