Marlon Prudente / Mbed 2 deprecated SO

Dependencies:   EthernetInterface Servo mbed-rtos mbed

Committer:
marlonprudente
Date:
Thu Dec 14 05:02:37 2017 +0000
Revision:
1:7b6dd8cc286d
Parent:
0:3f38d4693786
Child:
2:f3e4edf4d638
Comunicacao funcionando

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marlonprudente 0:3f38d4693786 1 #include "mbed.h"
marlonprudente 0:3f38d4693786 2 #include "EthernetInterface.h"
marlonprudente 1:7b6dd8cc286d 3 #include <string.h>
marlonprudente 0:3f38d4693786 4
marlonprudente 1:7b6dd8cc286d 5 #define POS_CENTRO 1750
marlonprudente 1:7b6dd8cc286d 6 #define POS_MAX (POS_CENTRO + 110)
marlonprudente 1:7b6dd8cc286d 7 #define POS_MIN (POS_CENTRO - 110)
marlonprudente 1:7b6dd8cc286d 8 #define DIST_MIN 10
marlonprudente 1:7b6dd8cc286d 9 #define DIST_MAX 56
marlonprudente 1:7b6dd8cc286d 10 #define RESOL 1
marlonprudente 1:7b6dd8cc286d 11 #define PERIODO 10
marlonprudente 0:3f38d4693786 12
marlonprudente 1:7b6dd8cc286d 13 const int ECHO_SERVER_PORT = 1234;
marlonprudente 0:3f38d4693786 14 const int BROADCAST_PORT = 58083;
marlonprudente 0:3f38d4693786 15
marlonprudente 0:3f38d4693786 16 bool conectado = false;
marlonprudente 0:3f38d4693786 17
marlonprudente 1:7b6dd8cc286d 18 int setpoint;
marlonprudente 1:7b6dd8cc286d 19
marlonprudente 1:7b6dd8cc286d 20 Thread tcp_server;
marlonprudente 1:7b6dd8cc286d 21 Thread broadcast;
marlonprudente 1:7b6dd8cc286d 22 EthernetInterface eth;
marlonprudente 1:7b6dd8cc286d 23
marlonprudente 1:7b6dd8cc286d 24 Mutex m_dist;
marlonprudente 1:7b6dd8cc286d 25 Mutex m_ref;
marlonprudente 1:7b6dd8cc286d 26
marlonprudente 1:7b6dd8cc286d 27 unsigned int dist;
marlonprudente 1:7b6dd8cc286d 28 //Servo Servo1(D6);
marlonprudente 1:7b6dd8cc286d 29
marlonprudente 1:7b6dd8cc286d 30 void tcp_server_thread ();
marlonprudente 1:7b6dd8cc286d 31 void udp_broadcast ();
marlonprudente 1:7b6dd8cc286d 32
marlonprudente 1:7b6dd8cc286d 33
marlonprudente 1:7b6dd8cc286d 34 int main (void)
marlonprudente 1:7b6dd8cc286d 35 {
marlonprudente 1:7b6dd8cc286d 36 //inicializa o módulo ethernet
marlonprudente 1:7b6dd8cc286d 37 eth.init();
marlonprudente 1:7b6dd8cc286d 38 eth.connect();
marlonprudente 1:7b6dd8cc286d 39
marlonprudente 1:7b6dd8cc286d 40 //Servo1.Enable(POS_CENTRO,20000);
marlonprudente 1:7b6dd8cc286d 41 // inicializa threads
marlonprudente 1:7b6dd8cc286d 42 tcp_server.start(tcp_server_thread);
marlonprudente 1:7b6dd8cc286d 43 broadcast.start(udp_broadcast);
marlonprudente 1:7b6dd8cc286d 44 //controle.start(controle_thread);
marlonprudente 1:7b6dd8cc286d 45
marlonprudente 1:7b6dd8cc286d 46 broadcast.join();
marlonprudente 1:7b6dd8cc286d 47 printf("Todas as thread iniciadas\n");
marlonprudente 1:7b6dd8cc286d 48
marlonprudente 0:3f38d4693786 49 while (true) {
marlonprudente 1:7b6dd8cc286d 50
marlonprudente 0:3f38d4693786 51 }
marlonprudente 0:3f38d4693786 52 }
marlonprudente 0:3f38d4693786 53
marlonprudente 1:7b6dd8cc286d 54 void tcp_server_thread ()
marlonprudente 1:7b6dd8cc286d 55 {
marlonprudente 1:7b6dd8cc286d 56 printf("\nServer IP Address is %s\n", eth.getIPAddress());
marlonprudente 0:3f38d4693786 57
marlonprudente 0:3f38d4693786 58 TCPSocketServer server;
marlonprudente 0:3f38d4693786 59 server.bind(ECHO_SERVER_PORT);
marlonprudente 0:3f38d4693786 60 server.listen();
marlonprudente 1:7b6dd8cc286d 61
marlonprudente 0:3f38d4693786 62 printf("\nWait for new connection...\n");
marlonprudente 0:3f38d4693786 63 TCPSocketConnection client;
marlonprudente 0:3f38d4693786 64 server.accept(client);
marlonprudente 0:3f38d4693786 65 client.set_blocking(false, 1500); // Timeout after (1.5)s
marlonprudente 0:3f38d4693786 66
marlonprudente 0:3f38d4693786 67 conectado = true;
marlonprudente 0:3f38d4693786 68 printf("\nConnection from: %s\n", client.get_address());
marlonprudente 0:3f38d4693786 69 char buffer[256];
marlonprudente 0:3f38d4693786 70 while (true) {
marlonprudente 0:3f38d4693786 71 int n = client.receive(buffer, sizeof(buffer));
marlonprudente 1:7b6dd8cc286d 72
marlonprudente 1:7b6dd8cc286d 73 if (n <=1) {
marlonprudente 1:7b6dd8cc286d 74 //printf("Waiting messenger...");
marlonprudente 1:7b6dd8cc286d 75 //break;
marlonprudente 1:7b6dd8cc286d 76 }else{
marlonprudente 0:3f38d4693786 77
marlonprudente 0:3f38d4693786 78 // print received message to terminal
marlonprudente 0:3f38d4693786 79 buffer[n] = '\0';
marlonprudente 0:3f38d4693786 80 printf("Received message from Client :'%s'\n",buffer);
marlonprudente 1:7b6dd8cc286d 81
marlonprudente 0:3f38d4693786 82 int pos = buffer[8] - '0';
marlonprudente 1:7b6dd8cc286d 83
marlonprudente 0:3f38d4693786 84 if(pos == 1)setpoint = 20;
marlonprudente 0:3f38d4693786 85 else if(pos == 2)setpoint = 35;
marlonprudente 0:3f38d4693786 86 else if(pos == 3)setpoint = 50;
marlonprudente 0:3f38d4693786 87 printf("Setpoint: %d\n", setpoint);
marlonprudente 1:7b6dd8cc286d 88
marlonprudente 0:3f38d4693786 89 m_ref.lock();
marlonprudente 0:3f38d4693786 90 // ref = atof(buffer);
marlonprudente 0:3f38d4693786 91 sprintf (buffer, "%s", client.get_address());
marlonprudente 0:3f38d4693786 92 m_ref.unlock();
marlonprudente 1:7b6dd8cc286d 93
marlonprudente 0:3f38d4693786 94 m_ref.lock();
marlonprudente 0:3f38d4693786 95 sprintf (buffer, "%s", client.get_address());
marlonprudente 0:3f38d4693786 96 m_ref.unlock();
marlonprudente 1:7b6dd8cc286d 97
marlonprudente 0:3f38d4693786 98 // print sending message to terminal
marlonprudente 0:3f38d4693786 99 printf("Sending message to Client: '%s'\n",buffer);
marlonprudente 1:7b6dd8cc286d 100
marlonprudente 0:3f38d4693786 101 // Echo received message back to client
marlonprudente 0:3f38d4693786 102 //client.send_all(buffer, n);
marlonprudente 0:3f38d4693786 103 // if (n <= 0) break;
marlonprudente 0:3f38d4693786 104 memset(buffer,0,256);
marlonprudente 1:7b6dd8cc286d 105 }
marlonprudente 0:3f38d4693786 106 }
marlonprudente 0:3f38d4693786 107
marlonprudente 1:7b6dd8cc286d 108 client.close();
marlonprudente 0:3f38d4693786 109 }
marlonprudente 0:3f38d4693786 110
marlonprudente 1:7b6dd8cc286d 111 void udp_broadcast ()
marlonprudente 1:7b6dd8cc286d 112 {
marlonprudente 0:3f38d4693786 113 UDPSocket sock;
marlonprudente 0:3f38d4693786 114 sock.init();
marlonprudente 0:3f38d4693786 115 sock.set_broadcasting();
marlonprudente 0:3f38d4693786 116
marlonprudente 0:3f38d4693786 117 Endpoint broadcast;
marlonprudente 0:3f38d4693786 118 broadcast.set_address("255.255.255.255", BROADCAST_PORT);
marlonprudente 0:3f38d4693786 119
marlonprudente 1:7b6dd8cc286d 120 char out_buffer[] = "Coppeti";
marlonprudente 0:3f38d4693786 121
marlonprudente 0:3f38d4693786 122 while (!conectado) {
marlonprudente 0:3f38d4693786 123 printf("Broadcasting...\n");
marlonprudente 0:3f38d4693786 124 sock.sendTo(broadcast, out_buffer, sizeof(out_buffer));
marlonprudente 0:3f38d4693786 125 Thread::wait(1000);
marlonprudente 0:3f38d4693786 126 }
marlonprudente 0:3f38d4693786 127 printf("Broadcast finalizado!\n");
marlonprudente 0:3f38d4693786 128 }