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

Revision:
1:2e2d0b0b57e0
Parent:
0:c111a981bfb8
Child:
2:ae37f80efd23
--- a/UDPSocketExample.cpp	Wed Nov 24 15:49:45 2010 +0000
+++ b/UDPSocketExample.cpp	Wed Nov 24 23:32:52 2010 +0000
@@ -2,23 +2,34 @@
 #include "EthernetNetIf.h"
 #include "UDPSocket.h"
 
-LocalFileSystem local("local");
 Serial pc(USBTX, USBRX);
 EthernetNetIf eth;
 UDPSocket udp;
 DigitalOut led(LED1);
 InterruptIn button(p5);
+Ticker AliveTicker;
 
+// xxx will be replaced by LSByte of the IP address
+//
 // Message to let the other know we are alive
-      char* str    = "MBED 001 ALIVE\0";
+char strAlive[15];
 // Address other MBED to turn on the LED
-const char* strON  = "MBED 002 LED ON  \0";
-const char* strOFF = "MBED 002 LED OFF \0";
+const char* strON  = "MBED yyy LED ON  \0";
+const char* strOFF = "MBED yyy LED OFF \0";
+char mebed_list[16];
+
+char ButtonPressed = 0;
+char SendAlive = 0;
+char mbed_id = 0;
 
-char dummy = 0;
-char mbed_id = 0;
-void button_pressed() {
-    dummy = 1;
+void Alive()         // ticker routine
+{
+    SendAlive = 1;
+}
+
+void button_pressed()   // irq routine for button
+{
+    ButtonPressed = 1;
 }
 
 
@@ -38,12 +49,12 @@
         // but this will do the trick for the moment
         if ( strncmp ( buf, "MBED", 4 ) == 0 )
         {
-            pc.printf ("message from %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf );
+            //pc.printf ("message from %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf );
 
             int mbed_client = 0;
-            for ( int i = 5; i < 9; i++ )
+            for ( int i = 5; i < 8; i++ )
             {
-                if ( ( buf[i] >= '0' ) & ( buf[i] <= '9' ) ) { mbed_client += ( buf[i] - '0' ); }
+                if ( ( buf[i] >= '0' ) & ( buf[i] <= '9' ) ) { mbed_client *=10; mbed_client += ( buf[i] - '0' ); }
             }
             if ( strstr (buf, "ALIVE") != NULL )
             {
@@ -51,11 +62,11 @@
             }
             if ( strstr (buf, "LED") != NULL )
             {
+                pc.printf ("MBED %d send a message\n", mbed_client );
                 if ( strstr (buf, "ON")  != NULL ) { led = 1; }
                 if ( strstr (buf, "OFF") != NULL ) { led = 0; }
             }
         }
-
     }
     break;
   }
@@ -68,42 +79,38 @@
   EthernetErr ethErr = eth.setup();
   if(ethErr)
   {
-    printf("Error %d in setup.\n", ethErr);
+    pc.printf("Error %d in setup.\n", ethErr);
     return -1;
   }
-  pc.printf("  OK\n");
+  pc.printf("  OK\n\r");
 
-/*
-  FILE *fp = fopen ("/local/mbed_udp.cfg", "r" );
-    if(!fp) {
-        printf("File /local/mbed_udp.cfg could not be opened!\n");
-        exit(1);
-    }
-  str[5] = fgetc ( fp );
-  fclose(fp);
-*/
-
-  
+  IpAddr ethIp = eth.getIp();
+  sprintf( strAlive, "MBED %d ALIVE\0", ethIp[3] );
+  pc.printf( "%s\n\r", strAlive );
+  // set Alive Broadcast to 10 seconds
+  AliveTicker.attach(&Alive, 10);  
   Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000
  
   udp.setOnEvent(&onUDPSocketEvent);
   
   udp.bind(multicast);
 
-  udp.sendto( str, strlen(str), &multicast );
+  udp.sendto( strAlive, strlen(strAlive), &multicast );
   
   button.rise(&button_pressed);
   while(true)
   {
     Net::poll();
-    if(dummy != 0)
+    if ( SendAlive == 1 ) { udp.sendto( strAlive, strlen(strAlive), &multicast ); SendAlive = 0; }    // Send Alive Signal
+    
+    if( ButtonPressed != 0 )
     {
       char strout[20];
       pos = !pos;
       if ( pos == 0 ) strncpy( strout, strOFF, 17 ); else strncpy( strout, strON, 17 );
       udp.sendto( strout, strlen(strout), &multicast );
-      pc.printf("%s\n", str);
-      dummy = 0;
+      //pc.printf("%s\n", str);
+      ButtonPressed = 0;
     }
   }