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 15:49:45 2010 +0000
Revision:
0:c111a981bfb8
Child:
1:2e2d0b0b57e0
0.01 Initial test

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 LocalFileSystem local("local");
Schueler 0:c111a981bfb8 6 Serial pc(USBTX, USBRX);
Schueler 0:c111a981bfb8 7 EthernetNetIf eth;
Schueler 0:c111a981bfb8 8 UDPSocket udp;
Schueler 0:c111a981bfb8 9 DigitalOut led(LED1);
Schueler 0:c111a981bfb8 10 InterruptIn button(p5);
Schueler 0:c111a981bfb8 11
Schueler 0:c111a981bfb8 12 // Message to let the other know we are alive
Schueler 0:c111a981bfb8 13 char* str = "MBED 001 ALIVE\0";
Schueler 0:c111a981bfb8 14 // Address other MBED to turn on the LED
Schueler 0:c111a981bfb8 15 const char* strON = "MBED 002 LED ON \0";
Schueler 0:c111a981bfb8 16 const char* strOFF = "MBED 002 LED OFF \0";
Schueler 0:c111a981bfb8 17
Schueler 0:c111a981bfb8 18 char dummy = 0;
Schueler 0:c111a981bfb8 19 char mbed_id = 0;
Schueler 0:c111a981bfb8 20 void button_pressed() {
Schueler 0:c111a981bfb8 21 dummy = 1;
Schueler 0:c111a981bfb8 22 }
Schueler 0:c111a981bfb8 23
Schueler 0:c111a981bfb8 24
Schueler 0:c111a981bfb8 25 void onUDPSocketEvent(UDPSocketEvent e)
Schueler 0:c111a981bfb8 26 {
Schueler 0:c111a981bfb8 27 switch(e)
Schueler 0:c111a981bfb8 28 {
Schueler 0:c111a981bfb8 29 case UDPSOCKET_READABLE: //The only event for now
Schueler 0:c111a981bfb8 30 char buf[64] = {0};
Schueler 0:c111a981bfb8 31 Host host;
Schueler 0:c111a981bfb8 32 while( int len = udp.recvfrom( buf, 63, &host ) )
Schueler 0:c111a981bfb8 33 {
Schueler 0:c111a981bfb8 34 if( len <= 0 )
Schueler 0:c111a981bfb8 35 break;
Schueler 0:c111a981bfb8 36
Schueler 0:c111a981bfb8 37 // should implement a parser...
Schueler 0:c111a981bfb8 38 // but this will do the trick for the moment
Schueler 0:c111a981bfb8 39 if ( strncmp ( buf, "MBED", 4 ) == 0 )
Schueler 0:c111a981bfb8 40 {
Schueler 0:c111a981bfb8 41 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 42
Schueler 0:c111a981bfb8 43 int mbed_client = 0;
Schueler 0:c111a981bfb8 44 for ( int i = 5; i < 9; i++ )
Schueler 0:c111a981bfb8 45 {
Schueler 0:c111a981bfb8 46 if ( ( buf[i] >= '0' ) & ( buf[i] <= '9' ) ) { mbed_client += ( buf[i] - '0' ); }
Schueler 0:c111a981bfb8 47 }
Schueler 0:c111a981bfb8 48 if ( strstr (buf, "ALIVE") != NULL )
Schueler 0:c111a981bfb8 49 {
Schueler 0:c111a981bfb8 50 pc.printf ("MBED %d is alive\n", mbed_client );
Schueler 0:c111a981bfb8 51 }
Schueler 0:c111a981bfb8 52 if ( strstr (buf, "LED") != NULL )
Schueler 0:c111a981bfb8 53 {
Schueler 0:c111a981bfb8 54 if ( strstr (buf, "ON") != NULL ) { led = 1; }
Schueler 0:c111a981bfb8 55 if ( strstr (buf, "OFF") != NULL ) { led = 0; }
Schueler 0:c111a981bfb8 56 }
Schueler 0:c111a981bfb8 57 }
Schueler 0:c111a981bfb8 58
Schueler 0:c111a981bfb8 59 }
Schueler 0:c111a981bfb8 60 break;
Schueler 0:c111a981bfb8 61 }
Schueler 0:c111a981bfb8 62 }
Schueler 0:c111a981bfb8 63
Schueler 0:c111a981bfb8 64 int main() {
Schueler 0:c111a981bfb8 65 char pos = 0;
Schueler 0:c111a981bfb8 66 pc.baud(115200);
Schueler 0:c111a981bfb8 67 pc.printf("Ethernet...\n");
Schueler 0:c111a981bfb8 68 EthernetErr ethErr = eth.setup();
Schueler 0:c111a981bfb8 69 if(ethErr)
Schueler 0:c111a981bfb8 70 {
Schueler 0:c111a981bfb8 71 printf("Error %d in setup.\n", ethErr);
Schueler 0:c111a981bfb8 72 return -1;
Schueler 0:c111a981bfb8 73 }
Schueler 0:c111a981bfb8 74 pc.printf(" OK\n");
Schueler 0:c111a981bfb8 75
Schueler 0:c111a981bfb8 76 /*
Schueler 0:c111a981bfb8 77 FILE *fp = fopen ("/local/mbed_udp.cfg", "r" );
Schueler 0:c111a981bfb8 78 if(!fp) {
Schueler 0:c111a981bfb8 79 printf("File /local/mbed_udp.cfg could not be opened!\n");
Schueler 0:c111a981bfb8 80 exit(1);
Schueler 0:c111a981bfb8 81 }
Schueler 0:c111a981bfb8 82 str[5] = fgetc ( fp );
Schueler 0:c111a981bfb8 83 fclose(fp);
Schueler 0:c111a981bfb8 84 */
Schueler 0:c111a981bfb8 85
Schueler 0:c111a981bfb8 86
Schueler 0:c111a981bfb8 87 Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000
Schueler 0:c111a981bfb8 88
Schueler 0:c111a981bfb8 89 udp.setOnEvent(&onUDPSocketEvent);
Schueler 0:c111a981bfb8 90
Schueler 0:c111a981bfb8 91 udp.bind(multicast);
Schueler 0:c111a981bfb8 92
Schueler 0:c111a981bfb8 93 udp.sendto( str, strlen(str), &multicast );
Schueler 0:c111a981bfb8 94
Schueler 0:c111a981bfb8 95 button.rise(&button_pressed);
Schueler 0:c111a981bfb8 96 while(true)
Schueler 0:c111a981bfb8 97 {
Schueler 0:c111a981bfb8 98 Net::poll();
Schueler 0:c111a981bfb8 99 if(dummy != 0)
Schueler 0:c111a981bfb8 100 {
Schueler 0:c111a981bfb8 101 char strout[20];
Schueler 0:c111a981bfb8 102 pos = !pos;
Schueler 0:c111a981bfb8 103 if ( pos == 0 ) strncpy( strout, strOFF, 17 ); else strncpy( strout, strON, 17 );
Schueler 0:c111a981bfb8 104 udp.sendto( strout, strlen(strout), &multicast );
Schueler 0:c111a981bfb8 105 pc.printf("%s\n", str);
Schueler 0:c111a981bfb8 106 dummy = 0;
Schueler 0:c111a981bfb8 107 }
Schueler 0:c111a981bfb8 108 }
Schueler 0:c111a981bfb8 109
Schueler 0:c111a981bfb8 110
Schueler 0:c111a981bfb8 111 }