Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf NTPClient_NetServices mbed
Revision 3:3c33a398189e, committed 2011-02-14
- Comitter:
- Schueler
- Date:
- Mon Feb 14 08:10:14 2011 +0000
- Parent:
- 2:ae37f80efd23
- Commit message:
- 1.0
Changed in this revision
diff -r ae37f80efd23 -r 3c33a398189e NTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NTPClient.lib Mon Feb 14 08:10:14 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/NTPClient/#7c3f1199256a
diff -r ae37f80efd23 -r 3c33a398189e UDPSocketExample.cpp
--- a/UDPSocketExample.cpp Wed Nov 24 23:46:38 2010 +0000
+++ b/UDPSocketExample.cpp Mon Feb 14 08:10:14 2011 +0000
@@ -1,25 +1,5 @@
-#include "mbed.h"
-#include "EthernetNetIf.h"
-#include "UDPSocket.h"
-
-Serial pc(USBTX, USBRX);
-EthernetNetIf eth;
-UDPSocket udp;
-DigitalOut led(LED1);
-InterruptIn button(p5);
-Ticker AliveTicker;
+#include "system.h"
-// xxx will be replaced by LSByte of the IP address
-//
-// Message to let the other know we are alive
-char strAlive[15];
-
-char mebed_list[16];
-
-char ButtonPressed = 0;
-char SendAlive = 0;
-char mbed_id = 0;
-char mbed_client = 0;
void Alive() // ticker routine
{
@@ -74,20 +54,29 @@
int main() {
char pos = 0;
pc.baud(115200);
- pc.printf("Ethernet...\n");
+ pc.printf("Ethernet...\n\r");
EthernetErr ethErr = eth.setup();
if(ethErr)
{
- pc.printf("Error %d in setup.\n", ethErr);
+ pc.printf("Error %d in setup.\n\r", ethErr);
return -1;
}
pc.printf(" OK\n\r");
-
IpAddr ethIp = eth.getIp();
mbed_id = ethIp[3];
sprintf( strAlive, "MBED %d ALIVE\0", mbed_id );
pc.printf( "%s\n\r", strAlive );
+
+ /* Set up NTP */
+// lcd.printf("Setting up NTP\n");
+ Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
+ ntp.setTime(server);
+ /* Temporary variables for control loop */
+ time_t rtc_time = time(NULL);
+ int minute = -1;
+
+
// 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
@@ -99,7 +88,7 @@
udp.sendto( strAlive, strlen(strAlive), &multicast );
button.rise(&button_pressed);
- while(true)
+ while( 1 ) // Never Ending Story by Michael Ende
{
Net::poll();
if ( SendAlive == 1 ) { udp.sendto( strAlive, strlen(strAlive), &multicast ); SendAlive = 0; } // Send Alive Signal
@@ -113,6 +102,22 @@
//pc.printf("%s\n", str);
ButtonPressed = 0;
}
+
+ /* Update current time */
+ rtc_time = time(NULL);
+ cTime = localtime(&rtc_time);
+ if (cTime->tm_min != minute)
+ {
+ pc.printf("*** %d:%d ***\n\r", cTime->tm_hour, cTime->tm_min);
+ minute = cTime->tm_min;
+ /* Update time from NTP server if it's midnight UTC */
+ if ((cTime->tm_min == 0) && (cTime->tm_hour == 0))
+ {
+ Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
+ ntp.setTime(server);
+ }
+ }
+
}
diff -r ae37f80efd23 -r 3c33a398189e system.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/system.h Mon Feb 14 08:10:14 2011 +0000 @@ -0,0 +1,24 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "UDPSocket.h" +#include "NTPClient.h" + +Serial pc(USBTX, USBRX); +EthernetNetIf eth; +NTPClient ntp; +UDPSocket udp; +DigitalOut led(LED1); +InterruptIn button(p5); +Ticker AliveTicker; + +struct tm *cTime; // Stucture that holds the current time + +// Message to let the other know we are alive +char strAlive[15]; + +char mebed_list[16]; + +char ButtonPressed = 0; +char SendAlive = 0; +char mbed_id = 0; +char mbed_client = 0;