TCP-client with round robin communication -> Added extra function to select address of client with joystick

Dependencies:   C12832 LM75B

Files at this revision

API Documentation at this revision

Comitter:
timonclaerhout
Date:
Sun Nov 08 13:12:21 2020 +0000
Parent:
0:1a75d762a447
Commit message:
TCP-client with round robin communication; -> Added extra function to select address of client with joystick

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
LM75B.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Sun Nov 08 13:12:21 2020 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/chris/code/C12832/#7de323fa46fe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Sun Nov 08 13:12:21 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/neilt6/code/LM75B/#fc27dc535ea9
--- a/main.cpp	Sat Mar 07 19:29:08 2020 +0000
+++ b/main.cpp	Sun Nov 08 13:12:21 2020 +0000
@@ -1,34 +1,142 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "LM75B.h"
+#include "C12832.h"
+#include <string>
 
+//Using Arduino pin notation
+C12832 lcd(D11, D13, D12, D7, D10);
+LM75B sensor(D14,D15);
+
+//Potentiometer
+AnalogIn pot (A0);
+
+//Joystick
+DigitalIn left(A4);
+DigitalIn right(A5);
+DigitalIn select(D4);
 
-DigitalOut led(LED1);
+void select_state(){
+        while(1){
+                lcd.cls();
+                lcd.locate(0,3);
+                lcd.printf("Select State by moving the joystick \n");
+                lcd.printf("And select by pressing the joystick \n");
+                if(left) {
+                    lcd.cls();
+                    lcd.printf("The node is a reciever");
+                    sender = false;
+                    }
+                if(right){
+                    lcd.cls();
+                    lcd.printf("The node is a sender");
+                    sender = true;
+                    }
+                if(select){
+                    lcd.cls();
+                    break;
+                    }
+            }
+    }
+    
+void select_address(){
+        while(1){
+                lcd.cls();
+                lcd.locate(0,3);
+                lcd.printf("Select your address by moving the joystick \n");
+                lcd.printf("And select by pressing the joystick");
+                if(left && id_of_sender > 0) {
+                    lcd.cls();
+                    id_of_sender -= 1;
+                    lcd.printf("The address is: 192.168.0.", id_of_sender);
+                    }
+                if(right && id_of_sender < 255){
+                    lcd.cls();
+                    id_of_sender += 1;
+                    lcd.printf("The address is: 192.168.0.", id_of_sender);
+                    }
+                if(select){
+                    lcd.cls();
+                    break;
+                    }
+            }
+    }
+    
+void select_reciever_id(){
+        while(1){
+                lcd.cls();
+                lcd.locate(0,3);
+                lcd.printf("Select reciever id by moving the joystick \n");
+                lcd.printf("And select by pressing the joystick");
+                if(left && id_of_reciever > 0) {
+                    lcd.cls();
+                    id_of_reciever -= 1;
+                    lcd.printf("The reciever id is: ", id_of_reciever);
+                    }
+                if(right && id_of_reciever < 255){
+                    lcd.cls();
+                    id_of_reciever += 1;
+                    lcd.printf("The reciever id is: ", id_of_reciever);
+                    }
+                if(select){
+                    lcd.cls();
+                    break;
+                    }
+            }
+    }
+    
+void store_all_values(){
+    char upperbyte = (temperature >> 8) & 0xFF;
+    char lowerbyte = temperature & 0xFF;
+    char potentiometer = pot/1024 * 256;
+    buffer[0] = upperbyte;
+    buffer[1] = lowerbyte;
+    buffer[2] = potentiometer;
+    buffer[3] = id_of_sender;
+    }
+    
+void send_all_values(){
+    socket.send(buffer,4);
+    }
+    
+void connect_server(){
+    TCPSocket socket;
+    string address_reciever = "192.168.0." + id_of_reciever;
+    socket.open(&eth);
+    socket.connect(address_reciever,4000);
+    }
+
+void connect_network(){
+    EthernetInterface eth;
+    string address_sender = "192.168.0." + id_of_sender;
+    eth.set_network(address_sender,"255.255.255.0","192.168.0.1");
+    eth.connect();
+    }
 
 int main()
 {
     printf("Client example\n\r");
-
-    EthernetInterface eth;
-    eth.set_network("192.168.0.39","255.255.255.0","192.168.0.1");
-    eth.connect();
-
+    
     printf("The client IP address is '%s'\n\r", eth.get_ip_address());
-
-
-
+    
+    bool sender = false;
+    char id_of_sender = 1;
+    char id_of_reciever = 1;
+    char buffer[4];
 
-    TCPSocket socket;
-
-    socket.open(&eth);
-    socket.connect("192.168.0.40",4000);
-
-    char rbuffer[64];
-    int rcount = socket.recv(rbuffer, sizeof rbuffer);
-    printf("received: %d\r\n", rcount);
-    printf(rbuffer);
-
-    socket.close();
-
+    uint16_t temperature = sensor.temp();
+    
+    select_address(); //What is the IP-address of the node
+    select_reciever_id(); //each node wether sender/reciever needs to select reciever id (round-robin)
+    select_state(); // is node a Sender or Reciever?
+    if(sender){
+        connect_network(); //Setup network
+        connect_server(); //Connect to server (reciever)
+        store_all_values(); //store the data in buffer
+        send_all_values();  //send data of buffer to reciever
+        sender = false; //if packet is send, node becomes reciever again (round-robin)
+        }
+    socket.close(); //close communication
 }