Allan Li / Mbed 2 deprecated TCPclient_ID-20_Midterm

Dependencies:   EthernetInterface ID12RFID mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "ID12RFID.h"
00005 
00006 ID12RFID rfid(p14); // uart rx
00007 EthernetInterface eth;
00008 //Here is the IP address of Server and port
00009 const char* ECHO_SERVER_ADDRESS = "130.207.234.205";
00010 const int ECHO_SERVER_PORT = 7;
00011 
00012 int main() {
00013     printf("Hello World\n");
00014     eth.init(); //Use DHCP
00015     //print out the MAC address first
00016     printf("MAC Address is %s\n", eth.getMACAddress());
00017     
00018     while(1) {
00019         if(rfid.readable()) {
00020             int j=rfid.read();
00021             //check if using right tag
00022             if (j==36902518){
00023                 printf("Right RFID, Here is the Monitor Info\r\n");
00024                 eth.connect(7000);//Longer timeout here
00025                 printf("Client IP Address is %s\n", eth.getIPAddress());
00026                 TCPSocketConnection socket;
00027                 //Begin Connecting the server, if not, print the error info
00028                 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
00029                     printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00030                     wait(1);
00031                 }
00032                 //Begin receiving the data from server, and print out onto the terminal 
00033                 char buf[256]={0};
00034                 int n = socket.receive(buf, 256);
00035                 buf[n] = '\0';
00036                 printf("%s", buf);
00037                 socket.close();
00038                 eth.disconnect();        
00039             }
00040             //if not right tag, print out the error info and wait for another tag
00041             else
00042                 printf("Wrong RFID Tag\r\n");
00043            
00044             printf("Wait for next RFID Tag\r\n");     
00045         }
00046     }
00047 }