Dependencies:   mbed EthernetNetIf

Committer:
nenad
Date:
Thu Apr 30 13:27:01 2015 +0000
Revision:
1:42d45015ad30
Parent:
0:527185ce17bc
azurirani portovi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nenad 0:527185ce17bc 1 #include "mbed.h"
nenad 0:527185ce17bc 2 #include "EthernetNetIf.h"
nenad 0:527185ce17bc 3 #include "UDPSocket.h"
nenad 0:527185ce17bc 4
nenad 0:527185ce17bc 5 //EthernetNetIf eth ;
nenad 0:527185ce17bc 6 EthernetNetIf eth(
nenad 0:527185ce17bc 7 IpAddr(192,168,1,111), //IP Address
nenad 0:527185ce17bc 8 IpAddr(255,255,255,0), //Network Mask
nenad 0:527185ce17bc 9 IpAddr(192,168,0,1), //Gateway
nenad 0:527185ce17bc 10 IpAddr(192,168,0,1) //DNS
nenad 0:527185ce17bc 11 );
nenad 0:527185ce17bc 12
nenad 0:527185ce17bc 13 UDPSocket udp;
nenad 0:527185ce17bc 14 int global_counter=0;
nenad 0:527185ce17bc 15 void onUDPSocketEvent(UDPSocketEvent e)
nenad 0:527185ce17bc 16 {
nenad 0:527185ce17bc 17 switch(e)
nenad 0:527185ce17bc 18 {
nenad 0:527185ce17bc 19 case UDPSOCKET_READABLE: //The only event for now
nenad 0:527185ce17bc 20 char buf[64] = {0};
nenad 0:527185ce17bc 21 Host host;
nenad 0:527185ce17bc 22 while( int len = udp.recvfrom( buf, 63, &host ) )
nenad 0:527185ce17bc 23 {
nenad 0:527185ce17bc 24 if( len <= 0 )
nenad 0:527185ce17bc 25 break;
nenad 0:527185ce17bc 26 printf("Received from %d.%d.%d.%d: %s\r\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf);
nenad 0:527185ce17bc 27 printf("\n");
nenad 0:527185ce17bc 28 }
nenad 0:527185ce17bc 29 break;
nenad 0:527185ce17bc 30 }
nenad 0:527185ce17bc 31 }
nenad 0:527185ce17bc 32
nenad 0:527185ce17bc 33 int main() {
nenad 0:527185ce17bc 34 printf("Setting up...\n");
nenad 0:527185ce17bc 35 EthernetErr ethErr = eth.setup(30000);
nenad 0:527185ce17bc 36 if(ethErr)
nenad 0:527185ce17bc 37 {
nenad 0:527185ce17bc 38 printf("Error %d in setup.\n", ethErr);
nenad 0:527185ce17bc 39 return -1;
nenad 0:527185ce17bc 40 }
nenad 0:527185ce17bc 41 printf("Setup OK\n");
nenad 0:527185ce17bc 42
nenad 0:527185ce17bc 43 Host multicast(IpAddr(224,0,0,0), 50000); //Join multicast group on port 50000
nenad 0:527185ce17bc 44
nenad 0:527185ce17bc 45 udp.setOnEvent(&onUDPSocketEvent);
nenad 0:527185ce17bc 46
nenad 0:527185ce17bc 47 udp.bind(multicast);
nenad 0:527185ce17bc 48
nenad 0:527185ce17bc 49 Timer tmr;
nenad 0:527185ce17bc 50 tmr.start();
nenad 0:527185ce17bc 51 while(true)
nenad 0:527185ce17bc 52 {
nenad 0:527185ce17bc 53 Net::poll();
nenad 0:527185ce17bc 54 if(tmr.read() > 2)
nenad 0:527185ce17bc 55 {
nenad 0:527185ce17bc 56 char Buffer[100]={0};
nenad 0:527185ce17bc 57 global_counter++;
nenad 0:527185ce17bc 58 sprintf(Buffer,"Counter=%d", global_counter);
nenad 0:527185ce17bc 59 printf("Sent to group: Counter=%d\r\n", global_counter);
nenad 0:527185ce17bc 60 udp.sendto( Buffer, strlen(Buffer)+1, &multicast );
nenad 0:527185ce17bc 61 tmr.reset();
nenad 0:527185ce17bc 62 }
nenad 0:527185ce17bc 63 }
nenad 0:527185ce17bc 64
nenad 0:527185ce17bc 65
nenad 0:527185ce17bc 66 }