Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MFRC522 MQTT WIZnetInterface mbed-src
main.cpp
00001 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay 00002 //This code is based on Martin Olejar's MFRC522 library. Minimal changes 00003 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too 00004 00005 //Connect as follows: 00006 //RFID pins -> Nucleo header CN5 (Arduino-compatible header) 00007 //---------------------------------------- 00008 //RFID IRQ=pin5 -> Not used. Leave open 00009 //RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12 00010 //RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11 00011 //RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13 00012 //RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10 00013 //RFID RST=pin7 -> Nucleo =PA_9=D8 00014 //3.3V and Gnd to the respective pins 00015 00016 #include "mbed.h" 00017 #include "MFRC522.h" 00018 #include "SPI.h" 00019 #include "MQTTEthernet.h" 00020 #include "MQTTClient.h" 00021 00022 #define ECHO_SERVER_PORT 7 00023 00024 // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) 00025 //#define MF_RESET D8 00026 00027 #define SPI_MOSI D11 00028 #define SPI_MISO D12 00029 #define SPI_SCK D13 00030 #define SPI_CS D10 00031 #define MF_RESET D9 00032 DigitalOut l1(D4); 00033 DigitalOut l2(D5); 00034 DigitalOut LedGreen(LED1); 00035 00036 //Serial connection to PC for output 00037 //Serial pc(USBTX, USBRX); 00038 00039 MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); 00040 00041 int main(void) { 00042 //pc.printf("starting...\n"); 00043 00044 Serial pc(USBTX, USBRX); 00045 pc.baud(115200); 00046 printf("Wait a second...\r\n"); 00047 char* topic = "openhab/parents/command"; 00048 MQTTEthernet ipstack = MQTTEthernet(); 00049 00050 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); 00051 00052 char* hostname = "172.16.73.4"; 00053 int port = 1883; 00054 00055 int rc = ipstack.connect(hostname, port); 00056 if (rc != 0) 00057 printf("rc from TCP connect is %d\n", rc); 00058 00059 printf("Topic: %s\r\n",topic); 00060 00061 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00062 data.MQTTVersion = 3; 00063 data.clientID.cstring = "parents"; 00064 00065 if ((rc = client.connect(data)) == 0) 00066 printf("rc from MQTT connect is %d\n", rc); 00067 00068 //Init. RC522 Chip 00069 RfChip.PCD_Init(); 00070 00071 while (true) { 00072 //LedGreen = 1; 00073 pc.printf("enterd loop...\n"); 00074 // Look for new cards 00075 if ( ! RfChip.PICC_IsNewCardPresent()) 00076 { 00077 wait_ms(500); 00078 continue; 00079 } 00080 00081 // Select one of the cards 00082 if ( ! RfChip.PICC_ReadCardSerial()) 00083 { 00084 wait_ms(500); 00085 continue; 00086 } 00087 00088 //LedGreen = 0; 00089 char data[20]=""; 00090 char data1[20]=""; 00091 00092 // Print Card UID 00093 pc.printf("Card UID: "); 00094 printf("Size of UID: %d \n",RfChip.uid.size); 00095 for (uint8_t i = 0; i < RfChip.uid.size; i++) 00096 { 00097 char temp[5]; 00098 pc.printf(" %X02", RfChip.uid.uidByte[i]); 00099 sprintf(temp,"%X02", RfChip.uid.uidByte[i]); 00100 strcat(data,temp); 00101 } 00102 00103 /* 00104 pc.printf("\n\r"); 00105 printf("%s\n\r",data); 00106 */ 00107 00108 // Print Card type 00109 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); 00110 pc.printf(" \nPICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); 00111 wait_ms(1000); 00112 00113 if(strcmp(data,"5C02820285024502")==0) 00114 { 00115 strcat(data1,"fruits"); 00116 } 00117 if(strcmp(data,"B3026802A002202")==0) 00118 { 00119 strcat(data1,"vegatables"); 00120 } 00121 if(strcmp(data,"F2029C02AC021F02")==0) 00122 { 00123 strcat(data1,"milk"); 00124 00125 MQTT::Message message; 00126 char buf[100]; 00127 sprintf(buf, "%s", data1); 00128 message.qos = MQTT::QOS0; 00129 message.retained = false; 00130 message.dup = false; 00131 message.payload = (void*)data1; 00132 message.payloadlen = strlen(data1); 00133 rc = client.publish("grocery", message); 00134 client.yield(60000); 00135 memset(data1, '\0',sizeof(data1)); 00136 } 00137 } 00138 }
Generated on Wed Aug 24 2022 12:27:36 by
1.7.2