Program to serially receive in WIZ750SR to publish data in MQTT broker

Dependencies:   MQTT WIZnetInterface mbed-src

Fork of RFID_copy by Rajib Kumer Dey

Committer:
Rajib
Date:
Thu Jun 07 04:14:28 2018 +0000
Revision:
0:1fdb07d055b9
Child:
1:7638b96d77af
rfid;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rajib 0:1fdb07d055b9 1 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay
Rajib 0:1fdb07d055b9 2 //This code is based on Martin Olejar's MFRC522 library. Minimal changes
Rajib 0:1fdb07d055b9 3 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too
Rajib 0:1fdb07d055b9 4
Rajib 0:1fdb07d055b9 5 //Connect as follows:
Rajib 0:1fdb07d055b9 6 //RFID pins -> Nucleo header CN5 (Arduino-compatible header)
Rajib 0:1fdb07d055b9 7 //----------------------------------------
Rajib 0:1fdb07d055b9 8 //RFID IRQ=pin5 -> Not used. Leave open
Rajib 0:1fdb07d055b9 9 //RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12
Rajib 0:1fdb07d055b9 10 //RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11
Rajib 0:1fdb07d055b9 11 //RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13
Rajib 0:1fdb07d055b9 12 //RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10
Rajib 0:1fdb07d055b9 13 //RFID RST=pin7 -> Nucleo =PA_9=D8
Rajib 0:1fdb07d055b9 14 //3.3V and Gnd to the respective pins
Rajib 0:1fdb07d055b9 15
Rajib 0:1fdb07d055b9 16 #include "mbed.h"
Rajib 0:1fdb07d055b9 17 #include "MFRC522.h"
Rajib 0:1fdb07d055b9 18 #include "SPI.h"
Rajib 0:1fdb07d055b9 19 #include "MQTTEthernet.h"
Rajib 0:1fdb07d055b9 20 #include "MQTTClient.h"
Rajib 0:1fdb07d055b9 21
Rajib 0:1fdb07d055b9 22 #define ECHO_SERVER_PORT 7
Rajib 0:1fdb07d055b9 23
Rajib 0:1fdb07d055b9 24 // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
Rajib 0:1fdb07d055b9 25 //#define MF_RESET D8
Rajib 0:1fdb07d055b9 26
Rajib 0:1fdb07d055b9 27 #define SPI_MOSI D11
Rajib 0:1fdb07d055b9 28 #define SPI_MISO D12
Rajib 0:1fdb07d055b9 29 #define SPI_SCK D13
Rajib 0:1fdb07d055b9 30 #define SPI_CS D10
Rajib 0:1fdb07d055b9 31 #define MF_RESET D9
Rajib 0:1fdb07d055b9 32 DigitalOut l1(D4);
Rajib 0:1fdb07d055b9 33 DigitalOut l2(D5);
Rajib 0:1fdb07d055b9 34 DigitalOut LedGreen(LED1);
Rajib 0:1fdb07d055b9 35
Rajib 0:1fdb07d055b9 36 //Serial connection to PC for output
Rajib 0:1fdb07d055b9 37 //Serial pc(USBTX, USBRX);
Rajib 0:1fdb07d055b9 38
Rajib 0:1fdb07d055b9 39 MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
Rajib 0:1fdb07d055b9 40
Rajib 0:1fdb07d055b9 41 int main(void) {
Rajib 0:1fdb07d055b9 42 //pc.printf("starting...\n");
Rajib 0:1fdb07d055b9 43
Rajib 0:1fdb07d055b9 44 Serial pc(USBTX, USBRX);
Rajib 0:1fdb07d055b9 45 pc.baud(115200);
Rajib 0:1fdb07d055b9 46 printf("Wait a second...\r\n");
Rajib 0:1fdb07d055b9 47 char* topic = "openhab/parents/command";
Rajib 0:1fdb07d055b9 48 MQTTEthernet ipstack = MQTTEthernet();
Rajib 0:1fdb07d055b9 49
Rajib 0:1fdb07d055b9 50 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
Rajib 0:1fdb07d055b9 51
Rajib 0:1fdb07d055b9 52 char* hostname = "172.16.73.4";
Rajib 0:1fdb07d055b9 53 int port = 1883;
Rajib 0:1fdb07d055b9 54
Rajib 0:1fdb07d055b9 55 int rc = ipstack.connect(hostname, port);
Rajib 0:1fdb07d055b9 56 if (rc != 0)
Rajib 0:1fdb07d055b9 57 printf("rc from TCP connect is %d\n", rc);
Rajib 0:1fdb07d055b9 58
Rajib 0:1fdb07d055b9 59 printf("Topic: %s\r\n",topic);
Rajib 0:1fdb07d055b9 60
Rajib 0:1fdb07d055b9 61 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
Rajib 0:1fdb07d055b9 62 data.MQTTVersion = 3;
Rajib 0:1fdb07d055b9 63 data.clientID.cstring = "parents";
Rajib 0:1fdb07d055b9 64
Rajib 0:1fdb07d055b9 65 if ((rc = client.connect(data)) == 0)
Rajib 0:1fdb07d055b9 66 printf("rc from MQTT connect is %d\n", rc);
Rajib 0:1fdb07d055b9 67
Rajib 0:1fdb07d055b9 68 //Init. RC522 Chip
Rajib 0:1fdb07d055b9 69 RfChip.PCD_Init();
Rajib 0:1fdb07d055b9 70
Rajib 0:1fdb07d055b9 71 while (true) {
Rajib 0:1fdb07d055b9 72 //LedGreen = 1;
Rajib 0:1fdb07d055b9 73 pc.printf("enterd loop...\n");
Rajib 0:1fdb07d055b9 74 // Look for new cards
Rajib 0:1fdb07d055b9 75 if ( ! RfChip.PICC_IsNewCardPresent())
Rajib 0:1fdb07d055b9 76 {
Rajib 0:1fdb07d055b9 77 wait_ms(500);
Rajib 0:1fdb07d055b9 78 continue;
Rajib 0:1fdb07d055b9 79 }
Rajib 0:1fdb07d055b9 80
Rajib 0:1fdb07d055b9 81 // Select one of the cards
Rajib 0:1fdb07d055b9 82 if ( ! RfChip.PICC_ReadCardSerial())
Rajib 0:1fdb07d055b9 83 {
Rajib 0:1fdb07d055b9 84 wait_ms(500);
Rajib 0:1fdb07d055b9 85 continue;
Rajib 0:1fdb07d055b9 86 }
Rajib 0:1fdb07d055b9 87
Rajib 0:1fdb07d055b9 88 //LedGreen = 0;
Rajib 0:1fdb07d055b9 89 char data[20]="";
Rajib 0:1fdb07d055b9 90 char data1[20]="";
Rajib 0:1fdb07d055b9 91
Rajib 0:1fdb07d055b9 92 // Print Card UID
Rajib 0:1fdb07d055b9 93 pc.printf("Card UID: ");
Rajib 0:1fdb07d055b9 94 printf("Size of UID: %d \n",RfChip.uid.size);
Rajib 0:1fdb07d055b9 95 for (uint8_t i = 0; i < RfChip.uid.size; i++)
Rajib 0:1fdb07d055b9 96 {
Rajib 0:1fdb07d055b9 97 char temp[5];
Rajib 0:1fdb07d055b9 98 pc.printf(" %X02", RfChip.uid.uidByte[i]);
Rajib 0:1fdb07d055b9 99 sprintf(temp,"%X02", RfChip.uid.uidByte[i]);
Rajib 0:1fdb07d055b9 100 strcat(data,temp);
Rajib 0:1fdb07d055b9 101 }
Rajib 0:1fdb07d055b9 102
Rajib 0:1fdb07d055b9 103 /*
Rajib 0:1fdb07d055b9 104 pc.printf("\n\r");
Rajib 0:1fdb07d055b9 105 printf("%s\n\r",data);
Rajib 0:1fdb07d055b9 106 */
Rajib 0:1fdb07d055b9 107
Rajib 0:1fdb07d055b9 108 // Print Card type
Rajib 0:1fdb07d055b9 109 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Rajib 0:1fdb07d055b9 110 pc.printf(" \nPICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Rajib 0:1fdb07d055b9 111 wait_ms(1000);
Rajib 0:1fdb07d055b9 112
Rajib 0:1fdb07d055b9 113 if(strcmp(data,"5C02820285024502")==0)
Rajib 0:1fdb07d055b9 114 {
Rajib 0:1fdb07d055b9 115 strcat(data1,"fruits");
Rajib 0:1fdb07d055b9 116 }
Rajib 0:1fdb07d055b9 117 if(strcmp(data,"B3026802A002202")==0)
Rajib 0:1fdb07d055b9 118 {
Rajib 0:1fdb07d055b9 119 strcat(data1,"vegatables");
Rajib 0:1fdb07d055b9 120 }
Rajib 0:1fdb07d055b9 121 if(strcmp(data,"F2029C02AC021F02")==0)
Rajib 0:1fdb07d055b9 122 {
Rajib 0:1fdb07d055b9 123 strcat(data1,"milk");
Rajib 0:1fdb07d055b9 124
Rajib 0:1fdb07d055b9 125 MQTT::Message message;
Rajib 0:1fdb07d055b9 126 char buf[100];
Rajib 0:1fdb07d055b9 127 sprintf(buf, "%s", data1);
Rajib 0:1fdb07d055b9 128 message.qos = MQTT::QOS0;
Rajib 0:1fdb07d055b9 129 message.retained = false;
Rajib 0:1fdb07d055b9 130 message.dup = false;
Rajib 0:1fdb07d055b9 131 message.payload = (void*)data1;
Rajib 0:1fdb07d055b9 132 message.payloadlen = strlen(data1);
Rajib 0:1fdb07d055b9 133 rc = client.publish("grocery", message);
Rajib 0:1fdb07d055b9 134 client.yield(60000);
Rajib 0:1fdb07d055b9 135 memset(data1, '\0',sizeof(data1));
Rajib 0:1fdb07d055b9 136 }
Rajib 0:1fdb07d055b9 137 }
Rajib 0:1fdb07d055b9 138 }