Program to receive the data serially in the WIZ750SR and add time data to the MQtt broker.
Dependencies: MFRC522 MQTT_NTP NTPClient WIZnetInterface mbed-src
Fork of RFID_copy by
Diff: main.cpp
- Revision:
- 1:f7f81ab19c7e
- Parent:
- 0:1fdb07d055b9
--- a/main.cpp Thu Jun 07 04:14:28 2018 +0000 +++ b/main.cpp Tue Jul 17 06:16:01 2018 +0000 @@ -1,20 +1,7 @@ -//Test of cheap 13.56 Mhz RFID-RC522 module from eBay -//This code is based on Martin Olejar's MFRC522 library. Minimal changes -//Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too - -//Connect as follows: -//RFID pins -> Nucleo header CN5 (Arduino-compatible header) -//---------------------------------------- -//RFID IRQ=pin5 -> Not used. Leave open -//RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12 -//RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11 -//RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13 -//RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10 -//RFID RST=pin7 -> Nucleo =PA_9=D8 -//3.3V and Gnd to the respective pins + #include "mbed.h" -#include "MFRC522.h" +#include "NTPClient.h" #include "SPI.h" #include "MQTTEthernet.h" #include "MQTTClient.h" @@ -23,28 +10,24 @@ // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) //#define MF_RESET D8 - -#define SPI_MOSI D11 -#define SPI_MISO D12 -#define SPI_SCK D13 -#define SPI_CS D10 -#define MF_RESET D9 -DigitalOut l1(D4); -DigitalOut l2(D5); -DigitalOut LedGreen(LED1); +EthernetInterface eth; +NTPClient ntpClient; + //Serial connection to PC for output -//Serial pc(USBTX, USBRX); - -MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); +Serial pc(USBTX, USBRX); + Serial a(D1,D0); +char c[100]=""; int main(void) { //pc.printf("starting...\n"); + int cnt = 0; Serial pc(USBTX, USBRX); - pc.baud(115200); + Serial a(D1,D0); + // pc.baud(115200); printf("Wait a second...\r\n"); - char* topic = "openhab/parents/command"; + char* topic = "Vending machine"; MQTTEthernet ipstack = MQTTEthernet(); MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); @@ -52,9 +35,10 @@ char* hostname = "172.16.73.4"; int port = 1883; - int rc = ipstack.connect(hostname, port); + + int rc = ipstack.connect(hostname, port); if (rc != 0) - printf("rc from TCP connect is %d\n", rc); + printf("rc from TCP connect is %d\n", rc); printf("Topic: %s\r\n",topic); @@ -64,75 +48,72 @@ if ((rc = client.connect(data)) == 0) printf("rc from MQTT connect is %d\n", rc); - + + // if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) +// printf("rc from MQTT subscribe is %d\n", rc); +// //Init. RC522 Chip - RfChip.PCD_Init(); + while (true) { - //LedGreen = 1; - pc.printf("enterd loop...\n"); - // Look for new cards - if ( ! RfChip.PICC_IsNewCardPresent()) - { - wait_ms(500); - continue; - } - - // Select one of the cards - if ( ! RfChip.PICC_ReadCardSerial()) + if (a.readable()) { - wait_ms(500); - continue; - } - - //LedGreen = 0; - char data[20]=""; - char data1[20]=""; + + int i; + c[i]=0; + for(i=0;i<=15;i++){ + char c1 =a.getc(); + c[i] = c1; + } + pc.printf("The value returned is %s ",c); + + char domainName[3][80] = {"kr.pool.ntp.org", "time.bora.net", "time.nuri.net"};//SET TO DOMAIN NAME OF SERVER GETTING TIME FROM + char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA + time_t sysTime; + pc.printf("Server IP Address is %s\r\n", eth.getIPAddress()); - // Print Card UID - pc.printf("Card UID: "); - printf("Size of UID: %d \n",RfChip.uid.size); - for (uint8_t i = 0; i < RfChip.uid.size; i++) - { - char temp[5]; - pc.printf(" %X02", RfChip.uid.uidByte[i]); - sprintf(temp,"%X02", RfChip.uid.uidByte[i]); - strcat(data,temp); - } + pc.printf("Getting time information by using NTP...\r\n"); - /* - pc.printf("\n\r"); - printf("%s\n\r",data); - */ - - // Print Card type - uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); - pc.printf(" \nPICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); - wait_ms(1000); - - if(strcmp(data,"5C02820285024502")==0) - { - strcat(data1,"fruits"); - } - if(strcmp(data,"B3026802A002202")==0) + cnt = 0; + while(1) { - strcat(data1,"vegatables"); + if(ntpClient.setTime(domainName[cnt],123,0x00005000) != NTP_OK) + { + pc.printf("Cannot get time information by NTP\r\n"); + cnt++; + } + else + break; + + if(cnt > 3) + { + pc.printf("All NTP servers are not resposed!!\r\n"); + return 1; + } } - if(strcmp(data,"F2029C02AC021F02")==0) - { - strcat(data1,"milk"); + + printf("Completed Get and Set Time\r\n\r\n"); + eth.disconnect(); - MQTT::Message message; + sysTime = time(NULL)+(3600*5.30); //TIME with offset for eastern time Indian + //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER + strftime(buffer,80,"%Y/%m/%d %p %I:%M:%S \r\n",localtime(&sysTime)); + pc.printf("Date and Time\r\n%s\r\n", buffer); + + + MQTT::Message message; char buf[100]; - sprintf(buf, "%s", data1); - message.qos = MQTT::QOS0; - message.retained = false; - message.dup = false; - message.payload = (void*)data1; - message.payloadlen = strlen(data1); - rc = client.publish("grocery", message); - client.yield(60000); - memset(data1, '\0',sizeof(data1)); - } + sprintf(buf, "%s %s",c,buffer); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + + message.payload = (void*)buf; + message.payloadlen = strlen(buf); + + rc = client.publish("RFID", message); + pc.printf("Rc result: %c \n ",rc); + client.yield(60); + } } } \ No newline at end of file