Its a code of W7500 board for the vending machine to vend an item to the user within the organization. When the user is identified with the help of the RFID reader and when the user choose the item . the data is sent serially to WIZ750SR and the item is vended . The LCD displays the details of the transaction.

Dependencies:   HCSR04 MFRC522 TextLCD mbed-src

Fork of RFID_copy by Rajib Kumer Dey

Committer:
HarshaDRAGNEEL
Date:
Tue Jul 17 05:59:27 2018 +0000
Revision:
1:6cd395827886
Parent:
0:1fdb07d055b9
Its a program to identify the user using RFID tag and vend an item to the user with displaying the details in LCD and the data is serially transmitted to WIZ750SR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HarshaDRAGNEEL 1:6cd395827886 1
Rajib 0:1fdb07d055b9 2 //Connect as follows:
HarshaDRAGNEEL 1:6cd395827886 3 //RFID pins, motor
Rajib 0:1fdb07d055b9 4 //----------------------------------------
Rajib 0:1fdb07d055b9 5 //RFID IRQ=pin5 -> Not used. Leave open
Rajib 0:1fdb07d055b9 6 //RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12
Rajib 0:1fdb07d055b9 7 //RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11
Rajib 0:1fdb07d055b9 8 //RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13
Rajib 0:1fdb07d055b9 9 //RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10
Rajib 0:1fdb07d055b9 10 //RFID RST=pin7 -> Nucleo =PA_9=D8
Rajib 0:1fdb07d055b9 11 //3.3V and Gnd to the respective pins
HarshaDRAGNEEL 1:6cd395827886 12 // 1st Motor transistor to Pin D9
HarshaDRAGNEEL 1:6cd395827886 13 // 2nd Motor transistor to Pin D14
HarshaDRAGNEEL 1:6cd395827886 14 // Pin D0 to pin 2 of the serial Cable
HarshaDRAGNEEL 1:6cd395827886 15 // pin D1 to pin 3 of the serial cable
HarshaDRAGNEEL 1:6cd395827886 16 // pin A0 to the trigger of ultrasonic sensor 1
HarshaDRAGNEEL 1:6cd395827886 17 // pin A1 to the Echo of ultrasonic sensor 1
HarshaDRAGNEEL 1:6cd395827886 18 // pin D2 to RS pin of LCD
HarshaDRAGNEEL 1:6cd395827886 19 // pin D3 to E pin of LCD
HarshaDRAGNEEL 1:6cd395827886 20 // pin D4 to D4 pin of LCD
HarshaDRAGNEEL 1:6cd395827886 21 // pin D5 to D5 pin of LCD
HarshaDRAGNEEL 1:6cd395827886 22 // pin D6 to D6 pin of LCD
HarshaDRAGNEEL 1:6cd395827886 23 // pin D7 to D7 pin of LCD
HarshaDRAGNEEL 1:6cd395827886 24
HarshaDRAGNEEL 1:6cd395827886 25 //---------------------------------------------
Rajib 0:1fdb07d055b9 26 #include "mbed.h"
Rajib 0:1fdb07d055b9 27 #include "MFRC522.h"
Rajib 0:1fdb07d055b9 28 #include "SPI.h"
HarshaDRAGNEEL 1:6cd395827886 29 #include "hcsr04.h"
HarshaDRAGNEEL 1:6cd395827886 30 #include "TextLCD.h"
Rajib 0:1fdb07d055b9 31 #define ECHO_SERVER_PORT 7
Rajib 0:1fdb07d055b9 32 #define SPI_MOSI D11
Rajib 0:1fdb07d055b9 33 #define SPI_MISO D12
Rajib 0:1fdb07d055b9 34 #define SPI_SCK D13
Rajib 0:1fdb07d055b9 35 #define SPI_CS D10
HarshaDRAGNEEL 1:6cd395827886 36 #define MF_RESET D8
HarshaDRAGNEEL 1:6cd395827886 37 TextLCD lcd(D2, D3, D4, D5, D6, D7,TextLCD::LCD20x4); // rs, e, d4-d7
HarshaDRAGNEEL 1:6cd395827886 38 DigitalOut myled(D9);
HarshaDRAGNEEL 1:6cd395827886 39 DigitalOut myled1(D14);
HarshaDRAGNEEL 1:6cd395827886 40 //DigitalOut myled2(D15); Can be enabled for 3rd motor slot.
HarshaDRAGNEEL 1:6cd395827886 41
HarshaDRAGNEEL 1:6cd395827886 42 HCSR04 sensor(A0,A1); // Trigger , Echo
HarshaDRAGNEEL 1:6cd395827886 43 HCSR04 sensor1(A2,A3); //Trigger , Echo
HarshaDRAGNEEL 1:6cd395827886 44 //HCSR04 sensor2(A4,A5); //Trigger , Echo Can enable if needed for 3rd slot.
HarshaDRAGNEEL 1:6cd395827886 45
HarshaDRAGNEEL 1:6cd395827886 46 void distance();
HarshaDRAGNEEL 1:6cd395827886 47 void display();
HarshaDRAGNEEL 1:6cd395827886 48
Rajib 0:1fdb07d055b9 49 //Serial connection to PC for output
HarshaDRAGNEEL 1:6cd395827886 50 Serial pc(USBTX, USBRX);
HarshaDRAGNEEL 1:6cd395827886 51 // Serial connection with WIZ750SR gateway for MQTT communication
HarshaDRAGNEEL 1:6cd395827886 52 Serial serial(D1,D0); //TX and RX
HarshaDRAGNEEL 1:6cd395827886 53 // Initializing RFID chip pins
Rajib 0:1fdb07d055b9 54 MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
HarshaDRAGNEEL 1:6cd395827886 55 char c;
HarshaDRAGNEEL 1:6cd395827886 56 int d;
Rajib 0:1fdb07d055b9 57 int main(void) {
HarshaDRAGNEEL 1:6cd395827886 58 pc.printf("starting...\n");
HarshaDRAGNEEL 1:6cd395827886 59 myled=0;
HarshaDRAGNEEL 1:6cd395827886 60 pc.printf("Wait a second...\r\n");
HarshaDRAGNEEL 1:6cd395827886 61
Rajib 0:1fdb07d055b9 62 //Init. RC522 Chip
Rajib 0:1fdb07d055b9 63 RfChip.PCD_Init();
Rajib 0:1fdb07d055b9 64
Rajib 0:1fdb07d055b9 65 while (true) {
HarshaDRAGNEEL 1:6cd395827886 66 pc.printf("enterd RFID reading loop...\n");
Rajib 0:1fdb07d055b9 67 // Look for new cards
Rajib 0:1fdb07d055b9 68 if ( ! RfChip.PICC_IsNewCardPresent())
Rajib 0:1fdb07d055b9 69 {
Rajib 0:1fdb07d055b9 70 wait_ms(500);
Rajib 0:1fdb07d055b9 71 continue;
Rajib 0:1fdb07d055b9 72 }
HarshaDRAGNEEL 1:6cd395827886 73 // Select one of the cards
Rajib 0:1fdb07d055b9 74 if ( ! RfChip.PICC_ReadCardSerial())
Rajib 0:1fdb07d055b9 75 {
Rajib 0:1fdb07d055b9 76 wait_ms(500);
Rajib 0:1fdb07d055b9 77 continue;
Rajib 0:1fdb07d055b9 78 }
HarshaDRAGNEEL 1:6cd395827886 79
HarshaDRAGNEEL 1:6cd395827886 80 char data[20]="";
HarshaDRAGNEEL 1:6cd395827886 81 char data1[20]="";
Rajib 0:1fdb07d055b9 82
Rajib 0:1fdb07d055b9 83 // Print Card UID
Rajib 0:1fdb07d055b9 84 pc.printf("Card UID: ");
Rajib 0:1fdb07d055b9 85 printf("Size of UID: %d \n",RfChip.uid.size);
Rajib 0:1fdb07d055b9 86 for (uint8_t i = 0; i < RfChip.uid.size; i++)
Rajib 0:1fdb07d055b9 87 {
Rajib 0:1fdb07d055b9 88 char temp[5];
Rajib 0:1fdb07d055b9 89 pc.printf(" %X02", RfChip.uid.uidByte[i]);
Rajib 0:1fdb07d055b9 90 sprintf(temp,"%X02", RfChip.uid.uidByte[i]);
Rajib 0:1fdb07d055b9 91 strcat(data,temp);
Rajib 0:1fdb07d055b9 92 }
Rajib 0:1fdb07d055b9 93
Rajib 0:1fdb07d055b9 94 // Print Card type
Rajib 0:1fdb07d055b9 95 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Rajib 0:1fdb07d055b9 96 pc.printf(" \nPICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Rajib 0:1fdb07d055b9 97 wait_ms(1000);
Rajib 0:1fdb07d055b9 98
HarshaDRAGNEEL 1:6cd395827886 99 if(strcmp(data,"BA0211026102D302")==0) //Comparing the stored data with the new data read by the RFID reader.
Rajib 0:1fdb07d055b9 100 {
HarshaDRAGNEEL 1:6cd395827886 101 strcat(data1,"natsu");
HarshaDRAGNEEL 1:6cd395827886 102 wait(1);
HarshaDRAGNEEL 1:6cd395827886 103 serial.printf("%s",data1);
HarshaDRAGNEEL 1:6cd395827886 104 wait(2);
HarshaDRAGNEEL 1:6cd395827886 105 distance();
Rajib 0:1fdb07d055b9 106 }
HarshaDRAGNEEL 1:6cd395827886 107 if(strcmp(data,"E8028002AC02E902")==0)
Rajib 0:1fdb07d055b9 108 {
HarshaDRAGNEEL 1:6cd395827886 109 strcat(data1,"luffy");
HarshaDRAGNEEL 1:6cd395827886 110 wait(1);
HarshaDRAGNEEL 1:6cd395827886 111 serial.printf("%s",data1);
HarshaDRAGNEEL 1:6cd395827886 112 wait(2);
HarshaDRAGNEEL 1:6cd395827886 113 distance ();
HarshaDRAGNEEL 1:6cd395827886 114 }
HarshaDRAGNEEL 1:6cd395827886 115 pc.printf("The user name is :%s \n",data1);
HarshaDRAGNEEL 1:6cd395827886 116 }
Rajib 0:1fdb07d055b9 117 }
HarshaDRAGNEEL 1:6cd395827886 118
HarshaDRAGNEEL 1:6cd395827886 119
HarshaDRAGNEEL 1:6cd395827886 120 void distance()
HarshaDRAGNEEL 1:6cd395827886 121 {
HarshaDRAGNEEL 1:6cd395827886 122
HarshaDRAGNEEL 1:6cd395827886 123 pc.printf("Entered into distance loop \n");
HarshaDRAGNEEL 1:6cd395827886 124 while(1){
HarshaDRAGNEEL 1:6cd395827886 125 long distance = sensor.distance();
HarshaDRAGNEEL 1:6cd395827886 126 wait(0.5);
HarshaDRAGNEEL 1:6cd395827886 127 long distance1 = sensor1.distance();
HarshaDRAGNEEL 1:6cd395827886 128 wait(0.5);
HarshaDRAGNEEL 1:6cd395827886 129 // long distance2 = sensor2.distance(); //can be enabled if connected to 3rd Ultrasonic sensor
HarshaDRAGNEEL 1:6cd395827886 130 pc.printf("distance %d \n",distance);
HarshaDRAGNEEL 1:6cd395827886 131 pc.printf("distance %d \n",distance1);
HarshaDRAGNEEL 1:6cd395827886 132 // pc.printf("distance %d \n",distance2); //can be enabled if connected to 3rd Ultrasonic sensor to enable it in the PC.
HarshaDRAGNEEL 1:6cd395827886 133 wait(1);
HarshaDRAGNEEL 1:6cd395827886 134
HarshaDRAGNEEL 1:6cd395827886 135 wait(1.0); // 1 sec
HarshaDRAGNEEL 1:6cd395827886 136 if(distance<=10){
HarshaDRAGNEEL 1:6cd395827886 137 myled= 1;
HarshaDRAGNEEL 1:6cd395827886 138 wait(3);
HarshaDRAGNEEL 1:6cd395827886 139 myled=0;
HarshaDRAGNEEL 1:6cd395827886 140 serial.printf("/drink");
HarshaDRAGNEEL 1:6cd395827886 141 c = '\0';
HarshaDRAGNEEL 1:6cd395827886 142 c = 'd';
HarshaDRAGNEEL 1:6cd395827886 143 d=15;
HarshaDRAGNEEL 1:6cd395827886 144 display();
HarshaDRAGNEEL 1:6cd395827886 145 pc.printf("/ item drink /cost 15");
HarshaDRAGNEEL 1:6cd395827886 146 break;
HarshaDRAGNEEL 1:6cd395827886 147 }
HarshaDRAGNEEL 1:6cd395827886 148 else if(distance1<=10){
HarshaDRAGNEEL 1:6cd395827886 149 myled1= 1;
HarshaDRAGNEEL 1:6cd395827886 150 wait(3);
HarshaDRAGNEEL 1:6cd395827886 151 myled1=0;
HarshaDRAGNEEL 1:6cd395827886 152 serial.printf("/snack");
HarshaDRAGNEEL 1:6cd395827886 153 c = '\0';
HarshaDRAGNEEL 1:6cd395827886 154 c = 's';
HarshaDRAGNEEL 1:6cd395827886 155 d=10;
HarshaDRAGNEEL 1:6cd395827886 156 display();
HarshaDRAGNEEL 1:6cd395827886 157 pc.printf("/item snack /cost 10");
HarshaDRAGNEEL 1:6cd395827886 158 break;
HarshaDRAGNEEL 1:6cd395827886 159 }
HarshaDRAGNEEL 1:6cd395827886 160 /* else if(distance2<=10){
HarshaDRAGNEEL 1:6cd395827886 161 myled1= 1;
HarshaDRAGNEEL 1:6cd395827886 162 wait(30);
HarshaDRAGNEEL 1:6cd395827886 163 myled1=0;
HarshaDRAGNEEL 1:6cd395827886 164 serial.printf("/choco");
HarshaDRAGNEEL 1:6cd395827886 165 c = '\0';
HarshaDRAGNEEL 1:6cd395827886 166 c = 's';
HarshaDRAGNEEL 1:6cd395827886 167 d=10;
HarshaDRAGNEEL 1:6cd395827886 168 display();
HarshaDRAGNEEL 1:6cd395827886 169 pc.printf("/snack/10");
HarshaDRAGNEEL 1:6cd395827886 170 break;
HarshaDRAGNEEL 1:6cd395827886 171 } */ //Can be enabled when 3rd ultrasonic is connected.
HarshaDRAGNEEL 1:6cd395827886 172
HarshaDRAGNEEL 1:6cd395827886 173 }
HarshaDRAGNEEL 1:6cd395827886 174 }
HarshaDRAGNEEL 1:6cd395827886 175
HarshaDRAGNEEL 1:6cd395827886 176 //function for LCD display ....
HarshaDRAGNEEL 1:6cd395827886 177
HarshaDRAGNEEL 1:6cd395827886 178 void display(){
HarshaDRAGNEEL 1:6cd395827886 179 lcd.locate(3,0);
HarshaDRAGNEEL 1:6cd395827886 180 lcd.printf("Vending Machine\n");
HarshaDRAGNEEL 1:6cd395827886 181 lcd.locate(0,2);
HarshaDRAGNEEL 1:6cd395827886 182 if(c=='d'){
HarshaDRAGNEEL 1:6cd395827886 183 lcd.printf("ITEM NAME: drink");
HarshaDRAGNEEL 1:6cd395827886 184 }
HarshaDRAGNEEL 1:6cd395827886 185 else if(c=='s')
HarshaDRAGNEEL 1:6cd395827886 186 {
HarshaDRAGNEEL 1:6cd395827886 187 lcd.printf("ITEM NAME: Snack");
HarshaDRAGNEEL 1:6cd395827886 188 }
HarshaDRAGNEEL 1:6cd395827886 189 lcd.locate(0,3);
HarshaDRAGNEEL 1:6cd395827886 190 lcd.printf("ITEM COST: %d",d);
HarshaDRAGNEEL 1:6cd395827886 191 }