A Bit funky still
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@0:337d7fbe5920, 2016-04-20 (annotated)
- Committer:
- ipv1
- Date:
- Wed Apr 20 08:57:07 2016 +0000
- Revision:
- 0:337d7fbe5920
- Child:
- 1:9a65c6d76db0
Should work version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ipv1 | 0:337d7fbe5920 | 1 | #include "mbed.h" |
ipv1 | 0:337d7fbe5920 | 2 | #include "EthernetInterface.h" |
ipv1 | 0:337d7fbe5920 | 3 | |
ipv1 | 0:337d7fbe5920 | 4 | #define ECHO_SERVER_PORT 7 |
ipv1 | 0:337d7fbe5920 | 5 | |
ipv1 | 0:337d7fbe5920 | 6 | |
ipv1 | 0:337d7fbe5920 | 7 | int main (void) |
ipv1 | 0:337d7fbe5920 | 8 | { |
ipv1 | 0:337d7fbe5920 | 9 | DigitalOut myled(LED1); |
ipv1 | 0:337d7fbe5920 | 10 | printf("Wait a second...\r\n"); |
ipv1 | 0:337d7fbe5920 | 11 | //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; |
ipv1 | 0:337d7fbe5920 | 12 | static const char* mbedIp = "192.168.64.232"; //IP |
ipv1 | 0:337d7fbe5920 | 13 | static const char* mbedMask = "255.255.255.0"; // Mask |
ipv1 | 0:337d7fbe5920 | 14 | static const char* mbedGateway = "192.168.64.1"; //Gateway |
ipv1 | 0:337d7fbe5920 | 15 | |
ipv1 | 0:337d7fbe5920 | 16 | EthernetInterface eth; |
ipv1 | 0:337d7fbe5920 | 17 | eth.init(mbedIp, mbedMask, mbedGateway); //Use DHCP |
ipv1 | 0:337d7fbe5920 | 18 | eth.connect(); |
ipv1 | 0:337d7fbe5920 | 19 | //printf("Server IP Address is %s\r\n", eth.getIPAddress()); |
ipv1 | 0:337d7fbe5920 | 20 | |
ipv1 | 0:337d7fbe5920 | 21 | TCPSocketServer server; |
ipv1 | 0:337d7fbe5920 | 22 | server.bind(ECHO_SERVER_PORT); |
ipv1 | 0:337d7fbe5920 | 23 | server.listen(); |
ipv1 | 0:337d7fbe5920 | 24 | |
ipv1 | 0:337d7fbe5920 | 25 | while (true) |
ipv1 | 0:337d7fbe5920 | 26 | { |
ipv1 | 0:337d7fbe5920 | 27 | //printf("Wait for new connection...\r\n"); |
ipv1 | 0:337d7fbe5920 | 28 | TCPSocketConnection client; |
ipv1 | 0:337d7fbe5920 | 29 | server.accept(client); |
ipv1 | 0:337d7fbe5920 | 30 | client.set_blocking(false, 15000); // Timeout after (1.5)s |
ipv1 | 0:337d7fbe5920 | 31 | |
ipv1 | 0:337d7fbe5920 | 32 | //printf("Connection from: %s\r\n", client.get_address()); |
ipv1 | 0:337d7fbe5920 | 33 | char buffer[256]; |
ipv1 | 0:337d7fbe5920 | 34 | while (true) { |
ipv1 | 0:337d7fbe5920 | 35 | int n = client.receive(buffer, sizeof(buffer)); |
ipv1 | 0:337d7fbe5920 | 36 | if (n <= 0) break; |
ipv1 | 0:337d7fbe5920 | 37 | |
ipv1 | 0:337d7fbe5920 | 38 | if(buffer[0]=='Y'){ |
ipv1 | 0:337d7fbe5920 | 39 | myled=1; |
ipv1 | 0:337d7fbe5920 | 40 | }else{ |
ipv1 | 0:337d7fbe5920 | 41 | myled=0; |
ipv1 | 0:337d7fbe5920 | 42 | } |
ipv1 | 0:337d7fbe5920 | 43 | // print received message to terminal |
ipv1 | 0:337d7fbe5920 | 44 | //buffer[n] = '\0'; |
ipv1 | 0:337d7fbe5920 | 45 | //printf("Received message from Client :'%s'\r\n",buffer); |
ipv1 | 0:337d7fbe5920 | 46 | |
ipv1 | 0:337d7fbe5920 | 47 | // reverse the message |
ipv1 | 0:337d7fbe5920 | 48 | //char temp; |
ipv1 | 0:337d7fbe5920 | 49 | // for(int f = 0, l = n-1; f<l; f++,l--){ |
ipv1 | 0:337d7fbe5920 | 50 | // temp = buffer[f]; |
ipv1 | 0:337d7fbe5920 | 51 | // buffer[f] = buffer[l]; |
ipv1 | 0:337d7fbe5920 | 52 | // buffer[l] = temp; |
ipv1 | 0:337d7fbe5920 | 53 | // } |
ipv1 | 0:337d7fbe5920 | 54 | sprintf(buffer, "Hi Alisha.How are u?"); |
ipv1 | 0:337d7fbe5920 | 55 | // print reversed message to terminal |
ipv1 | 0:337d7fbe5920 | 56 | //printf("Sending message to Client: '%s'\r\n",buffer); |
ipv1 | 0:337d7fbe5920 | 57 | |
ipv1 | 0:337d7fbe5920 | 58 | // Echo received message back to client |
ipv1 | 0:337d7fbe5920 | 59 | client.send_all(buffer, n); |
ipv1 | 0:337d7fbe5920 | 60 | if (n <= 0) break; |
ipv1 | 0:337d7fbe5920 | 61 | } |
ipv1 | 0:337d7fbe5920 | 62 | |
ipv1 | 0:337d7fbe5920 | 63 | client.close(); |
ipv1 | 0:337d7fbe5920 | 64 | } |
ipv1 | 0:337d7fbe5920 | 65 | |
ipv1 | 0:337d7fbe5920 | 66 | } |
ipv1 | 0:337d7fbe5920 | 67 |