9 years, 1 month ago.

NTP current time

hello i want read the computer current time and then shown it on the LPC1768 LCD through the cable, and i just import a code but the mbed cant get the IP address, and finally the time is not the real time, can somebody help me

app_board-NTPclock

#include "mbed.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#include "C12832_lcd.h"

C12832_LCD lcd; //Graphics LCD
EthernetInterface eth;
NTPClient ntp;

int main()
{
    eth.init(); //Use DHCP
    wait(2);
    lcd.cls();
    lcd.printf("Getting IP Address\r\n");
    if(eth.connect(60000)!=0) {
        lcd.printf("DHCP error - No IP");
        wait(10);
    } else {
        lcd.printf("IP is %s\n", eth.getIPAddress());
        wait(2);
    }
    lcd.cls();
    lcd.printf("Trying to update time...\r\n");
    if (ntp.setTime("0.pool.ntp.org") == 0) {
        lcd.printf("Set time successfully\r\n");
        while(1) {
            lcd.cls();
            lcd.locate(0,0);
            time_t ctTime;
            ctTime = time(NULL);
            lcd.printf("%s\r\n", ctime(&ctTime));
            lcd.printf("Current Time (UTC)");
            wait(1);
        }
    } else {
        lcd.printf("NTP Error\r\n");
    }

    eth.disconnect();

    while(1) {
    }
}

1 Answer

9 years, 1 month ago.

is your mbed connected to internet ? in that case try with an ststic ip address if your computer or ethernet switch doesn't provide an dhcp address to your mbed.

Accepted Answer

/media/uploads/maoqi8820/----_2015-04-12_--5.51.50.png Hello, my computer shows that the mbed has been connected, but the IP address is unavailable. i can show you the statement.

posted by qi mao 13 Apr 2015

If you want Mac Os X to give an dynamic address Ip to the mbed you have to enable Sharing Internet connection through the preferences menu.

You can also do in another way : set an static ip adress to the mbed with the init() function as :

eth.init("192.168.1.99", "255.255.255.0", "192.168.1.xx");

where xx is the ip of your mac computer (the wifi one). This is true if your network is in the 192.168.1.zzz network

the args of the init command are :

/ Initialize the interface with a static IP address.

  • Initialize the interface and configure it with the following static configuration (no connection at this point).
  • \param ip the IP address to use
  • \param mask the IP address mask
  • \param gateway the gateway to use
  • \return 0 on success, a negative number on failure
  • / static int init(const char* ip, const char* mask, const char* gateway);

you can find it : http://developer.mbed.org/users/mbed_official/code/EthernetInterface/file/2fc406e2553f/EthernetInterface.h

posted by Raph Francois 13 Apr 2015