still doesnt work :((((
This commit is contained in:
parent
fc24139f5d
commit
8b37490e88
@ -13,9 +13,9 @@
|
|||||||
#include <lwip/arch.h>
|
#include <lwip/arch.h>
|
||||||
#include <pico/stdio.h>
|
#include <pico/stdio.h>
|
||||||
|
|
||||||
#include "net_utils.h"
|
|
||||||
#include "lwip/dns.h"
|
#include "lwip/dns.h"
|
||||||
#include "lwip/tcp.h"
|
#include "lwip/tcp.h"
|
||||||
|
#include "net_utils.h"
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
@ -30,11 +30,15 @@ using nlohmann::json;
|
|||||||
#define PIN_MOSI 19
|
#define PIN_MOSI 19
|
||||||
|
|
||||||
// constexpr char* timezone_api_host = "worldtimeapi.org";
|
// 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_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_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_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_complete = false;
|
||||||
static volatile bool request_error = false;
|
static volatile bool request_error = false;
|
||||||
|
ip_addr_t timezone_server_ip;
|
||||||
|
|
||||||
static char buffer[1024];
|
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;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err_t set_timezone_offset(void *arg, struct altcp_pcb *conn, struct pbuf *p,
|
||||||
err_t set_timezone_offset(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) {
|
err_t err) {
|
||||||
printf("\ncontent err %d\n", err);
|
printf("\ncontent err %d\n", err);
|
||||||
|
|
||||||
if (err != ERR_OK || p == NULL) {
|
if (err != ERR_OK || p == NULL) {
|
||||||
@ -65,7 +69,8 @@ err_t set_timezone_offset(void *arg, struct altcp_pcb *conn, struct pbuf *p, err
|
|||||||
return ERR_OK;
|
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;
|
extern volatile bool request_complete, request_error;
|
||||||
u16_t *offset = static_cast<u16_t *>(arg);
|
u16_t *offset = static_cast<u16_t *>(arg);
|
||||||
|
|
||||||
@ -82,7 +87,8 @@ err_t timezone_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
json response = json::parse(buffer);
|
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;
|
request_complete = true;
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
printf("JSON parse error: %s\n", e.what());
|
printf("JSON parse error: %s\n", e.what());
|
||||||
@ -104,8 +110,6 @@ void timezone_err(void *arg, const err_t err) {
|
|||||||
request_error = true;
|
request_error = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::tuple<u16_t, err_t> get_timezone_offset() {
|
std::tuple<u16_t, err_t> get_timezone_offset() {
|
||||||
printf("getting timezone offset\n");
|
printf("getting timezone offset\n");
|
||||||
tcp_pcb *pcb = tcp_new();
|
tcp_pcb *pcb = tcp_new();
|
||||||
@ -119,11 +123,10 @@ std::tuple<u16_t, err_t> get_timezone_offset() {
|
|||||||
offset = 0;
|
offset = 0;
|
||||||
|
|
||||||
tcp_arg(pcb, &offset);
|
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) {
|
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_write(pcb, timezone_request, strlen(timezone_request),
|
||||||
|
TCP_WRITE_FLAG_COPY);
|
||||||
tcp_output(pcb);
|
tcp_output(pcb);
|
||||||
|
|
||||||
// Wait until request completes or errors out
|
// Wait until request completes or errors out
|
||||||
@ -154,25 +157,21 @@ std::tuple<u16_t, err_t> get_timezone_offset() {
|
|||||||
return {offset, ERR_OK};
|
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_sec = []() -> u16_t {
|
||||||
{
|
while (true) {
|
||||||
while (true)
|
printf("calling get_timezone_offset() now\n");
|
||||||
{
|
|
||||||
auto [offset, err] = get_timezone_offset();
|
auto [offset, err] = get_timezone_offset();
|
||||||
if (err == ERR_OK)
|
if (err == ERR_OK) {
|
||||||
{
|
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
printf("failed to get timezone offset, retrying\n");
|
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_sec;
|
||||||
@ -215,6 +214,9 @@ int main() {
|
|||||||
}
|
}
|
||||||
rtc_init();
|
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 initialisation. This example will use SPI at 1MHz.
|
||||||
spi_init(SPI_PORT, 1000 * 1000);
|
spi_init(SPI_PORT, 1000 * 1000);
|
||||||
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
|
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
|
||||||
@ -248,6 +250,8 @@ int main() {
|
|||||||
IP_ADDR4(&dns_server, 8, 8, 8, 8);
|
IP_ADDR4(&dns_server, 8, 8, 8, 8);
|
||||||
dns_setserver(1, &dns_server);
|
dns_setserver(1, &dns_server);
|
||||||
|
|
||||||
|
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();
|
||||||
@ -261,10 +265,6 @@ int main() {
|
|||||||
|
|
||||||
printf("SNTP initialized, waiting for time sync...\n");
|
printf("SNTP initialized, waiting for time sync...\n");
|
||||||
|
|
||||||
get_timezone_offset();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
// Keep the program running to allow SNTP to work
|
// Keep the program running to allow SNTP to work
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user