Timon Claerhout
/
tcp-server
main.cpp@2:f2188521d606, 2020-11-08 (annotated)
- Committer:
- timonclaerhout
- Date:
- Sun Nov 08 13:08:36 2020 +0000
- Revision:
- 2:f2188521d606
- Parent:
- 1:b658dfbe2a7c
TCP-server with round robin communication
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
deepikabhavnani | 0:ddb5698aa782 | 1 | #include "mbed.h" |
deepikabhavnani | 0:ddb5698aa782 | 2 | #include "EthernetInterface.h" |
timonclaerhout | 2:f2188521d606 | 3 | #include <string> |
pcordemans | 1:b658dfbe2a7c | 4 | |
timonclaerhout | 2:f2188521d606 | 5 | //RGB led |
timonclaerhout | 2:f2188521d606 | 6 | PwmOut r (D5); |
timonclaerhout | 2:f2188521d606 | 7 | PwmOut g (D8); |
timonclaerhout | 2:f2188521d606 | 8 | PwmOut b (D9); |
pcordemans | 1:b658dfbe2a7c | 9 | |
pcordemans | 1:b658dfbe2a7c | 10 | DigitalOut led(LED1); |
deepikabhavnani | 0:ddb5698aa782 | 11 | |
timonclaerhout | 2:f2188521d606 | 12 | void select_state(){ |
timonclaerhout | 2:f2188521d606 | 13 | while(1){ |
timonclaerhout | 2:f2188521d606 | 14 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 15 | lcd.locate(0,3); |
timonclaerhout | 2:f2188521d606 | 16 | lcd.printf("Select State by moving the joystick \n"); |
timonclaerhout | 2:f2188521d606 | 17 | lcd.printf("And select by pressing the joystick \n"); |
timonclaerhout | 2:f2188521d606 | 18 | if(left) { |
timonclaerhout | 2:f2188521d606 | 19 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 20 | lcd.printf("The node is a reciever"); |
timonclaerhout | 2:f2188521d606 | 21 | sender = false; |
timonclaerhout | 2:f2188521d606 | 22 | } |
timonclaerhout | 2:f2188521d606 | 23 | if(right){ |
timonclaerhout | 2:f2188521d606 | 24 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 25 | lcd.printf("The node is a sender"); |
timonclaerhout | 2:f2188521d606 | 26 | sender = true; |
timonclaerhout | 2:f2188521d606 | 27 | } |
timonclaerhout | 2:f2188521d606 | 28 | if(select){ |
timonclaerhout | 2:f2188521d606 | 29 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 30 | break; |
timonclaerhout | 2:f2188521d606 | 31 | } |
timonclaerhout | 2:f2188521d606 | 32 | } |
timonclaerhout | 2:f2188521d606 | 33 | } |
timonclaerhout | 2:f2188521d606 | 34 | |
timonclaerhout | 2:f2188521d606 | 35 | void select_reciever_id(){ |
timonclaerhout | 2:f2188521d606 | 36 | while(1){ |
timonclaerhout | 2:f2188521d606 | 37 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 38 | lcd.locate(0,3); |
timonclaerhout | 2:f2188521d606 | 39 | lcd.printf("Select reciever id by moving the joystick \n"); |
timonclaerhout | 2:f2188521d606 | 40 | lcd.printf("And select by pressing the joystick"); |
timonclaerhout | 2:f2188521d606 | 41 | if(left && id_of_reciever > 0) { |
timonclaerhout | 2:f2188521d606 | 42 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 43 | id_of_reciever -= 1; |
timonclaerhout | 2:f2188521d606 | 44 | lcd.printf("The reciever id is: ", id_of_reciever); |
timonclaerhout | 2:f2188521d606 | 45 | } |
timonclaerhout | 2:f2188521d606 | 46 | if(right && id_of_reciever < 255){ |
timonclaerhout | 2:f2188521d606 | 47 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 48 | id_of_reciever += 1; |
timonclaerhout | 2:f2188521d606 | 49 | lcd.printf("The reciever id is: ", id_of_reciever); |
timonclaerhout | 2:f2188521d606 | 50 | } |
timonclaerhout | 2:f2188521d606 | 51 | if(select){ |
timonclaerhout | 2:f2188521d606 | 52 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 53 | break; |
timonclaerhout | 2:f2188521d606 | 54 | } |
timonclaerhout | 2:f2188521d606 | 55 | } |
timonclaerhout | 2:f2188521d606 | 56 | } |
timonclaerhout | 2:f2188521d606 | 57 | |
timonclaerhout | 2:f2188521d606 | 58 | void rgb_led(){ |
timonclaerhout | 2:f2188521d606 | 59 | r.period(0.001); |
timonclaerhout | 2:f2188521d606 | 60 | while(1) { |
timonclaerhout | 2:f2188521d606 | 61 | for(float i = buffer[2]/255; i < 1.0 ; i += 0.001) { |
timonclaerhout | 2:f2188521d606 | 62 | float p = 3 * i; |
timonclaerhout | 2:f2188521d606 | 63 | r = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0); |
timonclaerhout | 2:f2188521d606 | 64 | g = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p); |
timonclaerhout | 2:f2188521d606 | 65 | b = 1.0 - ((p < 1.0) ? 0.0 : (p > 2.0) ? 3.0 - p : p - 1.0); ; |
timonclaerhout | 2:f2188521d606 | 66 | wait (0.01); |
timonclaerhout | 2:f2188521d606 | 67 | } |
timonclaerhout | 2:f2188521d606 | 68 | } |
timonclaerhout | 2:f2188521d606 | 69 | } |
timonclaerhout | 2:f2188521d606 | 70 | |
timonclaerhout | 2:f2188521d606 | 71 | void display_temperature(){ |
timonclaerhout | 2:f2188521d606 | 72 | lcd.cls(); |
timonclaerhout | 2:f2188521d606 | 73 | lcd.locate(0,3); |
timonclaerhout | 2:f2188521d606 | 74 | lcd.printf("The recieved temperature is: ", buffer[0]); |
timonclaerhout | 2:f2188521d606 | 75 | lcd.printf(",", buffer[1]); |
timonclaerhout | 2:f2188521d606 | 76 | } |
timonclaerhout | 2:f2188521d606 | 77 | |
timonclaerhout | 2:f2188521d606 | 78 | void connect_server(int port) { |
pcordemans | 1:b658dfbe2a7c | 79 | printf("Server example\n\r"); |
deepikabhavnani | 0:ddb5698aa782 | 80 | |
deepikabhavnani | 0:ddb5698aa782 | 81 | EthernetInterface eth; |
pcordemans | 1:b658dfbe2a7c | 82 | eth.set_network("192.168.0.40","255.255.255.0","192.168.0.1"); |
deepikabhavnani | 0:ddb5698aa782 | 83 | eth.connect(); |
deepikabhavnani | 0:ddb5698aa782 | 84 | |
pcordemans | 1:b658dfbe2a7c | 85 | printf("The Server IP address is '%s'\n\r", eth.get_ip_address()); |
pcordemans | 1:b658dfbe2a7c | 86 | |
pcordemans | 1:b658dfbe2a7c | 87 | TCPServer srv(ð); |
deepikabhavnani | 0:ddb5698aa782 | 88 | |
timonclaerhout | 2:f2188521d606 | 89 | srv.bind(port); |
deepikabhavnani | 0:ddb5698aa782 | 90 | |
pcordemans | 1:b658dfbe2a7c | 91 | srv.listen(); |
timonclaerhout | 2:f2188521d606 | 92 | } |
deepikabhavnani | 0:ddb5698aa782 | 93 | |
timonclaerhout | 2:f2188521d606 | 94 | void set_connection(){ |
pcordemans | 1:b658dfbe2a7c | 95 | TCPSocket client; |
pcordemans | 1:b658dfbe2a7c | 96 | SocketAddress client_addr; |
pcordemans | 1:b658dfbe2a7c | 97 | |
pcordemans | 1:b658dfbe2a7c | 98 | srv.accept(&client, &client_addr); |
pcordemans | 1:b658dfbe2a7c | 99 | |
pcordemans | 1:b658dfbe2a7c | 100 | printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), |
deepikabhavnani | 0:ddb5698aa782 | 101 | client_addr.get_port()); |
timonclaerhout | 2:f2188521d606 | 102 | } |
timonclaerhout | 2:f2188521d606 | 103 | |
timonclaerhout | 2:f2188521d606 | 104 | int main() |
timonclaerhout | 2:f2188521d606 | 105 | { |
timonclaerhout | 2:f2188521d606 | 106 | bool sender = false; |
timonclaerhout | 2:f2188521d606 | 107 | char buffer[255]; |
timonclaerhout | 2:f2188521d606 | 108 | int i = 4; //value of i is 4 because the first 4 items in the buffer has already values stored(buffer[0],buffer[1],buffer[2],buffer[3],) so we have to add values starting with buffer[4] |
timonclaerhout | 2:f2188521d606 | 109 | int id_of_reciever = 1; |
timonclaerhout | 2:f2188521d606 | 110 | |
timonclaerhout | 2:f2188521d606 | 111 | connect_server(4000); //connect server on port 4000 |
deepikabhavnani | 0:ddb5698aa782 | 112 | |
timonclaerhout | 2:f2188521d606 | 113 | while(true){ |
timonclaerhout | 2:f2188521d606 | 114 | |
timonclaerhout | 2:f2188521d606 | 115 | set_connection(); //connect with client |
timonclaerhout | 2:f2188521d606 | 116 | |
timonclaerhout | 2:f2188521d606 | 117 | select_state(); //Is the node sender/reciever? |
timonclaerhout | 2:f2188521d606 | 118 | select_reciever_id(); //each node wether sender/reciever needs to select reciever id (round-robin) |
timonclaerhout | 2:f2188521d606 | 119 | |
timonclaerhout | 2:f2188521d606 | 120 | if(sender = false){ |
timonclaerhout | 2:f2188521d606 | 121 | do{ |
timonclaerhout | 2:f2188521d606 | 122 | int n = client.receive(buffer, sizeof(buffer)); |
timonclaerhout | 2:f2188521d606 | 123 | if(n > 0){ //Makes sure the packet is recieved |
timonclaerhout | 2:f2188521d606 | 124 | size_t found = client_addr.get_ip_address().find_last_of("."); //find last '.' of the ip address ex. 192.168.0.13 |
timonclaerhout | 2:f2188521d606 | 125 | buffer[i] = client_addr.get_ip_address().substr(found+1); //Give the number after the last '.' in this ex. 13 that is the sender ID |
timonclaerhout | 2:f2188521d606 | 126 | i++; |
timonclaerhout | 2:f2188521d606 | 127 | //Other examples at: http://www.cplusplus.com/reference/string/string/find_last_of/ |
timonclaerhout | 2:f2188521d606 | 128 | client.send(buffer, sizeof(buffer)); |
timonclaerhout | 2:f2188521d606 | 129 | } |
timonclaerhout | 2:f2188521d606 | 130 | display_temperature(); //Display recieved temperature of sender on lcd |
timonclaerhout | 2:f2188521d606 | 131 | rgb_led(); //Display rgb_led with recieved pwm value of sender |
timonclaerhout | 2:f2188521d606 | 132 | }while(buffer[3] != buffer[i]) //Keeps looping untill the ID of the first sender ID is the same |
timonclaerhout | 2:f2188521d606 | 133 | |
timonclaerhout | 2:f2188521d606 | 134 | } |
timonclaerhout | 2:f2188521d606 | 135 | client.close(); //Stop communication with client |
pcordemans | 1:b658dfbe2a7c | 136 | |
pcordemans | 1:b658dfbe2a7c | 137 | } |
deepikabhavnani | 0:ddb5698aa782 | 138 | } |