Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface Servo mbed-rtos mbed
main.cpp
- Committer:
- marlonprudente
- Date:
- 2017-12-14
- Revision:
- 0:3f38d4693786
- Child:
- 1:7b6dd8cc286d
File content as of revision 0:3f38d4693786:
#include "mbed.h" #include "rtos.h" #include "EthernetInterface.h" AnalogIn pot(A0); PwmOut motor(D7); PwmOut motor1(D6); Serial pc(USBTX, USBRX); EthernetInterface eth; Thread pot; Thread motor; Thread rede; Thread broadcast; const int ECHO_SERVER_PORT = 7; const char* ECHO_SERVER_ADDRESS = "100.43.2.200"; const int BROADCAST_PORT = 58083; bool conectado = false; void pot_thread() { const int potenciometro; while (true) { potenciometro = pot.read(); pc.printf("Pot Thread: %f \n", potenciometro); //Thread::wait(100); } } void pwm_thread() { motor.period(0.020); motor1.period(0.020); while (true) { pc.printf("PWM Thread "); motor.pulsewidth(pot.read()); motor1.pulsewidth(pot.read()); //Thread::wait(100); } } void lan_thread() { printf("\nServer IP Address is %s\n", eth.getIPAddress()); TCPSocketServer server; server.bind(ECHO_SERVER_PORT); server.listen(); while (true) { printf("\nWait for new connection...\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 1500); // Timeout after (1.5)s conectado = true; printf("\nConnection from: %s\n", client.get_address()); char buffer[256]; while (true) { int n = client.receive(buffer, sizeof(buffer)); if (n <= 0) break; // print received message to terminal buffer[n] = '\0'; printf("Received message from Client :'%s'\n",buffer); int pos = buffer[8] - '0'; if(pos == 1)setpoint = 20; else if(pos == 2)setpoint = 35; else if(pos == 3)setpoint = 50; printf("Setpoint: %d\n", setpoint); m_ref.lock(); // ref = atof(buffer); sprintf (buffer, "%s", client.get_address()); m_ref.unlock(); m_ref.lock(); sprintf (buffer, "%s", client.get_address()); m_ref.unlock(); // print sending message to terminal printf("Sending message to Client: '%s'\n",buffer); // Echo received message back to client //client.send_all(buffer, n); // if (n <= 0) break; memset(buffer,0,256); } client.close(); } } void udp_broadcast(){ UDPSocket sock; sock.init(); sock.set_broadcasting(); Endpoint broadcast; broadcast.set_address("255.255.255.255", BROADCAST_PORT); char out_buffer[] = "diego"; while (!conectado) { printf("Broadcasting...\n"); sock.sendTo(broadcast, out_buffer, sizeof(out_buffer)); Thread::wait(1000); } printf("Broadcast finalizado!\n"); } int main() { pc.baud(115200); //inicializa o módulo ethernet eth.init(); eth.connect(); pot.start(pot_thread); motor.start(pwm_thread); rede.start(lan_thread); broadcast.start(udp_broadcast); while (true) { //Thread::wait(500); } }