Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6970

C/C++ • SNTP with LWIP fails

$
0
0
Hi,

I 'd like to tinker with SNTP. I made the following code work:

Code:

#include <stdio.h>#include <time.h>#include "pico/stdlib.h"#include "hardware/rtc.h"#include "lwip/apps/sntp.h"#include "pico/cyw43_arch.h"#include <sys/time.h>#include <unistd.h>#define WIFI_SSID "ssid"#define WIFI_PASSWORD "password"void SNTPSetRTC(u32_t t, u32_t us){    printf("updating RTC \n");    time_t secs = t - 2208988800;    struct tm *datetime = gmtime(&secs);    datetime_t dt;    dt.year = datetime->tm_year + 1900;    dt.month = datetime->tm_mon + 1;    dt.day = datetime->tm_mday;    dt.dotw = datetime->tm_wday;    dt.hour = datetime->tm_hour;    dt.min = datetime->tm_min;    dt.sec = datetime->tm_sec;    rtc_init();    rtc_set_datetime(&dt);}bool getDateNow(struct tm *t){    datetime_t rtc;    cyw43_arch_lwip_begin();    bool state = rtc_get_datetime(&rtc);    cyw43_arch_lwip_end();    if (state) {        t->tm_sec = rtc.sec;        t->tm_min = rtc.min;        t->tm_hour = rtc.hour;        t->tm_mday = rtc.dotw;        t->tm_mday = rtc.day;        t->tm_mon = rtc.month;        t->tm_year = rtc.year;        t->tm_yday = 0;        t->tm_isdst = -1;    }    return state;}int main(){    stdio_init_all();    // Initialise the Wi-Fi chip    if (cyw43_arch_init()) {        printf("Wi-Fi init failed\n");        return -1;    }    // Enable wifi station    cyw43_arch_enable_sta_mode();    printf("Connecting to Wi-Fi...\n");    if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {        printf("failed to connect.\n");        return 1;    } else {        printf("Connected.\n");        // Read the ip address in a human readable way        uint8_t *ip_address = (uint8_t*)&(cyw43_state.netif[0].ip_addr.addr);        printf("IP address %d.%d.%d.%d\n", ip_address[0], ip_address[1], ip_address[2], ip_address[3]);        sntp_setoperatingmode(SNTP_OPMODE_POLL);        ip_addr_t addr;        if (!ip4addr_aton("85.220.190.246", &addr)) {            printf("ip error\n");        }        //sntp_setserver(0, &addr);        time_t t1, t2, t3;                cyw43_arch_lwip_begin();        sntp_setservername(0, "pool.ntp.org");        sntp_init();        cyw43_arch_lwip_end();                while (true) {            struct tm t;            if (getDateNow(&t)) {                char date[100];                snprintf(date, sizeof(date), "Date: %d.%d.%d, Time: %d:%d:%d", t.tm_mday, t.tm_mon, t.tm_year, t.tm_hour, t.tm_min, t.tm_sec);                t1 = mktime(&t);                printf("%s %d %d\n", date, t1);            }             printf("Hello, world!\n");                        sleep_ms(5000);        }    }}
CMakeLists.txt is:

Code:

# Generated Cmake Pico project filecmake_minimum_required(VERSION 3.13)set(CMAKE_C_STANDARD 11)set(CMAKE_CXX_STANDARD 17)set(CMAKE_EXPORT_COMPILE_COMMANDS ON)# Initialise pico_sdk from installed location# (note this can come from environment, CMake cache etc)# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==if(WIN32)    set(USERHOME $ENV{USERPROFILE})else()    set(USERHOME $ENV{HOME})endif()set(sdkVersion 2.1.0)set(toolchainVersion 13_3_Rel1)set(picotoolVersion 2.1.0)set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)if (EXISTS ${picoVscode})    include(${picoVscode})endif()# ====================================================================================set(PICO_BOARD pico_w CACHE STRING "Board type")# Pull in Raspberry Pi Pico SDK (must be before project)include(pico_sdk_import.cmake)project(time C CXX ASM)# Initialise the Raspberry Pi Pico SDKpico_sdk_init()# Add executable. Default name is the project name, version 0.1add_executable(time time.c )pico_set_program_name(time "time")pico_set_program_version(time "0.1")# Modify the below lines to enable/disable output over UART/USBpico_enable_stdio_uart(time 1)pico_enable_stdio_usb(time 0)# Add the standard library to the buildtarget_link_libraries(time        pico_stdlib)# Add the standard include files to the buildtarget_include_directories(time PRIVATE        ${CMAKE_CURRENT_LIST_DIR})# Add any user requested librariestarget_link_libraries(time         pico_lwip_sntp         hardware_rtc        pico_cyw43_arch_lwip_threadsafe_background        )pico_add_extra_outputs(time)
Than I made an update, it leads to new standard CMakeLists.txt. The single difference is:

Code:

set(sdkVersion 2.1.1)set(toolchainVersion 14_2_Rel1)set(picotoolVersion 2.1.1)
Now I can't use

Code:

sntp_setservername(0, "pool.ntp.org");
anymore. I can replace it by

Code:

    ip_addr_t ntpAddr;    if (!ip4addr_aton("<IP adress of NTP server>", &ntpAddr)) {        printf("ip error\n");    }    cyw43_arch_lwip_begin();    sntp_setserver(0, &ntpAddr);    sntp_init();    cyw43_arch_lwip_end();
It compiles but won't get the time anymore. What's wrong?

Thanks a lot for any hint.
Joerg

Statistics: Posted by JoergBruenner123456 — Fri Mar 21, 2025 11:11 pm — Replies 0 — Views 34



Viewing all articles
Browse latest Browse all 6970

Trending Articles