1

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoServer by mbed official

Committer:
TakSolutions
Date:
Mon Oct 30 10:19:46 2017 +0000
Revision:
9:4afb190f911c
Parent:
8:23b1fba109b0
server

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TakSolutions 9:4afb190f911c 1 // TAK Server
TakSolutions 9:4afb190f911c 2
mbed_official 0:38cbb854d85f 3 #include "mbed.h"
emilmont 1:5cebe0e38cd2 4 #include "EthernetInterface.h"
mbed_official 8:23b1fba109b0 5
TakSolutions 9:4afb190f911c 6 #define SERVER_PORT 23
TakSolutions 9:4afb190f911c 7
TakSolutions 9:4afb190f911c 8 DigitalOut myled1(LED1);
TakSolutions 9:4afb190f911c 9 DigitalOut myled2(LED2);
TakSolutions 9:4afb190f911c 10 DigitalOut myled3(LED3);
TakSolutions 9:4afb190f911c 11 DigitalOut myled4(LED4);
TakSolutions 9:4afb190f911c 12 DigitalOut RelayLED(p5);
TakSolutions 9:4afb190f911c 13 DigitalOut RelayPUMP(p6);
mbed_official 8:23b1fba109b0 14
emilmont 1:5cebe0e38cd2 15 int main (void) {
emilmont 1:5cebe0e38cd2 16 EthernetInterface eth;
TakSolutions 9:4afb190f911c 17 //eth.init(); //Use DHCP
TakSolutions 9:4afb190f911c 18 static const char* mbedIp = "192.168.0.102"; //IP
TakSolutions 9:4afb190f911c 19 static const char* mbedMask = "255.255.255.0"; // Mask
TakSolutions 9:4afb190f911c 20 static const char* mbedGateway = "192.168.0.1"; //Gateway
TakSolutions 9:4afb190f911c 21 eth.init(mbedIp,mbedMask,mbedGateway); //Use these parameters for static IP
emilmont 1:5cebe0e38cd2 22 eth.connect();
mbedAustin 7:a5ead1402704 23 printf("\nServer IP Address is %s\n", eth.getIPAddress());
emilmont 1:5cebe0e38cd2 24
emilmont 1:5cebe0e38cd2 25 TCPSocketServer server;
TakSolutions 9:4afb190f911c 26 server.bind(SERVER_PORT);
emilmont 3:36fd3cfad85a 27 server.listen();
emilmont 1:5cebe0e38cd2 28
emilmont 1:5cebe0e38cd2 29 while (true) {
emilmont 1:5cebe0e38cd2 30 printf("\nWait for new connection...\n");
emilmont 1:5cebe0e38cd2 31 TCPSocketConnection client;
emilmont 1:5cebe0e38cd2 32 server.accept(client);
TakSolutions 9:4afb190f911c 33 client.set_blocking(false, 15000); // Timeout after (15)s
emilmont 1:5cebe0e38cd2 34
emilmont 1:5cebe0e38cd2 35 printf("Connection from: %s\n", client.get_address());
emilmont 1:5cebe0e38cd2 36 char buffer[256];
emilmont 1:5cebe0e38cd2 37 while (true) {
emilmont 3:36fd3cfad85a 38 int n = client.receive(buffer, sizeof(buffer));
TakSolutions 9:4afb190f911c 39 if (n <= 0) break;
mbedAustin 7:a5ead1402704 40 // print received message to terminal
mbedAustin 7:a5ead1402704 41 buffer[n] = '\0';
mbedAustin 7:a5ead1402704 42 printf("Received message from Client :'%s'\n",buffer);
mbedAustin 7:a5ead1402704 43
mbedAustin 7:a5ead1402704 44 // reverse the message
mbedAustin 7:a5ead1402704 45 char temp;
mbedAustin 7:a5ead1402704 46 for(int f = 0, l = n-1; f<l; f++,l--){
mbedAustin 7:a5ead1402704 47 temp = buffer[f];
TakSolutions 9:4afb190f911c 48 if(temp=='1')
TakSolutions 9:4afb190f911c 49 {
TakSolutions 9:4afb190f911c 50 myled1 = 1;
TakSolutions 9:4afb190f911c 51 myled2 = 1;
TakSolutions 9:4afb190f911c 52 myled3 = 1;
TakSolutions 9:4afb190f911c 53 myled4 = 1;
TakSolutions 9:4afb190f911c 54 }
TakSolutions 9:4afb190f911c 55 if(temp=='0')
TakSolutions 9:4afb190f911c 56 {
TakSolutions 9:4afb190f911c 57 myled1 = 0;
TakSolutions 9:4afb190f911c 58 myled2 = 0;
TakSolutions 9:4afb190f911c 59 myled3 = 0;
TakSolutions 9:4afb190f911c 60 myled4 = 0;
TakSolutions 9:4afb190f911c 61 }
TakSolutions 9:4afb190f911c 62 if(temp=='5')
TakSolutions 9:4afb190f911c 63 {
TakSolutions 9:4afb190f911c 64 RelayLED = !RelayLED;
TakSolutions 9:4afb190f911c 65 }
TakSolutions 9:4afb190f911c 66 if(temp=='6')
TakSolutions 9:4afb190f911c 67 {
TakSolutions 9:4afb190f911c 68 RelayPUMP = !RelayPUMP;
TakSolutions 9:4afb190f911c 69 }
mbedAustin 7:a5ead1402704 70 buffer[f] = buffer[l];
mbedAustin 7:a5ead1402704 71 buffer[l] = temp;
mbedAustin 7:a5ead1402704 72 }
mbedAustin 7:a5ead1402704 73
mbedAustin 7:a5ead1402704 74 // print reversed message to terminal
mbedAustin 7:a5ead1402704 75 printf("Sending message to Client: '%s'\n",buffer);
mbedAustin 7:a5ead1402704 76
mbedAustin 7:a5ead1402704 77 // Echo received message back to client
emilmont 2:ec5ae99791da 78 client.send_all(buffer, n);
emilmont 1:5cebe0e38cd2 79 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 80 }
emilmont 3:36fd3cfad85a 81
emilmont 1:5cebe0e38cd2 82 client.close();
emilmont 1:5cebe0e38cd2 83 }
emilmont 1:5cebe0e38cd2 84 }
mbed_official 8:23b1fba109b0 85