Using Ethernet Interface to communicate and add a RFID reader
Dependencies: EthernetInterface ID12RFID mbed-rtos mbed
main.cpp@0:053082d6a270, 2013-10-17 (annotated)
- Committer:
- airaylee
- Date:
- Thu Oct 17 23:56:29 2013 +0000
- Revision:
- 0:053082d6a270
- Child:
- 1:ccc5641be0cd
Original Version;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:053082d6a270 | 8 | const char* ECHO_SERVER_ADDRESS = "130.207.234.205"; |
airaylee | 0:053082d6a270 | 9 | const int ECHO_SERVER_PORT = 7; |
airaylee | 0:053082d6a270 | 10 | |
airaylee | 0:053082d6a270 | 11 | int main() { |
airaylee | 0:053082d6a270 | 12 | printf("Hello World\n"); |
airaylee | 0:053082d6a270 | 13 | eth.init(); //Use DHCP |
airaylee | 0:053082d6a270 | 14 | //print out the MAC address first |
airaylee | 0:053082d6a270 | 15 | printf("MAC Address is %s\n", eth.getMACAddress()); |
airaylee | 0:053082d6a270 | 16 | |
airaylee | 0:053082d6a270 | 17 | while(1) { |
airaylee | 0:053082d6a270 | 18 | if(rfid.readable()) { |
airaylee | 0:053082d6a270 | 19 | int j=rfid.read(); |
airaylee | 0:053082d6a270 | 20 | //check if using right tag |
airaylee | 0:053082d6a270 | 21 | if (j==36902518){ |
airaylee | 0:053082d6a270 | 22 | printf("Right RFID, Here is the Monitor Info\r\n"); |
airaylee | 0:053082d6a270 | 23 | eth.connect(7000);//Longer timeout here |
airaylee | 0:053082d6a270 | 24 | printf("Client IP Address is %s\n", eth.getIPAddress()); |
airaylee | 0:053082d6a270 | 25 | TCPSocketConnection socket; |
airaylee | 0:053082d6a270 | 26 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
airaylee | 0:053082d6a270 | 27 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
airaylee | 0:053082d6a270 | 28 | wait(1); |
airaylee | 0:053082d6a270 | 29 | } |
airaylee | 0:053082d6a270 | 30 | char buf[256]={0}; |
airaylee | 0:053082d6a270 | 31 | int n = socket.receive(buf, 256); |
airaylee | 0:053082d6a270 | 32 | buf[n] = '\0'; |
airaylee | 0:053082d6a270 | 33 | printf("%s", buf); |
airaylee | 0:053082d6a270 | 34 | socket.close(); |
airaylee | 0:053082d6a270 | 35 | eth.disconnect(); |
airaylee | 0:053082d6a270 | 36 | } |
airaylee | 0:053082d6a270 | 37 | else |
airaylee | 0:053082d6a270 | 38 | printf("Wrong RFID Tag\r\n"); |
airaylee | 0:053082d6a270 | 39 | |
airaylee | 0:053082d6a270 | 40 | printf("Wait for next RFID Tag\r\n"); |
airaylee | 0:053082d6a270 | 41 | } |
airaylee | 0:053082d6a270 | 42 | } |
airaylee | 0:053082d6a270 | 43 | } |