ggfd

Dependencies:   MQTT TextLCD WIZnetInterface mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 "MQTTEthernet.h"
00019 #include "MQTTClient.h"
00020 #include "TextLCD.h"
00021 #include <sstream>
00022 #include <string>
00023 #define ECHO_SERVER_PORT   7
00024 #define SPI_MOSI    D11
00025 #define SPI_MISO    D12
00026 #define SPI_SCLK    D13
00027 #define SPI_CS      D10
00028 
00029 // WIZWiki-W7500 Pin for MFRC522 reset(pick another D pin if you need D8)
00030 #define MF_RESET    D9
00031 
00032 TextLCD lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7
00033 
00034 Serial out(USBTX,USBRX);
00035 int arrivedcount = 0;
00036 char RFIDTagMessage[10];
00037 
00038 /*void messageArrived(MQTT::MessageData& md)
00039 {
00040     MQTT::Message &message = md.message;
00041     printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
00042     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
00043     ++arrivedcount;
00044 }*/
00045 
00046 void baud(int baudrate)
00047 {
00048     Serial s(USBTX, USBRX);
00049     s.baud(baudrate);
00050 }
00051 
00052 
00053 MFRC522    RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);        // Init MFRC522 card
00054 
00055 DigitalOut LedGreen(D0);
00056 DigitalOut LedRed(D1);
00057 //DigitalOut Buzzer(D4);
00058 char* store_buf;
00059 
00060 int main(void)
00061 {
00062     printf("starting...\n");
00063 
00064     // Init. RC522 Chip
00065     RfChip.PCD_Init();
00066     MQTTEthernet ipstack = MQTTEthernet();
00067 
00068 
00069     while (true) {
00070         // lcd.locate(4,0);
00071       //  lcd.printf(" CDI Attendance\n");
00072         // lcd.locate(4,1);
00073      //   lcd.printf(" System\n");
00074         // Look for new cards
00075         if ( ! RfChip.PICC_IsNewCardPresent()) {
00076             wait_ms(50);
00077             continue;
00078         }
00079 
00080         // Select one of the cards
00081         if ( ! RfChip.PICC_ReadCardSerial()) {
00082             wait_ms(50);
00083             continue;
00084         }
00085 
00086   //      lcd.cls();
00087  //       wait_ms(50);
00088 
00089         //Size
00090 
00091         printf("Size: %d \n",RfChip.uid.size);
00092         printf("\n\r");
00093         // Print Card UID
00094         printf("Card UID: ");
00095         for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00096             printf(" %X", RfChip.uid.uidByte[i]);
00097             store_buf+= sprintf(store_buf,"%X",RfChip.uid.uidByte[i]);
00098         }
00099         printf("\n\r");
00100         store_buf=RFIDTagMessage;
00101         printf("Card ID: %s",store_buf);
00102 
00103      /*   if(store_buf[0]!='\0') {
00104 
00105 
00106             MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
00107 
00108             char* hostname = "172.16.73.4";
00109             int port = 1883;
00110 
00111             int rc = ipstack.connect(hostname, port);
00112 
00113             out.printf("rc from TCP connect is %d\n", rc);
00114 
00115             if (rc != 0)
00116                 out.printf("rc from TCP connect is %d\n", rc);
00117 
00118             char MQTTClientID[30];
00119 
00120             MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00121             data.MQTTVersion = 3;
00122             sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000);
00123             data.clientID.cstring = MQTTClientID;
00124      */       /*   data.username.cstring = "testuser";
00125                data.password.cstring = "testpassword"; */
00126 
00127        /*     if ((rc = client.connect(data)) != 0)
00128                 printf("rc from MQTT connect is %d\n", rc);
00129 
00130             MQTT::Message message;
00131 
00132             message.qos = MQTT::QOS0;
00133             message.retained = false;
00134             message.dup = false;
00135 
00136             message.payload = (void*)RFIDTagMessage;
00137             message.payloadlen = strlen(RFIDTagMessage);
00138 
00139             rc = client.publish("cdi/employee/attendance", message);
00140             printf("Rc result: % d ",rc);
00141             if(rc!=0) {
00142        */         // lcd.printf(" FAILED..! TRY AGAIN!\n");
00143          /*       lcd.locate(4,0);
00144                 lcd.printf("FAILED.!\n");
00145                 lcd.locate(2,1);
00146                 lcd.printf("TRY AGAIN..\n");
00147 
00148                 LedRed=1;
00149 
00150                 LedGreen = 0;
00151                 wait_ms(3000);
00152                 LedRed=0;
00153 
00154                 lcd.cls();
00155                 // NVIC_SystemReset();
00156             } else {
00157                 lcd.printf(" USER VERIFIED!\n");
00158                 LedGreen = 1;
00159                 wait_ms(2000);
00160                 LedGreen = 0;
00161                 lcd.cls();
00162             }*/
00163     //    }
00164 
00165 
00166 
00167         // Print Card type
00168         uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00169         printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00170         wait_ms(50);
00171 
00172     }
00173 }