ntg

Dependencies:   MQTT_attendance 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        // printf("starting LCD initi ..\n");
00071 
00072         lcd.locate(0,0);
00073         lcd.printf("Attendance\n");
00074         lcd.locate(0,1);
00075         lcd.printf(" System\n");
00076         // Look for new cards
00077         if ( ! RfChip.PICC_IsNewCardPresent()) {
00078             wait_ms(50);
00079             continue;
00080         }
00081 
00082         // Select one of the cards
00083         if ( ! RfChip.PICC_ReadCardSerial()) {
00084             wait_ms(50);
00085             continue;
00086         }
00087 
00088         lcd.cls();
00089         wait_ms(50);
00090 
00091         //Size
00092 
00093         printf("Size: %d \n",RfChip.uid.size);
00094         printf("\n\r");
00095         // Print Card UID
00096         printf("Card UID: ");
00097         for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00098             printf(" %X", RfChip.uid.uidByte[i]);
00099             store_buf+= sprintf(store_buf,"%X",RfChip.uid.uidByte[i]);
00100         }
00101         printf("\n\r");
00102         store_buf=RFIDTagMessage;
00103         printf("Card ID: %s",store_buf);
00104  
00105          if(store_buf[0]!='\0') {
00106 
00107 
00108             MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
00109 
00110             char* hostname = "172.16.73.4";
00111             int port = 1883;
00112 
00113             int rc = ipstack.connect(hostname, port);
00114 
00115             out.printf("rc from TCP connect is %d\n", rc);
00116 
00117             if (rc != 0)
00118                 out.printf("rc from TCP connect is %d\n", rc);
00119 
00120             char MQTTClientID[30];
00121 
00122             MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00123             data.MQTTVersion = 3;
00124             sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000);
00125             data.clientID.cstring = MQTTClientID;
00126             /*   data.username.cstring = "testuser";
00127                data.password.cstring = "testpassword"; */
00128 
00129             if ((rc = client.connect(data)) != 0)
00130                 printf("rc from MQTT connect is %d\n", rc);
00131 
00132             MQTT::Message message;
00133 
00134             message.qos = MQTT::QOS0;
00135             message.retained = false;
00136             message.dup = false;
00137 
00138             message.payload = (void*)RFIDTagMessage;
00139             message.payloadlen = strlen(RFIDTagMessage);
00140 
00141             rc = client.publish("my/attendance", message);
00142             printf("Rc result: % d ",rc);
00143             if(rc!=0) {
00144             lcd.printf(" FAILED..! TRY AGAIN!\n");
00145                 lcd.locate(4,0);
00146                 lcd.printf("FAILED.!\n");
00147                 lcd.locate(2,1);
00148                 lcd.printf("TRY AGAIN..\n");
00149 
00150                 LedRed=1;
00151 
00152                 LedGreen = 0;
00153                 wait_ms(3000);
00154                 LedRed=0;
00155 
00156                 lcd.cls();
00157                 // NVIC_SystemReset();
00158             } else {
00159                 lcd.printf(" USER VERIFIED!\n");
00160                 LedGreen = 1;
00161                 wait_ms(2000);
00162                 LedGreen = 0;
00163                 lcd.cls();
00164             }
00165         }
00166 
00167 
00168 
00169         // Print Card type
00170         uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00171         printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00172         wait_ms(50);
00173 
00174     }
00175 }