Using Ethernet Interface to communicate and add a RFID reader

Dependencies:   EthernetInterface ID12RFID mbed-rtos mbed

Committer:
airaylee
Date:
Fri Oct 18 15:07:56 2013 +0000
Revision:
1:ccc5641be0cd
Parent:
0:053082d6a270
Second Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
airaylee 0:053082d6a270 1
airaylee 0:053082d6a270 2 #include "mbed.h"
airaylee 0:053082d6a270 3 #include "EthernetInterface.h"
airaylee 0:053082d6a270 4 #include "ID12RFID.h"
airaylee 0:053082d6a270 5
airaylee 0:053082d6a270 6 ID12RFID rfid(p14); // uart rx
airaylee 0:053082d6a270 7 EthernetInterface eth;
airaylee 1:ccc5641be0cd 8 //Here is the IP address of Server and port
airaylee 0:053082d6a270 9 const char* ECHO_SERVER_ADDRESS = "130.207.234.205";
airaylee 0:053082d6a270 10 const int ECHO_SERVER_PORT = 7;
airaylee 0:053082d6a270 11
airaylee 0:053082d6a270 12 int main() {
airaylee 0:053082d6a270 13 printf("Hello World\n");
airaylee 0:053082d6a270 14 eth.init(); //Use DHCP
airaylee 0:053082d6a270 15 //print out the MAC address first
airaylee 0:053082d6a270 16 printf("MAC Address is %s\n", eth.getMACAddress());
airaylee 0:053082d6a270 17
airaylee 0:053082d6a270 18 while(1) {
airaylee 0:053082d6a270 19 if(rfid.readable()) {
airaylee 0:053082d6a270 20 int j=rfid.read();
airaylee 0:053082d6a270 21 //check if using right tag
airaylee 0:053082d6a270 22 if (j==36902518){
airaylee 0:053082d6a270 23 printf("Right RFID, Here is the Monitor Info\r\n");
airaylee 0:053082d6a270 24 eth.connect(7000);//Longer timeout here
airaylee 0:053082d6a270 25 printf("Client IP Address is %s\n", eth.getIPAddress());
airaylee 0:053082d6a270 26 TCPSocketConnection socket;
airaylee 1:ccc5641be0cd 27 //Begin Connecting the server, if not, print the error info
airaylee 0:053082d6a270 28 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
airaylee 0:053082d6a270 29 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
airaylee 0:053082d6a270 30 wait(1);
airaylee 0:053082d6a270 31 }
airaylee 1:ccc5641be0cd 32 //Begin receiving the data from server, and print out onto the terminal
airaylee 0:053082d6a270 33 char buf[256]={0};
airaylee 0:053082d6a270 34 int n = socket.receive(buf, 256);
airaylee 0:053082d6a270 35 buf[n] = '\0';
airaylee 0:053082d6a270 36 printf("%s", buf);
airaylee 0:053082d6a270 37 socket.close();
airaylee 0:053082d6a270 38 eth.disconnect();
airaylee 0:053082d6a270 39 }
airaylee 1:ccc5641be0cd 40 //if not right tag, print out the error info and wait for another tag
airaylee 0:053082d6a270 41 else
airaylee 0:053082d6a270 42 printf("Wrong RFID Tag\r\n");
airaylee 0:053082d6a270 43
airaylee 0:053082d6a270 44 printf("Wait for next RFID Tag\r\n");
airaylee 0:053082d6a270 45 }
airaylee 0:053082d6a270 46 }
airaylee 0:053082d6a270 47 }