5 days, 18 hours ago.

How to use Mbed Ethernet functions on this Wi-Fi NTPClient example

Are there any easy to understand examples to use the functions of the Mbed Ethernet API?

I have the following lightweight NTP code that I use for an (ESP8266) Wi-Fi module where I can pick up the NTP time and set my RTC within around 80mS and would like to use the same over cabled Ethernet.

I'm using the startup code from Zoltan's server example here:

and have added my own web pages. This works fine but I want add this NTPClient code to initialize the RTC on startup. I'm not sure how to use the "open" , "send" and "receive" parts as indicated. Also can I start up in 'SERVER' mode but still use 'CLIENT' mode functions like I do with the ESP8266 or would I have to start up in 'CLIENT' mode get my NTP, disconnect then restart as 'SERVER'?

NTPClient function

uint32_t ESP82::getNTP(char * NTPpool,uint32_t tzoffset, bool dst, uint32_t setRTC)
    // example NTPpool = ""
    // serial.printf("Seconds since 1970 = %d\r\n", esp.getNTP("",3600,1,1));
    uint32_t NTP_PACKET_SIZE = 48;
    char packetBuffer[NTP_PACKET_SIZE];
    uint32_t NTP_OFFSET = 2208988800; // for UTC time
    memset(packetBuffer, 0x00, NTP_PACKET_SIZE);
    // Initialize values needed to form NTP request
    packetBuffer[0]  = 0b11100011;
    packetBuffer[1]  = 0;
    packetBuffer[2]  = 6;
    packetBuffer[3]  = 0xEC;
    packetBuffer[12] = 49;
    packetBuffer[13] = 0x4E;
    packetBuffer[14] = 49;
    packetBuffer[15] = 52;
    uint32_t recv_size = 0;
    uint32_t tries1 = 10;
    while ((recv_size != NTP_PACKET_SIZE) && (tries1 > 0)){
        open("UDP", 1, NTPpool, 123);                    // This open part
        send(1, packetBuffer, NTP_PACKET_SIZE);               // This send part
        uint32_t tries2 = 10;
        while ((recv_size != NTP_PACKET_SIZE) && (tries2 > 0)){
            recv_size = recv(1, packetBuffer, NTP_PACKET_SIZE);       //  This receive part
        uint32_t secsSince1970=((packetBuffer[40]<<24)|(packetBuffer[41]<<16)|(packetBuffer[42]<<8)|packetBuffer[43])-NTP_OFFSET;
        uint32_t CETtime=(secsSince1970+(tzoffset*3600));
        // check for DST time change, only valid for europe!!!
        uint32_t DST=0;  
            uint32_t dow,day,month;             
            char buffer[10];        
            strftime(buffer, 2,"%H", localtime(&CETtime));                                
            hour = atoi(buffer);
            strftime(buffer, 2,"%w", localtime(&CETtime));                                
            dow = atoi(buffer);
            strftime(buffer, 2,"%e", localtime(&CETtime));                
            day = atoi(buffer);
            strftime(buffer, 2,"%m", localtime(&CETtime));
            month = atoi(buffer);       
            uint32_t previousSunday = day - dow;
            if (month > 2 && month < 9){DST=3600;}            
            if (month == 2 && previousSunday >= 25 && hour >= 2){DST=3600;} // DST starts 2nd Sunday of March;  2am
            if (month == 9 && previousSunday < 25 && hour >= 2){DST=0;}     // DST ends 1st Sunday of November; 2am
        if (setRTC){set_time(CETtime+DST+1);} // todo: add timer code to addjust fractional second to offset MCU processing delay        
        return (CETtime+DST);
    else{return 0;}

Many thanks

Comment on this question
Be the first to answer this question.

To post an answer, please log in.