This content is RFID Reader (Passive) Using Wizwiki-W7500 + RFID-RC522 + Buzzer

Dependencies:   MFRC522 WIZnetInterface mbed

Fork of TCPEchoServer-WIZwiki-W7500 by WIZnet

Committer:
najgh08
Date:
Wed Aug 26 10:55:22 2015 +0000
Revision:
12:f22ae34e1d95
Parent:
11:0da8667a9201
..

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 8:f837e0d255e8 1 #include "mbed.h"
najgh08 12:f22ae34e1d95 2 // Ethernet
justinkim 8:f837e0d255e8 3 #include "EthernetInterface.h"
najgh08 12:f22ae34e1d95 4 // MFRC522(RFID)
najgh08 12:f22ae34e1d95 5 #include "MFRC522.h"
hjjeon 9:a63ff95c354b 6
najgh08 12:f22ae34e1d95 7 #define WEB_SERVER_PORT 80
najgh08 12:f22ae34e1d95 8
najgh08 12:f22ae34e1d95 9 Serial pc(USBTX, USBRX);
najgh08 12:f22ae34e1d95 10
najgh08 12:f22ae34e1d95 11 // MFRC522 reset (pick another D pin if you need D8)
najgh08 12:f22ae34e1d95 12 #define MF_RESET D8
najgh08 12:f22ae34e1d95 13 // Buzzer
najgh08 12:f22ae34e1d95 14 #define VOLUME 0.02
najgh08 12:f22ae34e1d95 15 #define BPM 100.0
najgh08 12:f22ae34e1d95 16 // LED
najgh08 12:f22ae34e1d95 17 DigitalOut Led(LED1);
najgh08 12:f22ae34e1d95 18
najgh08 12:f22ae34e1d95 19 // Buzzer
najgh08 12:f22ae34e1d95 20 PwmOut pwm_pin(D3);
najgh08 12:f22ae34e1d95 21
najgh08 12:f22ae34e1d95 22 float beat_duration;
najgh08 12:f22ae34e1d95 23 // Plays a sound with the defined frequency, duration, and volume
najgh08 12:f22ae34e1d95 24 void playNote(float frequency, float duration, float volume) {
najgh08 12:f22ae34e1d95 25 pwm_pin.period(1.0/(double)frequency);
najgh08 12:f22ae34e1d95 26 pwm_pin = ((double)volume/2.0);
najgh08 12:f22ae34e1d95 27 wait(duration);
najgh08 12:f22ae34e1d95 28 pwm_pin = 0.0;
najgh08 12:f22ae34e1d95 29 }
najgh08 12:f22ae34e1d95 30
najgh08 12:f22ae34e1d95 31 // CARD number
najgh08 12:f22ae34e1d95 32 uint8_t CARD_1 = 0x44;
najgh08 12:f22ae34e1d95 33 uint8_t CARD_2 = 0xCB;
najgh08 12:f22ae34e1d95 34 uint8_t CARD_3 = 0x2A;
najgh08 12:f22ae34e1d95 35 uint8_t CARD_4 = 0xA4;
najgh08 12:f22ae34e1d95 36
najgh08 12:f22ae34e1d95 37 uint8_t CARD_5 = 0xA2;
najgh08 12:f22ae34e1d95 38 uint8_t CARD_6 = 0xCE;
najgh08 12:f22ae34e1d95 39 uint8_t CARD_7 = 0xEC;
najgh08 12:f22ae34e1d95 40 uint8_t CARD_8 = 0xD5;
najgh08 12:f22ae34e1d95 41
najgh08 12:f22ae34e1d95 42 // RC522 SPI communication
najgh08 12:f22ae34e1d95 43 MFRC522 RfChip (D11, D12, D13, D10, MF_RESET);
najgh08 12:f22ae34e1d95 44
najgh08 12:f22ae34e1d95 45 //HTTP WEB SERVER
najgh08 12:f22ae34e1d95 46 char recv_buffer[1024] = {0,};
najgh08 12:f22ae34e1d95 47 char echoHeader[1024] = {0,};
najgh08 12:f22ae34e1d95 48 char * findrfid = 0;
najgh08 12:f22ae34e1d95 49 char change_rfid[4] = {1,2,3,4};
hjjeon 9:a63ff95c354b 50
hjjeon 11:0da8667a9201 51 int main (void)
justinkim 8:f837e0d255e8 52 {
najgh08 12:f22ae34e1d95 53 // Init. RC522 Chip
najgh08 12:f22ae34e1d95 54 RfChip.PCD_Init();
najgh08 12:f22ae34e1d95 55 pc.printf("Init end\r\n");
najgh08 12:f22ae34e1d95 56 // WEB SERVER Start
justinkim 8:f837e0d255e8 57 printf("Wait a second...\r\n");
najgh08 12:f22ae34e1d95 58 // IP Setting..
najgh08 12:f22ae34e1d95 59 #ifdef dhcp
najgh08 12:f22ae34e1d95 60 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
najgh08 12:f22ae34e1d95 61 EthernetInterface eth;
najgh08 12:f22ae34e1d95 62 eth.init(mac_addr); //Use DHCP
najgh08 12:f22ae34e1d95 63 eth.connect();
najgh08 12:f22ae34e1d95 64 #else
najgh08 12:f22ae34e1d95 65 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
najgh08 12:f22ae34e1d95 66 char ip_addr[] = "192.168.1.111";
najgh08 12:f22ae34e1d95 67 char subnet_mask[] = "255.255.255.0";
najgh08 12:f22ae34e1d95 68 char gateway_addr[] = "192.168.1.1";
najgh08 12:f22ae34e1d95 69 EthernetInterface eth;
najgh08 12:f22ae34e1d95 70 eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP
najgh08 12:f22ae34e1d95 71
najgh08 12:f22ae34e1d95 72 while(1) { //Wait link up
najgh08 12:f22ae34e1d95 73 if(eth.link() == true)
najgh08 12:f22ae34e1d95 74 break;
najgh08 12:f22ae34e1d95 75 }
najgh08 12:f22ae34e1d95 76 printf("Check Ethernet Link\r\n");
najgh08 12:f22ae34e1d95 77 printf("Link up\r\n");
najgh08 12:f22ae34e1d95 78 #endif
najgh08 12:f22ae34e1d95 79
najgh08 12:f22ae34e1d95 80 //eth.connect();
justinkim 8:f837e0d255e8 81 printf("Server IP Address is %s\r\n", eth.getIPAddress());
hjjeon 11:0da8667a9201 82
hjjeon 11:0da8667a9201 83 TCPSocketServer server;
najgh08 12:f22ae34e1d95 84 server.bind(WEB_SERVER_PORT);
najgh08 12:f22ae34e1d95 85 printf("bind complete\r\n");
hjjeon 11:0da8667a9201 86 server.listen();
najgh08 12:f22ae34e1d95 87 printf("listen complete\r\n");
hjjeon 11:0da8667a9201 88 while (true)
najgh08 12:f22ae34e1d95 89 {
hjjeon 11:0da8667a9201 90 printf("Wait for new connection...\r\n");
hjjeon 11:0da8667a9201 91 TCPSocketConnection client;
hjjeon 11:0da8667a9201 92 server.accept(client);
hjjeon 11:0da8667a9201 93 client.set_blocking(false, 15000); // Timeout after (1.5)s
hjjeon 11:0da8667a9201 94
hjjeon 11:0da8667a9201 95 printf("Connection from: %s\r\n", client.get_address());
najgh08 12:f22ae34e1d95 96
najgh08 12:f22ae34e1d95 97 while (true) {
najgh08 12:f22ae34e1d95 98 int n = client.receive(recv_buffer, sizeof(recv_buffer));
najgh08 12:f22ae34e1d95 99 if (n <= 0) break; // n = n < 0
hjjeon 11:0da8667a9201 100
hjjeon 11:0da8667a9201 101 // print received message to terminal
najgh08 12:f22ae34e1d95 102 recv_buffer[n] = '\0';
najgh08 12:f22ae34e1d95 103 printf("Received message from Client :'%s'\r\n",recv_buffer);
najgh08 12:f22ae34e1d95 104 if(recv_buffer[0] == 'G' && recv_buffer[1] == 'E' && recv_buffer[2] == 'T')
najgh08 12:f22ae34e1d95 105 {
najgh08 12:f22ae34e1d95 106 printf("GET request incomming.\n\r");
najgh08 12:f22ae34e1d95 107
najgh08 12:f22ae34e1d95 108 //setup http response header + data
najgh08 12:f22ae34e1d95 109 char send_buffer[] = "<!DOCTYPE HTML>\r\n\n\r<html>\n\r\n\r\n\r<head>\n\r<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n\r<title>Wizwiki-W7500 Web Server</title>\n\r<meta name=\"generator\">\n\r</head>\n\r\n\r<body bgcolor=\"white\" text=\"black\" link=\"blue\" vlink=\"purple\" alink=\"red\">\r\n<p>Wizwiki-W7500 Home-Automation RFID</p>\r\n<p>Read RFID = $$$$$$$$$$$</p>\r\n</body>\n\r</html>\r\n";
najgh08 12:f22ae34e1d95 110
najgh08 12:f22ae34e1d95 111 findrfid = strstr(send_buffer, "$$$$$$$$$$$");
najgh08 12:f22ae34e1d95 112 memset(findrfid,0,11);
najgh08 12:f22ae34e1d95 113 sprintf(change_rfid, "%X,%X,%X,%X", RfChip.uid.uidByte[0],RfChip.uid.uidByte[1],RfChip.uid.uidByte[2],RfChip.uid.uidByte[3]);
najgh08 12:f22ae34e1d95 114 memcpy(findrfid,change_rfid,11);
najgh08 12:f22ae34e1d95 115
najgh08 12:f22ae34e1d95 116 sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text/html\n\rConnection: Closen\n\rRefresh: 5\n\r\n\r\n\r",strlen(send_buffer));
najgh08 12:f22ae34e1d95 117 client.send(echoHeader,strlen(echoHeader));
najgh08 12:f22ae34e1d95 118 client.send(send_buffer,strlen(send_buffer));
najgh08 12:f22ae34e1d95 119
najgh08 12:f22ae34e1d95 120 // RFID Area
najgh08 12:f22ae34e1d95 121 // Look for new cards
najgh08 12:f22ae34e1d95 122 if ( ! RfChip.PICC_IsNewCardPresent())
najgh08 12:f22ae34e1d95 123 {
najgh08 12:f22ae34e1d95 124 wait_ms(500);
najgh08 12:f22ae34e1d95 125 continue;
hjjeon 11:0da8667a9201 126 }
najgh08 12:f22ae34e1d95 127 // Select one of the cards
najgh08 12:f22ae34e1d95 128 if ( ! RfChip.PICC_ReadCardSerial())
najgh08 12:f22ae34e1d95 129 {
najgh08 12:f22ae34e1d95 130 wait_ms(500);
najgh08 12:f22ae34e1d95 131 continue;
najgh08 12:f22ae34e1d95 132 }
najgh08 12:f22ae34e1d95 133 // Print Card UID
najgh08 12:f22ae34e1d95 134 pc.printf("Card UID: ");
najgh08 12:f22ae34e1d95 135 for (uint8_t i = 0; i < RfChip.uid.size; i++)
najgh08 12:f22ae34e1d95 136 {
najgh08 12:f22ae34e1d95 137 pc.printf(" %X02", RfChip.uid.uidByte[i]);
najgh08 12:f22ae34e1d95 138 }
najgh08 12:f22ae34e1d95 139 pc.printf("\n\r");
najgh08 12:f22ae34e1d95 140 // Print Card type
najgh08 12:f22ae34e1d95 141 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
najgh08 12:f22ae34e1d95 142 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
najgh08 12:f22ae34e1d95 143 wait_ms(1000);
najgh08 12:f22ae34e1d95 144
najgh08 12:f22ae34e1d95 145 // Show RFID Tag
najgh08 12:f22ae34e1d95 146 if((RfChip.uid.uidByte[0] == CARD_1) && (RfChip.uid.uidByte[1] == CARD_2) && (RfChip.uid.uidByte[2] == CARD_3) && (RfChip.uid.uidByte[3] == CARD_4))
najgh08 12:f22ae34e1d95 147 {
najgh08 12:f22ae34e1d95 148 // Buzzer Sound
najgh08 12:f22ae34e1d95 149 pc.printf("TAG Read !\r\n");
najgh08 12:f22ae34e1d95 150 beat_duration = 60.0 / BPM;
najgh08 12:f22ae34e1d95 151 playNote(999.999, (0.75 * (double)beat_duration), VOLUME);
najgh08 12:f22ae34e1d95 152 }
najgh08 12:f22ae34e1d95 153 // Show RFID Card
najgh08 12:f22ae34e1d95 154 else if((RfChip.uid.uidByte[0] == CARD_5) && (RfChip.uid.uidByte[1] == CARD_6) && (RfChip.uid.uidByte[2] == CARD_7) && (RfChip.uid.uidByte[3] == CARD_8))
najgh08 12:f22ae34e1d95 155 {
najgh08 12:f22ae34e1d95 156 pc.printf("CARD Read !\r\n");
najgh08 12:f22ae34e1d95 157 beat_duration = 60.0 / BPM;
najgh08 12:f22ae34e1d95 158 playNote(111.111, (0.75 * (double)beat_duration), VOLUME);
najgh08 12:f22ae34e1d95 159 }
najgh08 12:f22ae34e1d95 160 }
hjjeon 11:0da8667a9201 161 if (n <= 0) break;
hjjeon 11:0da8667a9201 162 }
hjjeon 11:0da8667a9201 163 client.close();
hjjeon 9:a63ff95c354b 164 }
najgh08 12:f22ae34e1d95 165 }