#include "hardware/clocks.h" #include "hardware/rtc.h" #include "hardware/spi.h" #include "hardware/timer.h" #include "lwip/apps/sntp.h" #include "lwip/err.h" #include "lwip/ip_addr.h" #include "pico/cyw43_arch.h" #include "pico/stdlib.h" #include "pico/util/datetime.h" #include "wifi_conf.h" #include #include #include #include "lwip/dns.h" #include "lwip/init.h" #include "lwip/tcp.h" #include "net_utils.h" // SPI Defines // We are going to use SPI 0, and allocate it to the following GPIO pins // Pins can be changed, see the GPIO function select table in the datasheet for // information on GPIO assignments #define SPI_PORT spi0 #define PIN_MISO 16 #define PIN_CS 17 #define PIN_SCK 18 #define PIN_MOSI 19 extern "C" void sync_system_time(unsigned int sec, unsigned int usec) { printf("SNTP callback received: %lu\n", (unsigned long)sec); time_t unix_time = (time_t)sec; u16_t timezone_offset = 7200; // Apply timezone offset for local time unix_time += timezone_offset; struct tm *time_info = gmtime(&unix_time); if (time_info == NULL) { printf("SNTP: Invalid timestamp %lu\n", (unsigned long)sec); return; } datetime_t dt = {.year = static_cast(time_info->tm_year + 1900), .month = static_cast(time_info->tm_mon + 1), .day = static_cast(time_info->tm_mday), .dotw = static_cast(time_info->tm_wday), .hour = static_cast(time_info->tm_hour), .min = static_cast(time_info->tm_min), .sec = static_cast(time_info->tm_sec)}; // Sanity checks if (dt.year < 2020 || dt.year > 2100 || dt.month < 1 || dt.month > 12 || dt.day < 1 || dt.day > 31 || dt.hour > 23 || dt.min > 59 || dt.sec > 59) { printf("SNTP: Invalid date/time components\n"); return; } // Set RTC if (rtc_set_datetime(&dt)) { printf("SNTP: Time set to %04d-%02d-%02d %02d:%02d:%02d %s\n", dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, (timezone_offset == 0) ? "UTC" : "Local"); } else { printf("SNTP: Failed to set RTC\n"); } } int main() { stdio_init_all(); if (cyw43_arch_init()) { return 1; } rtc_init(); // sign of life cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true); // SPI initialisation. This example will use SPI at 1MHz. spi_init(SPI_PORT, 1000 * 1000); gpio_set_function(PIN_MISO, GPIO_FUNC_SPI); gpio_set_function(PIN_CS, GPIO_FUNC_SIO); gpio_set_function(PIN_SCK, GPIO_FUNC_SPI); gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI); // Chip select is active-low, so we'll initialise it to a driven-high state gpio_set_dir(PIN_CS, GPIO_OUT); gpio_put(PIN_CS, 1); // Timer example code - This example fires off the callback after 2000ms // add_alarm_in_ms(2000, alarm_callback, NULL, false); printf("connecting to wifi\n"); // connect to wifi cyw43_arch_enable_sta_mode(); if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass, CYW43_AUTH_WPA2_AES_PSK, 30000)) { return 1; } printf("IP: %s\n", ip4addr_ntoa(netif_ip_addr4(netif_default))); printf("Mask: %s\n", ip4addr_ntoa(netif_ip_netmask4(netif_default))); printf("Gateway: %s\n", ip4addr_ntoa(netif_ip_gw4(netif_default))); { ip_addr_t ip_addr; IP_ADDR4(&ip_addr, 1, 1, 1, 1); dns_setserver(0, &ip_addr); IP_ADDR4(&ip_addr, 8, 8, 8, 8); dns_setserver(1, &ip_addr); printf("Initializing SNTP\n"); sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_init(); dns_resolve_sync("ntp.cesnet.cz", &ip_addr, 10000); sntp_setserver(0, &ip_addr); dns_resolve_sync("ntp0.fau.de", &ip_addr, 10000); sntp_setserver(1, &ip_addr); } printf("SNTP initialized, waiting for time sync...\n"); while (true) { // Keep the program running to allow SNTP to work sleep_ms(1000); // Optional: Print current time periodically to verify sync datetime_t t; if (rtc_get_datetime(&t)) { printf("Current time: %04d-%02d-%02d %02d:%02d:%02d\n", t.year, t.month, t.day, t.hour, t.min, t.sec); } else { printf("failed to get rtc\n"); } } cyw43_arch_lwip_end(); return 0; }