This commit is contained in:
PoliEcho 2025-09-14 08:08:15 +02:00
parent 5010544ba9
commit 5e0a972b18
5 changed files with 46 additions and 11 deletions

View File

@ -11,6 +11,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(PICO_SDK_PATH "/usr/share/pico-sdk")
set(PICO_BOARD pico_w CACHE STRING "Board type")
set(PICO_STACK_SIZE 0x2000)
# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)
@ -29,7 +30,7 @@ pico_sdk_init()
# Add executable. Default name is the project name, version 0.1
add_executable(smart_alarm smart_alarm.cpp
net_utils.cpp)
net_utils.cpp timezones.cpp)
pico_set_program_name(smart_alarm "smart_alarm")
pico_set_program_version(smart_alarm "0.2")
@ -62,6 +63,9 @@ target_compile_definitions(smart_alarm PRIVATE
SNTP_SET_SYSTEM_TIME_NTP=sync_system_time
SNTP_UPDATE_DELAY=1800000 # 30 minutes
)
target_compile_options(smart_alarm PRIVATE
-Wno-write-strings
)
pico_add_extra_outputs(smart_alarm)

View File

@ -263,8 +263,6 @@ void sync_system_time(unsigned int sec, unsigned int usec);
}
#endif
#endif /* __LWIPOPTS_H__ */
/*****END OF FILE****/

View File

@ -1,6 +1,5 @@
#include "hardware/clocks.h"
#include "lwip/ip_addr.h"
#include "nlohmann/json.hpp"
#include "pico/cyw43_arch.h"
#include "pico/util/datetime.h"
#include <lwip/arch.h>

View File

@ -17,6 +17,7 @@
#include "lwip/init.h"
#include "lwip/tcp.h"
#include "net_utils.h"
#include "timezones.h"
// SPI Defines
// We are going to use SPI 0, and allocate it to the following GPIO pins
@ -27,6 +28,12 @@
#define PIN_CS 17
#define PIN_SCK 18
#define PIN_MOSI 19
#define PIN_DC 16
#define PIN_RST 20
#define NTP_DELTA 2208988800UL
volatile u16_t timezone_index = 353;
extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
if (sec == 0) {
@ -37,12 +44,8 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
printf("SNTP callback received: %lu\n", (unsigned long)sec);
// Convert NTP timestamp to Unix timestamp
#define NTP_DELTA 2208988800UL
time_t unix_time = (time_t)(sec - NTP_DELTA);
u16_t timezone_offset = 7200;
unix_time += timezone_offset;
struct tm *time_info = gmtime(&unix_time);
if (time_info == NULL) {
@ -64,11 +67,13 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
.sec = static_cast<int8_t>(time_info->tm_sec)
};
timezone_offset(timezone_index, dt);
// Set RTC
if (rtc_set_datetime(&dt)) {
printf("SNTP: Time synchronized 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");
"Local");
} else {
printf("SNTP: Failed to set RTC\n");
}
@ -85,7 +90,7 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
if (rtc_set_datetime(&dt)) {
printf("SNTP: Time synchronized 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");
"Local");
} else {
printf("SNTP: Failed to set RTC\n");
}

29
timezones.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef SMART_ALARM_TIMEZONES_H
#define SMART_ALARM_TIMEZONES_H
#include "lwip/arch.h"
typedef struct {
char zone_name[31]; // Timezone name (e.g., "Europe/Berlin")
char std_abbr[6]; // Standard time abbreviation
char dst_abbr[6]; // DST abbreviation
s8_t std_offset_hours; // Standard time UTC offset hours (-12 to +14)
u8_t std_offset_mins; // Standard time UTC offset minutes (0-59)
s8_t dst_offset_hours; // DST UTC offset hours (-12 to +14)
u8_t dst_offset_mins; // DST UTC offset minutes (0-59)
u8_t dst_start_month; // DST start month (1-12)
u8_t dst_start_week; // DST start week (1-5, where 5=last week)
u8_t dst_start_day; // DST start day of week (0=Sunday..6=Saturday)
s8_t dst_start_hour; // DST start hour (0-23)
u8_t dst_start_min; // DST start minute (0-59)
u8_t dst_end_month; // DST end month (1-12)
u8_t dst_end_week; // DST end week (1-5, where 5=last week)
u8_t dst_end_day; // DST end day of week (0=Sunday..6=Saturday)
u8_t dst_end_hour; // DST end hour (0-23)
u8_t dst_end_min; // DST end minute (0-59)
bool has_dst; // 1 if timezone uses DST, 0 otherwise
u8_t reserved; // Padding for alignment
} timezone_t;
extern const timezone_t timezones[];
void timezone_offset(u16_t timezone_index, datetime_t &dt);
#endif //SMART_ALARM_TIMEZONES_H