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

Committer:
Schueler
Date:
Wed Nov 24 23:32:52 2010 +0000
Revision:
1:2e2d0b0b57e0
Parent:
0:c111a981bfb8
Child:
2:ae37f80efd23
0.02
added automatic mbed ID mechanism.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:c111a981bfb8 16 // Address other MBED to turn on the LED
Schueler 1:2e2d0b0b57e0 17 const char* strON = "MBED yyy LED ON \0";
Schueler 1:2e2d0b0b57e0 18 const char* strOFF = "MBED yyy LED OFF \0";
Schueler 1:2e2d0b0b57e0 19 char mebed_list[16];
Schueler 1:2e2d0b0b57e0 20
Schueler 1:2e2d0b0b57e0 21 char ButtonPressed = 0;
Schueler 1:2e2d0b0b57e0 22 char SendAlive = 0;
Schueler 1:2e2d0b0b57e0 23 char mbed_id = 0;
Schueler 0:c111a981bfb8 24
Schueler 1:2e2d0b0b57e0 25 void Alive() // ticker routine
Schueler 1:2e2d0b0b57e0 26 {
Schueler 1:2e2d0b0b57e0 27 SendAlive = 1;
Schueler 1:2e2d0b0b57e0 28 }
Schueler 1:2e2d0b0b57e0 29
Schueler 1:2e2d0b0b57e0 30 void button_pressed() // irq routine for button
Schueler 1:2e2d0b0b57e0 31 {
Schueler 1:2e2d0b0b57e0 32 ButtonPressed = 1;
Schueler 0:c111a981bfb8 33 }
Schueler 0:c111a981bfb8 34
Schueler 0:c111a981bfb8 35
Schueler 0:c111a981bfb8 36 void onUDPSocketEvent(UDPSocketEvent e)
Schueler 0:c111a981bfb8 37 {
Schueler 0:c111a981bfb8 38 switch(e)
Schueler 0:c111a981bfb8 39 {
Schueler 0:c111a981bfb8 40 case UDPSOCKET_READABLE: //The only event for now
Schueler 0:c111a981bfb8 41 char buf[64] = {0};
Schueler 0:c111a981bfb8 42 Host host;
Schueler 0:c111a981bfb8 43 while( int len = udp.recvfrom( buf, 63, &host ) )
Schueler 0:c111a981bfb8 44 {
Schueler 0:c111a981bfb8 45 if( len <= 0 )
Schueler 0:c111a981bfb8 46 break;
Schueler 0:c111a981bfb8 47
Schueler 0:c111a981bfb8 48 // should implement a parser...
Schueler 0:c111a981bfb8 49 // but this will do the trick for the moment
Schueler 0:c111a981bfb8 50 if ( strncmp ( buf, "MBED", 4 ) == 0 )
Schueler 0:c111a981bfb8 51 {
Schueler 1:2e2d0b0b57e0 52 //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 53
Schueler 0:c111a981bfb8 54 int mbed_client = 0;
Schueler 1:2e2d0b0b57e0 55 for ( int i = 5; i < 8; i++ )
Schueler 0:c111a981bfb8 56 {
Schueler 1:2e2d0b0b57e0 57 if ( ( buf[i] >= '0' ) & ( buf[i] <= '9' ) ) { mbed_client *=10; mbed_client += ( buf[i] - '0' ); }
Schueler 0:c111a981bfb8 58 }
Schueler 0:c111a981bfb8 59 if ( strstr (buf, "ALIVE") != NULL )
Schueler 0:c111a981bfb8 60 {
Schueler 0:c111a981bfb8 61 pc.printf ("MBED %d is alive\n", mbed_client );
Schueler 0:c111a981bfb8 62 }
Schueler 0:c111a981bfb8 63 if ( strstr (buf, "LED") != NULL )
Schueler 0:c111a981bfb8 64 {
Schueler 1:2e2d0b0b57e0 65 pc.printf ("MBED %d send a message\n", mbed_client );
Schueler 0:c111a981bfb8 66 if ( strstr (buf, "ON") != NULL ) { led = 1; }
Schueler 0:c111a981bfb8 67 if ( strstr (buf, "OFF") != NULL ) { led = 0; }
Schueler 0:c111a981bfb8 68 }
Schueler 0:c111a981bfb8 69 }
Schueler 0:c111a981bfb8 70 }
Schueler 0:c111a981bfb8 71 break;
Schueler 0:c111a981bfb8 72 }
Schueler 0:c111a981bfb8 73 }
Schueler 0:c111a981bfb8 74
Schueler 0:c111a981bfb8 75 int main() {
Schueler 0:c111a981bfb8 76 char pos = 0;
Schueler 0:c111a981bfb8 77 pc.baud(115200);
Schueler 0:c111a981bfb8 78 pc.printf("Ethernet...\n");
Schueler 0:c111a981bfb8 79 EthernetErr ethErr = eth.setup();
Schueler 0:c111a981bfb8 80 if(ethErr)
Schueler 0:c111a981bfb8 81 {
Schueler 1:2e2d0b0b57e0 82 pc.printf("Error %d in setup.\n", ethErr);
Schueler 0:c111a981bfb8 83 return -1;
Schueler 0:c111a981bfb8 84 }
Schueler 1:2e2d0b0b57e0 85 pc.printf(" OK\n\r");
Schueler 0:c111a981bfb8 86
Schueler 1:2e2d0b0b57e0 87 IpAddr ethIp = eth.getIp();
Schueler 1:2e2d0b0b57e0 88 sprintf( strAlive, "MBED %d ALIVE\0", ethIp[3] );
Schueler 1:2e2d0b0b57e0 89 pc.printf( "%s\n\r", strAlive );
Schueler 1:2e2d0b0b57e0 90 // set Alive Broadcast to 10 seconds
Schueler 1:2e2d0b0b57e0 91 AliveTicker.attach(&Alive, 10);
Schueler 0:c111a981bfb8 92 Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000
Schueler 0:c111a981bfb8 93
Schueler 0:c111a981bfb8 94 udp.setOnEvent(&onUDPSocketEvent);
Schueler 0:c111a981bfb8 95
Schueler 0:c111a981bfb8 96 udp.bind(multicast);
Schueler 0:c111a981bfb8 97
Schueler 1:2e2d0b0b57e0 98 udp.sendto( strAlive, strlen(strAlive), &multicast );
Schueler 0:c111a981bfb8 99
Schueler 0:c111a981bfb8 100 button.rise(&button_pressed);
Schueler 0:c111a981bfb8 101 while(true)
Schueler 0:c111a981bfb8 102 {
Schueler 0:c111a981bfb8 103 Net::poll();
Schueler 1:2e2d0b0b57e0 104 if ( SendAlive == 1 ) { udp.sendto( strAlive, strlen(strAlive), &multicast ); SendAlive = 0; } // Send Alive Signal
Schueler 1:2e2d0b0b57e0 105
Schueler 1:2e2d0b0b57e0 106 if( ButtonPressed != 0 )
Schueler 0:c111a981bfb8 107 {
Schueler 0:c111a981bfb8 108 char strout[20];
Schueler 0:c111a981bfb8 109 pos = !pos;
Schueler 0:c111a981bfb8 110 if ( pos == 0 ) strncpy( strout, strOFF, 17 ); else strncpy( strout, strON, 17 );
Schueler 0:c111a981bfb8 111 udp.sendto( strout, strlen(strout), &multicast );
Schueler 1:2e2d0b0b57e0 112 //pc.printf("%s\n", str);
Schueler 1:2e2d0b0b57e0 113 ButtonPressed = 0;
Schueler 0:c111a981bfb8 114 }
Schueler 0:c111a981bfb8 115 }
Schueler 0:c111a981bfb8 116
Schueler 0:c111a981bfb8 117
Schueler 0:c111a981bfb8 118 }