this program can by compiled and run on several mbeds in a network. The mbed ID is automatically generated. This program is a small test to see if it works. To test the firmware you will need two ( or more ) mbed modules connected to the same network. Pin p5 should be connected with a push button to 3v3 and a resistor of 10k to ground. If the button is pressed the mbed will broadcast a message on ip 239, 192, 1, 100 on port 50000. All mbeds are listening to this ip address and port so they will pick up this message. If the message says MBED xyz LED ON, led1 will id on. If the message says MBED LED OFF, led1 will be off. It\\\'s that simple. So with one mbed you can turn the other mbeds led on or off.
Dependencies: EthernetNetIf NTPClient_NetServices mbed
UDPSocketExample.cpp@2:ae37f80efd23, 2010-11-24 (annotated)
- Committer:
- Schueler
- Date:
- Wed Nov 24 23:46:38 2010 +0000
- Revision:
- 2:ae37f80efd23
- Parent:
- 1:2e2d0b0b57e0
- Child:
- 3:3c33a398189e
0.03
Debug info improved.
Send message to client ID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Schueler | 0:c111a981bfb8 | 1 | #include "mbed.h" |
Schueler | 0:c111a981bfb8 | 2 | #include "EthernetNetIf.h" |
Schueler | 0:c111a981bfb8 | 3 | #include "UDPSocket.h" |
Schueler | 0:c111a981bfb8 | 4 | |
Schueler | 0:c111a981bfb8 | 5 | Serial pc(USBTX, USBRX); |
Schueler | 0:c111a981bfb8 | 6 | EthernetNetIf eth; |
Schueler | 0:c111a981bfb8 | 7 | UDPSocket udp; |
Schueler | 0:c111a981bfb8 | 8 | DigitalOut led(LED1); |
Schueler | 0:c111a981bfb8 | 9 | InterruptIn button(p5); |
Schueler | 1:2e2d0b0b57e0 | 10 | Ticker AliveTicker; |
Schueler | 0:c111a981bfb8 | 11 | |
Schueler | 1:2e2d0b0b57e0 | 12 | // xxx will be replaced by LSByte of the IP address |
Schueler | 1:2e2d0b0b57e0 | 13 | // |
Schueler | 0:c111a981bfb8 | 14 | // Message to let the other know we are alive |
Schueler | 1:2e2d0b0b57e0 | 15 | char strAlive[15]; |
Schueler | 2:ae37f80efd23 | 16 | |
Schueler | 1:2e2d0b0b57e0 | 17 | char mebed_list[16]; |
Schueler | 1:2e2d0b0b57e0 | 18 | |
Schueler | 1:2e2d0b0b57e0 | 19 | char ButtonPressed = 0; |
Schueler | 1:2e2d0b0b57e0 | 20 | char SendAlive = 0; |
Schueler | 1:2e2d0b0b57e0 | 21 | char mbed_id = 0; |
Schueler | 2:ae37f80efd23 | 22 | char mbed_client = 0; |
Schueler | 0:c111a981bfb8 | 23 | |
Schueler | 1:2e2d0b0b57e0 | 24 | void Alive() // ticker routine |
Schueler | 1:2e2d0b0b57e0 | 25 | { |
Schueler | 1:2e2d0b0b57e0 | 26 | SendAlive = 1; |
Schueler | 1:2e2d0b0b57e0 | 27 | } |
Schueler | 1:2e2d0b0b57e0 | 28 | |
Schueler | 1:2e2d0b0b57e0 | 29 | void button_pressed() // irq routine for button |
Schueler | 1:2e2d0b0b57e0 | 30 | { |
Schueler | 1:2e2d0b0b57e0 | 31 | ButtonPressed = 1; |
Schueler | 0:c111a981bfb8 | 32 | } |
Schueler | 0:c111a981bfb8 | 33 | |
Schueler | 0:c111a981bfb8 | 34 | |
Schueler | 0:c111a981bfb8 | 35 | void onUDPSocketEvent(UDPSocketEvent e) |
Schueler | 0:c111a981bfb8 | 36 | { |
Schueler | 0:c111a981bfb8 | 37 | switch(e) |
Schueler | 0:c111a981bfb8 | 38 | { |
Schueler | 0:c111a981bfb8 | 39 | case UDPSOCKET_READABLE: //The only event for now |
Schueler | 0:c111a981bfb8 | 40 | char buf[64] = {0}; |
Schueler | 0:c111a981bfb8 | 41 | Host host; |
Schueler | 0:c111a981bfb8 | 42 | while( int len = udp.recvfrom( buf, 63, &host ) ) |
Schueler | 0:c111a981bfb8 | 43 | { |
Schueler | 0:c111a981bfb8 | 44 | if( len <= 0 ) |
Schueler | 0:c111a981bfb8 | 45 | break; |
Schueler | 0:c111a981bfb8 | 46 | |
Schueler | 0:c111a981bfb8 | 47 | // should implement a parser... |
Schueler | 0:c111a981bfb8 | 48 | // but this will do the trick for the moment |
Schueler | 0:c111a981bfb8 | 49 | if ( strncmp ( buf, "MBED", 4 ) == 0 ) |
Schueler | 0:c111a981bfb8 | 50 | { |
Schueler | 1:2e2d0b0b57e0 | 51 | //pc.printf ("message from %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf ); |
Schueler | 0:c111a981bfb8 | 52 | |
Schueler | 2:ae37f80efd23 | 53 | mbed_client = 0; |
Schueler | 1:2e2d0b0b57e0 | 54 | for ( int i = 5; i < 8; i++ ) |
Schueler | 0:c111a981bfb8 | 55 | { |
Schueler | 1:2e2d0b0b57e0 | 56 | if ( ( buf[i] >= '0' ) & ( buf[i] <= '9' ) ) { mbed_client *=10; mbed_client += ( buf[i] - '0' ); } |
Schueler | 0:c111a981bfb8 | 57 | } |
Schueler | 0:c111a981bfb8 | 58 | if ( strstr (buf, "ALIVE") != NULL ) |
Schueler | 0:c111a981bfb8 | 59 | { |
Schueler | 2:ae37f80efd23 | 60 | pc.printf ("MBED %d is alive\n\r", mbed_client ); |
Schueler | 0:c111a981bfb8 | 61 | } |
Schueler | 0:c111a981bfb8 | 62 | if ( strstr (buf, "LED") != NULL ) |
Schueler | 0:c111a981bfb8 | 63 | { |
Schueler | 2:ae37f80efd23 | 64 | pc.printf ("Message for MBED %d\n\r", mbed_client ); |
Schueler | 0:c111a981bfb8 | 65 | if ( strstr (buf, "ON") != NULL ) { led = 1; } |
Schueler | 0:c111a981bfb8 | 66 | if ( strstr (buf, "OFF") != NULL ) { led = 0; } |
Schueler | 0:c111a981bfb8 | 67 | } |
Schueler | 0:c111a981bfb8 | 68 | } |
Schueler | 0:c111a981bfb8 | 69 | } |
Schueler | 0:c111a981bfb8 | 70 | break; |
Schueler | 0:c111a981bfb8 | 71 | } |
Schueler | 0:c111a981bfb8 | 72 | } |
Schueler | 0:c111a981bfb8 | 73 | |
Schueler | 0:c111a981bfb8 | 74 | int main() { |
Schueler | 0:c111a981bfb8 | 75 | char pos = 0; |
Schueler | 0:c111a981bfb8 | 76 | pc.baud(115200); |
Schueler | 0:c111a981bfb8 | 77 | pc.printf("Ethernet...\n"); |
Schueler | 0:c111a981bfb8 | 78 | EthernetErr ethErr = eth.setup(); |
Schueler | 0:c111a981bfb8 | 79 | if(ethErr) |
Schueler | 0:c111a981bfb8 | 80 | { |
Schueler | 1:2e2d0b0b57e0 | 81 | pc.printf("Error %d in setup.\n", ethErr); |
Schueler | 0:c111a981bfb8 | 82 | return -1; |
Schueler | 0:c111a981bfb8 | 83 | } |
Schueler | 1:2e2d0b0b57e0 | 84 | pc.printf(" OK\n\r"); |
Schueler | 0:c111a981bfb8 | 85 | |
Schueler | 1:2e2d0b0b57e0 | 86 | IpAddr ethIp = eth.getIp(); |
Schueler | 2:ae37f80efd23 | 87 | mbed_id = ethIp[3]; |
Schueler | 2:ae37f80efd23 | 88 | sprintf( strAlive, "MBED %d ALIVE\0", mbed_id ); |
Schueler | 2:ae37f80efd23 | 89 | |
Schueler | 1:2e2d0b0b57e0 | 90 | pc.printf( "%s\n\r", strAlive ); |
Schueler | 1:2e2d0b0b57e0 | 91 | // set Alive Broadcast to 10 seconds |
Schueler | 1:2e2d0b0b57e0 | 92 | AliveTicker.attach(&Alive, 10); |
Schueler | 0:c111a981bfb8 | 93 | Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000 |
Schueler | 0:c111a981bfb8 | 94 | |
Schueler | 0:c111a981bfb8 | 95 | udp.setOnEvent(&onUDPSocketEvent); |
Schueler | 0:c111a981bfb8 | 96 | |
Schueler | 0:c111a981bfb8 | 97 | udp.bind(multicast); |
Schueler | 0:c111a981bfb8 | 98 | |
Schueler | 1:2e2d0b0b57e0 | 99 | udp.sendto( strAlive, strlen(strAlive), &multicast ); |
Schueler | 0:c111a981bfb8 | 100 | |
Schueler | 0:c111a981bfb8 | 101 | button.rise(&button_pressed); |
Schueler | 0:c111a981bfb8 | 102 | while(true) |
Schueler | 0:c111a981bfb8 | 103 | { |
Schueler | 0:c111a981bfb8 | 104 | Net::poll(); |
Schueler | 1:2e2d0b0b57e0 | 105 | if ( SendAlive == 1 ) { udp.sendto( strAlive, strlen(strAlive), &multicast ); SendAlive = 0; } // Send Alive Signal |
Schueler | 1:2e2d0b0b57e0 | 106 | |
Schueler | 1:2e2d0b0b57e0 | 107 | if( ButtonPressed != 0 ) |
Schueler | 0:c111a981bfb8 | 108 | { |
Schueler | 0:c111a981bfb8 | 109 | char strout[20]; |
Schueler | 0:c111a981bfb8 | 110 | pos = !pos; |
Schueler | 2:ae37f80efd23 | 111 | if ( pos == 0 ) sprintf( strout, "MBED %d LED OFF\0", mbed_client ); else sprintf( strout, "MBED %d LED ON\0", mbed_client ); |
Schueler | 0:c111a981bfb8 | 112 | udp.sendto( strout, strlen(strout), &multicast ); |
Schueler | 1:2e2d0b0b57e0 | 113 | //pc.printf("%s\n", str); |
Schueler | 1:2e2d0b0b57e0 | 114 | ButtonPressed = 0; |
Schueler | 0:c111a981bfb8 | 115 | } |
Schueler | 0:c111a981bfb8 | 116 | } |
Schueler | 0:c111a981bfb8 | 117 | |
Schueler | 0:c111a981bfb8 | 118 | |
Schueler | 0:c111a981bfb8 | 119 | } |