A Bit funky still

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
ipv1
Date:
Wed Apr 20 09:47:18 2016 +0000
Revision:
1:9a65c6d76db0
Parent:
0:337d7fbe5920
take two

Who changed what in which revision?

UserRevisionLine numberNew 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 1:9a65c6d76db0 6 DigitalOut heartbeatLed(LED_GREEN);
ipv1 1:9a65c6d76db0 7 DigitalOut myled(LED_RED);
ipv1 0:337d7fbe5920 8
ipv1 1:9a65c6d76db0 9 void server_thread(void const* arg) {
ipv1 0:337d7fbe5920 10
ipv1 0:337d7fbe5920 11 TCPSocketServer server;
ipv1 1:9a65c6d76db0 12
ipv1 1:9a65c6d76db0 13 server.set_blocking (true);
ipv1 0:337d7fbe5920 14 server.bind(ECHO_SERVER_PORT);
ipv1 1:9a65c6d76db0 15 server.listen(1);
ipv1 0:337d7fbe5920 16 while (true)
ipv1 0:337d7fbe5920 17 {
ipv1 0:337d7fbe5920 18 //printf("Wait for new connection...\r\n");
ipv1 0:337d7fbe5920 19 TCPSocketConnection client;
ipv1 0:337d7fbe5920 20 server.accept(client);
ipv1 1:9a65c6d76db0 21 client.set_blocking(false, true); // Timeout after (1.5)s
ipv1 0:337d7fbe5920 22
ipv1 0:337d7fbe5920 23 //printf("Connection from: %s\r\n", client.get_address());
ipv1 0:337d7fbe5920 24 char buffer[256];
ipv1 0:337d7fbe5920 25 while (true) {
ipv1 0:337d7fbe5920 26 int n = client.receive(buffer, sizeof(buffer));
ipv1 0:337d7fbe5920 27 if (n <= 0) break;
ipv1 0:337d7fbe5920 28
ipv1 0:337d7fbe5920 29 if(buffer[0]=='Y'){
ipv1 0:337d7fbe5920 30 myled=1;
ipv1 0:337d7fbe5920 31 }else{
ipv1 0:337d7fbe5920 32 myled=0;
ipv1 0:337d7fbe5920 33 }
ipv1 1:9a65c6d76db0 34
ipv1 0:337d7fbe5920 35 sprintf(buffer, "Hi Alisha.How are u?");
ipv1 0:337d7fbe5920 36 client.send_all(buffer, n);
ipv1 0:337d7fbe5920 37 if (n <= 0) break;
ipv1 1:9a65c6d76db0 38 }
ipv1 0:337d7fbe5920 39 client.close();
ipv1 0:337d7fbe5920 40 }
ipv1 1:9a65c6d76db0 41 }
ipv1 1:9a65c6d76db0 42
ipv1 1:9a65c6d76db0 43
ipv1 1:9a65c6d76db0 44 int main (void)
ipv1 1:9a65c6d76db0 45 {
ipv1 1:9a65c6d76db0 46
ipv1 1:9a65c6d76db0 47 //printf("Wait a second...\r\n");
ipv1 1:9a65c6d76db0 48 //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
ipv1 1:9a65c6d76db0 49 static const char* mbedIp = "192.168.64.232"; //IP
ipv1 1:9a65c6d76db0 50 static const char* mbedMask = "255.255.255.0"; // Mask
ipv1 1:9a65c6d76db0 51 static const char* mbedGateway = "192.168.64.1"; //Gateway
ipv1 1:9a65c6d76db0 52
ipv1 1:9a65c6d76db0 53 EthernetInterface eth;
ipv1 1:9a65c6d76db0 54 eth.init(mbedIp, mbedMask, mbedGateway); //Use DHCP
ipv1 1:9a65c6d76db0 55 eth.connect();
ipv1 1:9a65c6d76db0 56 //printf("Server IP Address is %s\r\n", eth.getIPAddress());
ipv1 1:9a65c6d76db0 57
ipv1 1:9a65c6d76db0 58 Thread server(&server_thread);
ipv1 1:9a65c6d76db0 59
ipv1 1:9a65c6d76db0 60 while(true){
ipv1 1:9a65c6d76db0 61 Thread::wait(1000);
ipv1 1:9a65c6d76db0 62 // Heartbeat.
ipv1 1:9a65c6d76db0 63 // The board systematically crashes after a low number of sends from a client,
ipv1 1:9a65c6d76db0 64 // e.g. ~10 times 1000 bytes, ~2-3 times 10000 bytes, etc.
ipv1 1:9a65c6d76db0 65 heartbeatLed = !heartbeatLed;
ipv1 1:9a65c6d76db0 66 }
ipv1 0:337d7fbe5920 67
ipv1 0:337d7fbe5920 68 }
ipv1 0:337d7fbe5920 69