still doesnt work :((((
This commit is contained in:
parent
fc24139f5d
commit
8b37490e88
@ -13,9 +13,9 @@
|
||||
#include <lwip/arch.h>
|
||||
#include <pico/stdio.h>
|
||||
|
||||
#include "net_utils.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "net_utils.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
@ -29,12 +29,16 @@ using nlohmann::json;
|
||||
#define PIN_SCK 18
|
||||
#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";
|
||||
// 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];
|
||||
|
||||
@ -47,8 +51,8 @@ static err_t connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
err_t set_timezone_offset(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) {
|
||||
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) {
|
||||
@ -65,9 +69,10 @@ err_t set_timezone_offset(void *arg, struct altcp_pcb *conn, struct pbuf *p, err
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t timezone_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
|
||||
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);
|
||||
u16_t *offset = static_cast<u16_t *>(arg);
|
||||
|
||||
if (p == NULL) {
|
||||
printf("Connection closed by remote host\n");
|
||||
@ -82,9 +87,10 @@ err_t timezone_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
|
||||
try {
|
||||
json response = json::parse(buffer);
|
||||
*offset = response["raw_offset"].get<int>() + response["dst_offset"].get<int>();
|
||||
*offset =
|
||||
response["raw_offset"].get<int>() + response["dst_offset"].get<int>();
|
||||
request_complete = true;
|
||||
} catch (const std::exception& e) {
|
||||
} catch (const std::exception &e) {
|
||||
printf("JSON parse error: %s\n", e.what());
|
||||
request_error = true;
|
||||
}
|
||||
@ -104,8 +110,6 @@ void timezone_err(void *arg, const err_t err) {
|
||||
request_error = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::tuple<u16_t, err_t> get_timezone_offset() {
|
||||
printf("getting timezone offset\n");
|
||||
tcp_pcb *pcb = tcp_new();
|
||||
@ -119,11 +123,10 @@ std::tuple<u16_t, err_t> get_timezone_offset() {
|
||||
offset = 0;
|
||||
|
||||
tcp_arg(pcb, &offset);
|
||||
ip_addr_t server_ip;
|
||||
dns_resolve_sync(timezone_api_host, &server_ip, 5000);
|
||||
|
||||
if (tcp_connect(pcb, &server_ip, 80, connected_callback) == ERR_OK) {
|
||||
tcp_write(pcb, timezone_request, strlen(timezone_request), TCP_WRITE_FLAG_COPY);
|
||||
if (tcp_connect(pcb, &timezone_server_ip, 80, connected_callback) == ERR_OK) {
|
||||
tcp_write(pcb, timezone_request, strlen(timezone_request),
|
||||
TCP_WRITE_FLAG_COPY);
|
||||
tcp_output(pcb);
|
||||
|
||||
// Wait until request completes or errors out
|
||||
@ -140,39 +143,35 @@ std::tuple<u16_t, err_t> get_timezone_offset() {
|
||||
|
||||
if (request_error) {
|
||||
printf("Request failed due to error\n");
|
||||
return {0,ERR_ABRT};
|
||||
return {0, ERR_ABRT};
|
||||
} else if (elapsed_ms >= timeout_ms) {
|
||||
printf("Request timed out\n");
|
||||
return {0,ERR_TIMEOUT};
|
||||
return {0, ERR_TIMEOUT};
|
||||
}
|
||||
} else {
|
||||
printf("Failed to connect\n");
|
||||
tcp_close(pcb);
|
||||
return {0,ERR_ABRT};
|
||||
return {0, ERR_ABRT};
|
||||
}
|
||||
|
||||
return {offset,ERR_OK};
|
||||
return {offset, ERR_OK};
|
||||
}
|
||||
|
||||
|
||||
|
||||
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_sec = []()->u16_t
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
auto [offset,err] = get_timezone_offset();
|
||||
if (err == ERR_OK)
|
||||
{
|
||||
u16_t timezone_offset_sec = []() -> u16_t {
|
||||
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
|
||||
unix_time += timezone_offset_sec;
|
||||
@ -215,6 +214,9 @@ int main() {
|
||||
}
|
||||
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);
|
||||
@ -248,6 +250,8 @@ int main() {
|
||||
IP_ADDR4(&dns_server, 8, 8, 8, 8);
|
||||
dns_setserver(1, &dns_server);
|
||||
|
||||
dns_resolve_sync(timezone_api_host, &timezone_server_ip, 5000);
|
||||
|
||||
printf("Initializing SNTP\n");
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
sntp_init();
|
||||
@ -261,10 +265,6 @@ int main() {
|
||||
|
||||
printf("SNTP initialized, waiting for time sync...\n");
|
||||
|
||||
get_timezone_offset();
|
||||
|
||||
|
||||
|
||||
while (true) {
|
||||
|
||||
// Keep the program running to allow SNTP to work
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user