commit 5c5d4eb48c40c8387d6dbcba249a9f31e80e733a Author: PoliEcho Date: Mon Sep 8 19:35:36 2025 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..acca597 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +wifi_conf.h +build +.cache +.idea +compile_commands.json \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c971d9f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "json"] + path = json + url = https://github.com/nlohmann/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..681f621 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,67 @@ +# Generated Cmake Pico project file + +cmake_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) +set(PICO_SDK_PATH "/usr/share/pico-sdk") + +set(PICO_BOARD pico_w CACHE STRING "Board type") + +# Pull in Raspberry Pi Pico SDK (must be before project) +include(pico_sdk_import.cmake) + +if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0") + message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}") +endif() + +project(smart_alarm C CXX ASM) + +set(PICO_CXX_ENABLE_EXCEPTIONS 1) + +# Initialise the Raspberry Pi Pico SDK +pico_sdk_init() + +# Add executable. Default name is the project name, version 0.1 + +add_executable(smart_alarm smart_alarm.cpp ) + +pico_set_program_name(smart_alarm "smart_alarm") +pico_set_program_version(smart_alarm "0.1") + +pico_enable_stdio_uart(smart_alarm 1) +pico_enable_stdio_usb(smart_alarm 1) + +# Add the standard library to the build +target_link_libraries(smart_alarm + pico_stdlib) + +# Add the standard include files to the build +target_include_directories(smart_alarm PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required + ${CMAKE_CURRENT_LIST_DIR}/json/include +) + +# Add any user requested libraries +target_link_libraries(smart_alarm + hardware_spi + hardware_timer + hardware_clocks + pico_cyw43_arch_lwip_threadsafe_background + pico_lwip_sntp + hardware_rtc + pico_lwip_http + ) + +target_compile_definitions(smart_alarm PRIVATE + SNTP_SET_SYSTEM_TIME_NTP=sync_system_time + SNTP_UPDATE_DELAY=1800000 # 30 minutes + ) + +pico_add_extra_outputs(smart_alarm) + diff --git a/ip.json b/ip.json new file mode 100644 index 0000000..06287cd --- /dev/null +++ b/ip.json @@ -0,0 +1 @@ +{"utc_offset":"+02:00","timezone":"Europe/Prague","day_of_week":0,"day_of_year":250,"datetime":"2025-09-07T13:17:27.614176+02:00","utc_datetime":"2025-09-07T11:17:27.614176+00:00","unixtime":1757243847,"raw_offset":3600,"week_number":36,"dst":true,"abbreviation":"CEST","dst_offset":3600,"dst_from":"2025-03-30T01:00:00+00:00","dst_until":"2025-10-26T01:00:00+00:00","client_ip":"185.100.196.2"} \ No newline at end of file diff --git a/json b/json new file mode 160000 index 0000000..867127c --- /dev/null +++ b/json @@ -0,0 +1 @@ +Subproject commit 867127c2f49ba238c6f6345c4b3cff72fdd96aec diff --git a/lwipopts.h b/lwipopts.h new file mode 100644 index 0000000..d1243ab --- /dev/null +++ b/lwipopts.h @@ -0,0 +1,260 @@ +#ifndef __LWIPOPTS_H__ +#define __LWIPOPTS_H__ + +/** + * NO_SYS==1: Bare metal lwIP + */ +#define NO_SYS 1 +/** + * LWIP_NETCONN==0: Disable Netconn API (require to use api_lib.c) + */ +#define LWIP_NETCONN 0 +/** + * LWIP_SOCKET==0: Disable Socket API (require to use sockets.c) + */ +#define LWIP_SOCKET 0 + +/** + * SYS_LIGHTWEIGHT_PROT==1: enable inter-task protection (and task-vs-interrupt + * protection) for certain critical regions during buffer allocation, + * deallocation and memory allocation and deallocation. ATTENTION: This is + * required when using lwIP from more than one context! If you disable this, you + * must be sure what you are doing! + */ +/** + * SYS_LIGHTWEIGHT_PROT==0: + */ +#define SYS_LIGHTWEIGHT_PROT 0 + +/* ---------- Memory options ---------- */ +/** + * MEM_ALIGNMENT: should be set to the alignment of the CPU + * 4 byte alignment -> #define MEM_ALIGNMENT 4 + * 2 byte alignment -> #define MEM_ALIGNMENT 2 + */ +#ifndef MEM_ALIGNMENT +#define MEM_ALIGNMENT 4 +#endif + +/** + * MEM_SIZE: the size of the heap memory. If the application will send + * a lot of data that needs to be copied, this should be set high. + */ +#ifndef MEM_SIZE +#define MEM_SIZE (22 * 1024) +#endif + +/* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application + sends a lot of data out of ROM (or other static memory), this + should be set high. */ +#ifndef MEMP_NUM_PBUF +#define MEMP_NUM_PBUF 15 +#endif +/* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One + per active UDP "connection". */ +#ifndef MEMP_NUM_UDP_PCB +#define MEMP_NUM_UDP_PCB 6 +#endif +/* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP + connections. */ +#ifndef MEMP_NUM_TCP_PCB +#define MEMP_NUM_TCP_PCB 10 +#endif +/* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP + connections. */ +#ifndef MEMP_NUM_TCP_PCB_LISTEN +#define MEMP_NUM_TCP_PCB_LISTEN 6 +#endif +/* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP + segments. */ +#ifndef MEMP_NUM_TCP_SEG +#define MEMP_NUM_TCP_SEG 22 +#endif +/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active + timeouts. */ +#ifndef MEMP_NUM_SYS_TIMEOUT +#define MEMP_NUM_SYS_TIMEOUT 10 +#endif + +/* ---------- Pbuf options ---------- */ +/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ +#ifndef PBUF_POOL_SIZE +#define PBUF_POOL_SIZE 9 +#endif + +/* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */ +/* Default value is defined in lwip\src\include\lwip\opt.h as + * LWIP_MEM_ALIGN_SIZE(TCP_MSS+40+PBUF_LINK_ENCAPSULATION_HLEN+PBUF_LINK_HLEN)*/ + +/* ---------- TCP options ---------- */ +#ifndef LWIP_TCP +#define LWIP_TCP 1 +#endif + +#ifndef TCP_TTL +#define TCP_TTL 255 +#endif + +/* Controls if TCP should queue segments that arrive out of + order. Define to 0 if your device is low on memory. */ +#ifndef TCP_QUEUE_OOSEQ +#define TCP_QUEUE_OOSEQ 0 +#endif + +/* TCP Maximum segment size. */ +#ifndef TCP_MSS +#define TCP_MSS \ + (1500 - 40) /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) \ + */ +#endif + +/* TCP sender buffer space (bytes). */ +#ifndef TCP_SND_BUF +#define TCP_SND_BUF (6 * TCP_MSS) // 2 +#endif + +/* TCP sender buffer space (pbufs). This must be at least = 2 * + TCP_SND_BUF/TCP_MSS for things to work. */ +#ifndef TCP_SND_QUEUELEN +#define TCP_SND_QUEUELEN (3 * TCP_SND_BUF) / TCP_MSS // 6 +#endif + +/* TCP receive window. */ +#ifndef TCP_WND +#define TCP_WND (2 * TCP_MSS) +#endif + +/* Enable backlog*/ +#ifndef TCP_LISTEN_BACKLOG +#define TCP_LISTEN_BACKLOG 1 +#endif + +/* ---------- Network Interfaces options ---------- */ +/* Support netif api (in netifapi.c). */ +#ifndef LWIP_NETIF_API +#define LWIP_NETIF_API 0 +#endif + +/* ---------- ICMP options ---------- */ +#ifndef LWIP_ICMP +#define LWIP_ICMP 1 +#endif + +/* ---------- DHCP options ---------- */ +/* Define LWIP_DHCP to 1 if you want DHCP configuration of + interfaces. DHCP is not implemented in lwIP 0.5.1, however, so + turning this on does currently not work. */ +#ifndef LWIP_DHCP +#define LWIP_DHCP 1 +#endif + +/* ---------- UDP options ---------- */ +#ifndef LWIP_UDP +#define LWIP_UDP 1 +#endif +#ifndef UDP_TTL +#define UDP_TTL 255 +#endif + +/* ---------- Statistics options ---------- */ +#ifndef LWIP_STATS +#define LWIP_STATS 0 +#endif +#ifndef LWIP_PROVIDE_ERRNO +#define LWIP_PROVIDE_ERRNO 1 +#endif + +/* + -------------------------------------- + ---------- Checksum options ---------- + -------------------------------------- +*/ + +/* +Some MCU allow computing and verifying the IP, UDP, TCP and ICMP checksums by +hardware: + - To use this feature let the following define uncommented. + - To disable it and process by CPU comment the the checksum. +*/ +// #define CHECKSUM_BY_HARDWARE + +#ifdef CHECKSUM_BY_HARDWARE +/* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/ +#define CHECKSUM_GEN_IP 0 +/* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing UDP + * packets.*/ +#define CHECKSUM_GEN_UDP 0 +/* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing TCP + * packets.*/ +#define CHECKSUM_GEN_TCP 0 +/* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP packets.*/ +#define CHECKSUM_CHECK_IP 0 +/* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming UDP + * packets.*/ +#define CHECKSUM_CHECK_UDP 0 +/* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming TCP + * packets.*/ +#define CHECKSUM_CHECK_TCP 0 +#else +/* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing IP packets.*/ +#define CHECKSUM_GEN_IP 1 +/* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing UDP + * packets.*/ +#define CHECKSUM_GEN_UDP 1 +/* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing TCP + * packets.*/ +#define CHECKSUM_GEN_TCP 1 +/* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP packets.*/ +#define CHECKSUM_CHECK_IP 1 +/* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming UDP + * packets.*/ +#define CHECKSUM_CHECK_UDP 1 +/* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming TCP + * packets.*/ +#define CHECKSUM_CHECK_TCP 1 +#endif + +/* + ------------------------------------ + ---------- Debugging options ---------- + ------------------------------------ +*/ + +// #define LWIP_DEBUG + +// TODO: map these to + +#ifdef LWIP_DEBUG +#define U8_F "c" +#define S8_F "c" +#define X8_F "02x" +#define U16_F "u" +#define S16_F "d" +#define X16_F "x" +#define U32_F "u" +#define S32_F "d" +#define X32_F "x" +#define SZT_F "u" +#endif + +#if (LWIP_DNS || LWIP_IGMP || LWIP_IPV6) && !defined(LWIP_RAND) +/* When using IGMP or IPv6, LWIP_RAND() needs to be defined to a random-function + * returning an u32_t random value*/ +#include "lwip/arch.h" +u32_t lwip_rand(void); +#define LWIP_RAND() lwip_rand() +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +void sync_system_time(unsigned int sec, unsigned int usec); + +#ifdef __cplusplus +} +#endif + +#endif /* __LWIPOPTS_H__ */ + +/*****END OF FILE****/ diff --git a/pico_sdk_import.cmake b/pico_sdk_import.cmake new file mode 100644 index 0000000..d493cc2 --- /dev/null +++ b/pico_sdk_import.cmake @@ -0,0 +1,121 @@ +# This is a copy of /external/pico_sdk_import.cmake + +# This can be dropped into an external project to help locate this SDK +# It should be include()ed prior to project() + +# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the +# following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following +# disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) + set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) + message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) + set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) + message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) + set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) + message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG)) + set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG}) + message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')") +endif () + +if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG) + set(PICO_SDK_FETCH_FROM_GIT_TAG "master") + message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG") +endif() + +set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") +set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") +set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") +set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK") + +if (NOT PICO_SDK_PATH) + if (PICO_SDK_FETCH_FROM_GIT) + include(FetchContent) + set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) + if (PICO_SDK_FETCH_FROM_GIT_PATH) + get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") + endif () + FetchContent_Declare( + pico_sdk + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + ) + + if (NOT pico_sdk) + message("Downloading Raspberry Pi Pico SDK") + # GIT_SUBMODULES_RECURSE was added in 3.17 + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + GIT_SUBMODULES_RECURSE FALSE + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + else () + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + endif () + + set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) + endif () + set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) + else () + message(FATAL_ERROR + "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." + ) + endif () +endif () + +get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") +if (NOT EXISTS ${PICO_SDK_PATH}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") +endif () + +set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) +if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") +endif () + +set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) + +include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/smart_alarm.cpp b/smart_alarm.cpp new file mode 100644 index 0000000..d8af8c3 --- /dev/null +++ b/smart_alarm.cpp @@ -0,0 +1,183 @@ +#include "hardware/clocks.h" +#include "hardware/rtc.h" +#include "hardware/spi.h" +#include "hardware/timer.h" +#include "lwip/apps/http_client.h" +#include "lwip/apps/sntp.h" +#include "lwip/err.h" +#include "lwip/ip_addr.h" +#include "nlohmann/json.hpp" +#include "pico/async_context.h" +#include "pico/cyw43_arch.h" +#include "pico/stdlib.h" +#include "pico/util/datetime.h" +#include "wifi_conf.h" +#include +#include +#include +#include + +// 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 + +static void result_fn(void *arg, httpc_result_t httpc_result, + u32_t rx_content_len, u32_t srv_res, err_t err) { + if (httpc_result == HTTPC_RESULT_OK && err == ERR_OK) { + printf("Request completed successfully. Received %lu bytes\n", rx_content_len); + } else { + printf("Request failed - HTTP result: %d, lwIP error: %d, HTTP status: %lu\n", + httpc_result, err, srv_res); + } +} + +static err_t get_timezone_callback(void *arg, struct altcp_pcb *pcb, + struct pbuf *p, err_t err) { + if (p == NULL) { + // Connection closed + return ERR_OK; + } + + // Print the JSON response body + char *json_data = (char *)p->payload; + printf("%.*s\n", p->len, json_data); + + // Required: Free memory and acknowledge receipt + uint16_t len = p->tot_len; + pbuf_free(p); + altcp_recved(pcb, len); + + return ERR_OK; +} + +err_t get_timezone_offset() { + httpc_connection_t settings = {.use_proxy = 0, + .result_fn = result_fn, + .headers_done_fn = NULL}; + memset(&settings, 0, sizeof(settings)); + settings.use_proxy = 0; + settings.result_fn = result_fn; + settings.headers_done_fn = NULL; + + httpc_state_t *connection = NULL; + + err_t err = httpc_get_file_dns("worldtimeapi.org", 80, "/api/ip", &settings, + get_timezone_callback, NULL, &connection); + + if (err != ERR_OK) { + printf("Failed to start request: %d\n", err); + } + return err; +} + +#define TIMEZONE_OFFSET_SEC 7200 +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; + + // Apply timezone offset for local time + unix_time += TIMEZONE_OFFSET_SEC; + + 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_SEC == 0) ? "UTC" : "Local"); + } else { + printf("SNTP: Failed to set RTC\n"); + } +} + +int main() { + stdio_init_all(); + if (cyw43_arch_init()) { + return 1; + } + rtc_init(); + + // 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))); + + printf("Initializing SNTP\n"); + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_init(); + ip_addr_t ntp_server; + if (ipaddr_aton("162.159.200.123", &ntp_server)) { // Cloudflare NTP + sntp_setserver(0, &ntp_server); + } + if (ipaddr_aton("129.6.15.28", &ntp_server)) { // NIST NTP + sntp_setserver(1, &ntp_server); + } + + printf("SNTP initialized, waiting for time sync...\n"); + +get_timezone_offset(); + + + + 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"); + } + } + return 0; +}