
TCP-client with round robin communication -> Added extra function to select address of client with joystick
main.cpp@1:61982a2230eb, 2020-11-08 (annotated)
- Committer:
- timonclaerhout
- Date:
- Sun Nov 08 13:12:21 2020 +0000
- Revision:
- 1:61982a2230eb
- Parent:
- 0:1a75d762a447
TCP-client with round robin communication; -> Added extra function to select address of client with joystick
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pcordemans | 0:1a75d762a447 | 1 | #include "mbed.h" |
pcordemans | 0:1a75d762a447 | 2 | #include "EthernetInterface.h" |
timonclaerhout | 1:61982a2230eb | 3 | #include "LM75B.h" |
timonclaerhout | 1:61982a2230eb | 4 | #include "C12832.h" |
timonclaerhout | 1:61982a2230eb | 5 | #include <string> |
pcordemans | 0:1a75d762a447 | 6 | |
timonclaerhout | 1:61982a2230eb | 7 | //Using Arduino pin notation |
timonclaerhout | 1:61982a2230eb | 8 | C12832 lcd(D11, D13, D12, D7, D10); |
timonclaerhout | 1:61982a2230eb | 9 | LM75B sensor(D14,D15); |
timonclaerhout | 1:61982a2230eb | 10 | |
timonclaerhout | 1:61982a2230eb | 11 | //Potentiometer |
timonclaerhout | 1:61982a2230eb | 12 | AnalogIn pot (A0); |
timonclaerhout | 1:61982a2230eb | 13 | |
timonclaerhout | 1:61982a2230eb | 14 | //Joystick |
timonclaerhout | 1:61982a2230eb | 15 | DigitalIn left(A4); |
timonclaerhout | 1:61982a2230eb | 16 | DigitalIn right(A5); |
timonclaerhout | 1:61982a2230eb | 17 | DigitalIn select(D4); |
pcordemans | 0:1a75d762a447 | 18 | |
timonclaerhout | 1:61982a2230eb | 19 | void select_state(){ |
timonclaerhout | 1:61982a2230eb | 20 | while(1){ |
timonclaerhout | 1:61982a2230eb | 21 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 22 | lcd.locate(0,3); |
timonclaerhout | 1:61982a2230eb | 23 | lcd.printf("Select State by moving the joystick \n"); |
timonclaerhout | 1:61982a2230eb | 24 | lcd.printf("And select by pressing the joystick \n"); |
timonclaerhout | 1:61982a2230eb | 25 | if(left) { |
timonclaerhout | 1:61982a2230eb | 26 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 27 | lcd.printf("The node is a reciever"); |
timonclaerhout | 1:61982a2230eb | 28 | sender = false; |
timonclaerhout | 1:61982a2230eb | 29 | } |
timonclaerhout | 1:61982a2230eb | 30 | if(right){ |
timonclaerhout | 1:61982a2230eb | 31 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 32 | lcd.printf("The node is a sender"); |
timonclaerhout | 1:61982a2230eb | 33 | sender = true; |
timonclaerhout | 1:61982a2230eb | 34 | } |
timonclaerhout | 1:61982a2230eb | 35 | if(select){ |
timonclaerhout | 1:61982a2230eb | 36 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 37 | break; |
timonclaerhout | 1:61982a2230eb | 38 | } |
timonclaerhout | 1:61982a2230eb | 39 | } |
timonclaerhout | 1:61982a2230eb | 40 | } |
timonclaerhout | 1:61982a2230eb | 41 | |
timonclaerhout | 1:61982a2230eb | 42 | void select_address(){ |
timonclaerhout | 1:61982a2230eb | 43 | while(1){ |
timonclaerhout | 1:61982a2230eb | 44 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 45 | lcd.locate(0,3); |
timonclaerhout | 1:61982a2230eb | 46 | lcd.printf("Select your address by moving the joystick \n"); |
timonclaerhout | 1:61982a2230eb | 47 | lcd.printf("And select by pressing the joystick"); |
timonclaerhout | 1:61982a2230eb | 48 | if(left && id_of_sender > 0) { |
timonclaerhout | 1:61982a2230eb | 49 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 50 | id_of_sender -= 1; |
timonclaerhout | 1:61982a2230eb | 51 | lcd.printf("The address is: 192.168.0.", id_of_sender); |
timonclaerhout | 1:61982a2230eb | 52 | } |
timonclaerhout | 1:61982a2230eb | 53 | if(right && id_of_sender < 255){ |
timonclaerhout | 1:61982a2230eb | 54 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 55 | id_of_sender += 1; |
timonclaerhout | 1:61982a2230eb | 56 | lcd.printf("The address is: 192.168.0.", id_of_sender); |
timonclaerhout | 1:61982a2230eb | 57 | } |
timonclaerhout | 1:61982a2230eb | 58 | if(select){ |
timonclaerhout | 1:61982a2230eb | 59 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 60 | break; |
timonclaerhout | 1:61982a2230eb | 61 | } |
timonclaerhout | 1:61982a2230eb | 62 | } |
timonclaerhout | 1:61982a2230eb | 63 | } |
timonclaerhout | 1:61982a2230eb | 64 | |
timonclaerhout | 1:61982a2230eb | 65 | void select_reciever_id(){ |
timonclaerhout | 1:61982a2230eb | 66 | while(1){ |
timonclaerhout | 1:61982a2230eb | 67 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 68 | lcd.locate(0,3); |
timonclaerhout | 1:61982a2230eb | 69 | lcd.printf("Select reciever id by moving the joystick \n"); |
timonclaerhout | 1:61982a2230eb | 70 | lcd.printf("And select by pressing the joystick"); |
timonclaerhout | 1:61982a2230eb | 71 | if(left && id_of_reciever > 0) { |
timonclaerhout | 1:61982a2230eb | 72 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 73 | id_of_reciever -= 1; |
timonclaerhout | 1:61982a2230eb | 74 | lcd.printf("The reciever id is: ", id_of_reciever); |
timonclaerhout | 1:61982a2230eb | 75 | } |
timonclaerhout | 1:61982a2230eb | 76 | if(right && id_of_reciever < 255){ |
timonclaerhout | 1:61982a2230eb | 77 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 78 | id_of_reciever += 1; |
timonclaerhout | 1:61982a2230eb | 79 | lcd.printf("The reciever id is: ", id_of_reciever); |
timonclaerhout | 1:61982a2230eb | 80 | } |
timonclaerhout | 1:61982a2230eb | 81 | if(select){ |
timonclaerhout | 1:61982a2230eb | 82 | lcd.cls(); |
timonclaerhout | 1:61982a2230eb | 83 | break; |
timonclaerhout | 1:61982a2230eb | 84 | } |
timonclaerhout | 1:61982a2230eb | 85 | } |
timonclaerhout | 1:61982a2230eb | 86 | } |
timonclaerhout | 1:61982a2230eb | 87 | |
timonclaerhout | 1:61982a2230eb | 88 | void store_all_values(){ |
timonclaerhout | 1:61982a2230eb | 89 | char upperbyte = (temperature >> 8) & 0xFF; |
timonclaerhout | 1:61982a2230eb | 90 | char lowerbyte = temperature & 0xFF; |
timonclaerhout | 1:61982a2230eb | 91 | char potentiometer = pot/1024 * 256; |
timonclaerhout | 1:61982a2230eb | 92 | buffer[0] = upperbyte; |
timonclaerhout | 1:61982a2230eb | 93 | buffer[1] = lowerbyte; |
timonclaerhout | 1:61982a2230eb | 94 | buffer[2] = potentiometer; |
timonclaerhout | 1:61982a2230eb | 95 | buffer[3] = id_of_sender; |
timonclaerhout | 1:61982a2230eb | 96 | } |
timonclaerhout | 1:61982a2230eb | 97 | |
timonclaerhout | 1:61982a2230eb | 98 | void send_all_values(){ |
timonclaerhout | 1:61982a2230eb | 99 | socket.send(buffer,4); |
timonclaerhout | 1:61982a2230eb | 100 | } |
timonclaerhout | 1:61982a2230eb | 101 | |
timonclaerhout | 1:61982a2230eb | 102 | void connect_server(){ |
timonclaerhout | 1:61982a2230eb | 103 | TCPSocket socket; |
timonclaerhout | 1:61982a2230eb | 104 | string address_reciever = "192.168.0." + id_of_reciever; |
timonclaerhout | 1:61982a2230eb | 105 | socket.open(ð); |
timonclaerhout | 1:61982a2230eb | 106 | socket.connect(address_reciever,4000); |
timonclaerhout | 1:61982a2230eb | 107 | } |
timonclaerhout | 1:61982a2230eb | 108 | |
timonclaerhout | 1:61982a2230eb | 109 | void connect_network(){ |
timonclaerhout | 1:61982a2230eb | 110 | EthernetInterface eth; |
timonclaerhout | 1:61982a2230eb | 111 | string address_sender = "192.168.0." + id_of_sender; |
timonclaerhout | 1:61982a2230eb | 112 | eth.set_network(address_sender,"255.255.255.0","192.168.0.1"); |
timonclaerhout | 1:61982a2230eb | 113 | eth.connect(); |
timonclaerhout | 1:61982a2230eb | 114 | } |
pcordemans | 0:1a75d762a447 | 115 | |
pcordemans | 0:1a75d762a447 | 116 | int main() |
pcordemans | 0:1a75d762a447 | 117 | { |
pcordemans | 0:1a75d762a447 | 118 | printf("Client example\n\r"); |
timonclaerhout | 1:61982a2230eb | 119 | |
pcordemans | 0:1a75d762a447 | 120 | printf("The client IP address is '%s'\n\r", eth.get_ip_address()); |
timonclaerhout | 1:61982a2230eb | 121 | |
timonclaerhout | 1:61982a2230eb | 122 | bool sender = false; |
timonclaerhout | 1:61982a2230eb | 123 | char id_of_sender = 1; |
timonclaerhout | 1:61982a2230eb | 124 | char id_of_reciever = 1; |
timonclaerhout | 1:61982a2230eb | 125 | char buffer[4]; |
pcordemans | 0:1a75d762a447 | 126 | |
timonclaerhout | 1:61982a2230eb | 127 | uint16_t temperature = sensor.temp(); |
timonclaerhout | 1:61982a2230eb | 128 | |
timonclaerhout | 1:61982a2230eb | 129 | select_address(); //What is the IP-address of the node |
timonclaerhout | 1:61982a2230eb | 130 | select_reciever_id(); //each node wether sender/reciever needs to select reciever id (round-robin) |
timonclaerhout | 1:61982a2230eb | 131 | select_state(); // is node a Sender or Reciever? |
timonclaerhout | 1:61982a2230eb | 132 | if(sender){ |
timonclaerhout | 1:61982a2230eb | 133 | connect_network(); //Setup network |
timonclaerhout | 1:61982a2230eb | 134 | connect_server(); //Connect to server (reciever) |
timonclaerhout | 1:61982a2230eb | 135 | store_all_values(); //store the data in buffer |
timonclaerhout | 1:61982a2230eb | 136 | send_all_values(); //send data of buffer to reciever |
timonclaerhout | 1:61982a2230eb | 137 | sender = false; //if packet is send, node becomes reciever again (round-robin) |
timonclaerhout | 1:61982a2230eb | 138 | } |
timonclaerhout | 1:61982a2230eb | 139 | socket.close(); //close communication |
pcordemans | 0:1a75d762a447 | 140 | } |
pcordemans | 0:1a75d762a447 | 141 | |
pcordemans | 0:1a75d762a447 | 142 |