why did it stop working :((
This commit is contained in:
parent
60f0d8b690
commit
d6c8082338
177
smart_alarm.cpp
177
smart_alarm.cpp
@ -5,20 +5,19 @@
|
|||||||
#include "lwip/apps/sntp.h"
|
#include "lwip/apps/sntp.h"
|
||||||
#include "lwip/err.h"
|
#include "lwip/err.h"
|
||||||
#include "lwip/ip_addr.h"
|
#include "lwip/ip_addr.h"
|
||||||
#include "nlohmann/json.hpp"
|
|
||||||
#include "pico/cyw43_arch.h"
|
#include "pico/cyw43_arch.h"
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "pico/util/datetime.h"
|
#include "pico/util/datetime.h"
|
||||||
#include "wifi_conf.h"
|
#include "wifi_conf.h"
|
||||||
#include <lwip/arch.h>
|
#include <lwip/arch.h>
|
||||||
#include <pico/stdio.h>
|
#include <pico/stdio.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "lwip/dns.h"
|
#include "lwip/dns.h"
|
||||||
|
#include "lwip/init.h"
|
||||||
#include "lwip/tcp.h"
|
#include "lwip/tcp.h"
|
||||||
#include "net_utils.h"
|
#include "net_utils.h"
|
||||||
|
|
||||||
using nlohmann::json;
|
|
||||||
|
|
||||||
// SPI Defines
|
// SPI Defines
|
||||||
// We are going to use SPI 0, and allocate it to the following GPIO pins
|
// 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
|
// Pins can be changed, see the GPIO function select table in the datasheet for
|
||||||
@ -29,155 +28,14 @@ using nlohmann::json;
|
|||||||
#define PIN_SCK 18
|
#define PIN_SCK 18
|
||||||
#define PIN_MOSI 19
|
#define PIN_MOSI 19
|
||||||
|
|
||||||
// constexpr char* timezone_api_host = "worldtimeapi.org";
|
|
||||||
// constexpr char* timezone_request = "GET /api/ip HTTP/1.1\r\nHost:
|
|
||||||
// worldtimeapi.org\r\nUser-Agent: smart_clock/0.1.0\r\nAccept: */*\r\n\r\n";
|
|
||||||
constexpr char *timezone_api_host = "example.org";
|
|
||||||
constexpr char *timezone_request =
|
|
||||||
"GET / HTTP/1.1\r\nHost: example.org\r\nUser-Agent: curl/8.15.0\r\nAccept: "
|
|
||||||
"*/*\r\n\r\n";
|
|
||||||
static volatile bool request_complete = false;
|
|
||||||
static volatile bool request_error = false;
|
|
||||||
ip_addr_t timezone_server_ip;
|
|
||||||
|
|
||||||
static char buffer[1024];
|
|
||||||
|
|
||||||
static err_t connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
|
||||||
if (err == ERR_OK) {
|
|
||||||
printf("Connected to server successfully!\n");
|
|
||||||
tcp_write(tpcb, timezone_request, strlen(timezone_request), TCP_WRITE_FLAG_COPY);
|
|
||||||
tcp_output(tpcb);
|
|
||||||
} else {
|
|
||||||
printf("Connection failed with error: %d\n", err);
|
|
||||||
request_error = true;
|
|
||||||
}
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
err_t set_timezone_offset(void *arg, struct altcp_pcb *conn, struct pbuf *p,
|
|
||||||
err_t err) {
|
|
||||||
printf("\ncontent err %d\n", err);
|
|
||||||
|
|
||||||
if (err != ERR_OK || p == NULL) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
u16_t offset = 0;
|
|
||||||
while (offset < p->tot_len) {
|
|
||||||
char c = (char)pbuf_get_at(p, offset++);
|
|
||||||
putchar(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
pbuf_free(p); // Important: Free the pbuf
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
err_t timezone_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,
|
|
||||||
err_t err) {
|
|
||||||
extern volatile bool request_complete, request_error;
|
|
||||||
u16_t *offset = static_cast<u16_t *>(arg);
|
|
||||||
|
|
||||||
if (p == NULL) {
|
|
||||||
printf("Connection closed by remote host\n");
|
|
||||||
request_complete = true;
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (err == ERR_OK) {
|
|
||||||
printf("Received %u bytes: ", p->tot_len);
|
|
||||||
pbuf_copy_partial(p, buffer, p->tot_len, 0);
|
|
||||||
buffer[p->tot_len] = '\0';
|
|
||||||
|
|
||||||
try {
|
|
||||||
json response = json::parse(buffer);
|
|
||||||
*offset =
|
|
||||||
response["raw_offset"].get<int>() + response["dst_offset"].get<int>();
|
|
||||||
request_complete = true;
|
|
||||||
} catch (const std::exception &e) {
|
|
||||||
printf("JSON parse error: %s\n", e.what());
|
|
||||||
request_error = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
tcp_recved(tpcb, p->tot_len);
|
|
||||||
} else {
|
|
||||||
request_error = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pbuf_free(p);
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
void timezone_err(void *arg, const err_t err) {
|
|
||||||
extern volatile bool request_error;
|
|
||||||
printf("timezone_err: %d:%s\n", err, lwip_strerr(err));
|
|
||||||
request_error = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::tuple<u16_t, err_t> get_timezone_offset() {
|
|
||||||
printf("getting timezone offset\n");
|
|
||||||
tcp_pcb *pcb = tcp_new();
|
|
||||||
tcp_recv(pcb, timezone_recv);
|
|
||||||
tcp_err(pcb, timezone_err);
|
|
||||||
static u16_t offset = 0;
|
|
||||||
|
|
||||||
// Reset flags for new request
|
|
||||||
request_complete = false;
|
|
||||||
request_error = false;
|
|
||||||
offset = 0;
|
|
||||||
|
|
||||||
tcp_arg(pcb, &offset);
|
|
||||||
|
|
||||||
// ONLY initiate connection - do NOT send data here
|
|
||||||
if (tcp_connect(pcb, &timezone_server_ip, 80, connected_callback) == ERR_OK) {
|
|
||||||
// Wait until request completes or errors out
|
|
||||||
uint32_t timeout_ms = 10000;
|
|
||||||
uint32_t elapsed_ms = 0;
|
|
||||||
const uint32_t poll_interval = 50;
|
|
||||||
|
|
||||||
while (!request_complete && !request_error && elapsed_ms < timeout_ms) {
|
|
||||||
cyw43_arch_poll(); // Critical: Give lwIP time to process packets
|
|
||||||
sleep_ms(poll_interval);
|
|
||||||
elapsed_ms += poll_interval;
|
|
||||||
}
|
|
||||||
|
|
||||||
tcp_close(pcb);
|
|
||||||
|
|
||||||
if (request_error) {
|
|
||||||
printf("Request failed due to error\n");
|
|
||||||
return {0, ERR_ABRT};
|
|
||||||
} else if (elapsed_ms >= timeout_ms) {
|
|
||||||
printf("Request timed out\n");
|
|
||||||
return {0, ERR_TIMEOUT};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printf("Failed to connect\n");
|
|
||||||
tcp_close(pcb);
|
|
||||||
return {0, ERR_ABRT};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {offset, ERR_OK};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
|
extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
|
||||||
printf("SNTP callback received: %lu\n", (unsigned long)sec);
|
printf("SNTP callback received: %lu\n", (unsigned long)sec);
|
||||||
time_t unix_time = (time_t)sec;
|
time_t unix_time = (time_t)sec;
|
||||||
|
|
||||||
u16_t timezone_offset_sec = []() -> u16_t {
|
u16_t timezone_offset = 7200;
|
||||||
while (true) {
|
|
||||||
printf("calling get_timezone_offset() now\n");
|
|
||||||
auto [offset, err] = get_timezone_offset();
|
|
||||||
if (err == ERR_OK) {
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
printf("failed to get timezone offset, retrying\n");
|
|
||||||
}
|
|
||||||
}();
|
|
||||||
printf("got timezone\n");
|
|
||||||
|
|
||||||
// Apply timezone offset for local time
|
// Apply timezone offset for local time
|
||||||
unix_time += timezone_offset_sec;
|
unix_time += timezone_offset;
|
||||||
|
|
||||||
struct tm *time_info = gmtime(&unix_time);
|
struct tm *time_info = gmtime(&unix_time);
|
||||||
if (time_info == NULL) {
|
if (time_info == NULL) {
|
||||||
@ -204,7 +62,7 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
|
|||||||
if (rtc_set_datetime(&dt)) {
|
if (rtc_set_datetime(&dt)) {
|
||||||
printf("SNTP: Time set to %04d-%02d-%02d %02d:%02d:%02d %s\n", dt.year,
|
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,
|
dt.month, dt.day, dt.hour, dt.min, dt.sec,
|
||||||
(timezone_offset_sec == 0) ? "UTC" : "Local");
|
(timezone_offset == 0) ? "UTC" : "Local");
|
||||||
} else {
|
} else {
|
||||||
printf("SNTP: Failed to set RTC\n");
|
printf("SNTP: Failed to set RTC\n");
|
||||||
}
|
}
|
||||||
@ -246,24 +104,23 @@ int main() {
|
|||||||
printf("Mask: %s\n", ip4addr_ntoa(netif_ip_netmask4(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("Gateway: %s\n", ip4addr_ntoa(netif_ip_gw4(netif_default)));
|
||||||
|
|
||||||
ip_addr_t dns_server;
|
{
|
||||||
IP_ADDR4(&dns_server, 1, 1, 1, 1);
|
ip_addr_t ip_addr;
|
||||||
dns_setserver(0, &dns_server);
|
IP_ADDR4(&ip_addr, 1, 1, 1, 1);
|
||||||
|
dns_setserver(0, &ip_addr);
|
||||||
|
|
||||||
IP_ADDR4(&dns_server, 8, 8, 8, 8);
|
IP_ADDR4(&ip_addr, 8, 8, 8, 8);
|
||||||
dns_setserver(1, &dns_server);
|
dns_setserver(1, &ip_addr);
|
||||||
|
|
||||||
dns_resolve_sync(timezone_api_host, &timezone_server_ip, 5000);
|
|
||||||
|
|
||||||
printf("Initializing SNTP\n");
|
printf("Initializing SNTP\n");
|
||||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||||
sntp_init();
|
sntp_init();
|
||||||
ip_addr_t ntp_server;
|
|
||||||
if (ipaddr_aton("162.159.200.123", &ntp_server)) { // Cloudflare NTP
|
dns_resolve_sync("ntp.cesnet.cz", &ip_addr, 10000);
|
||||||
sntp_setserver(0, &ntp_server);
|
sntp_setserver(0, &ip_addr);
|
||||||
}
|
|
||||||
if (ipaddr_aton("129.6.15.28", &ntp_server)) { // NIST NTP
|
dns_resolve_sync("ntp0.fau.de", &ip_addr, 10000);
|
||||||
sntp_setserver(1, &ntp_server);
|
sntp_setserver(1, &ip_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("SNTP initialized, waiting for time sync...\n");
|
printf("SNTP initialized, waiting for time sync...\n");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user