Move wifi stuff to utils
This commit is contained in:
parent
df3ef6f9fa
commit
75d630604d
4 changed files with 83 additions and 67 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
set(SOURCES
|
||||
ws2812.cpp
|
||||
utils.cpp
|
||||
http_client.cpp
|
||||
ntp_client.cpp
|
||||
abfall.cpp
|
||||
|
|
|
@ -10,8 +10,7 @@
|
|||
#include "pico/stdlib.h"
|
||||
#include "pico/util/datetime.h"
|
||||
|
||||
//#include "lwip/apps/http_client.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "ntp_client.h"
|
||||
#include "http_client.h"
|
||||
#include "ws2812.h"
|
||||
|
@ -20,80 +19,18 @@ using std::string;
|
|||
|
||||
#define WS2812_PIN 22
|
||||
|
||||
int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firstTry = true) {
|
||||
if (firstTry) {
|
||||
if (cyw43_arch_init_with_country(country)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
netif_set_hostname(netif_default, "AbfallPicoW");
|
||||
|
||||
if (cyw43_arch_wifi_connect_async(ssid.c_str(), pw.c_str(), CYW43_AUTH_WPA2_MIXED_PSK)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
int flashrate = 1000;
|
||||
int status = CYW43_LINK_UP + 1;
|
||||
|
||||
while (status >= 0 && status != CYW43_LINK_UP) {
|
||||
int status_new = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
|
||||
|
||||
if (status_new != status) {
|
||||
status = status_new;
|
||||
if (status < 0) {
|
||||
continue;
|
||||
}
|
||||
flashrate = flashrate / (status + 1);
|
||||
// printf("Connect status: %d %d\n", status, flashrate);
|
||||
}
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
sleep_ms(flashrate);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
sleep_ms(flashrate);
|
||||
}
|
||||
|
||||
if (status < 0) {
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
} else {
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
|
||||
printf("IP: %s\n", ip4addr_ntoa(netif_ip_addr4(netif_default)));
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const string ssid{"Apis cerana"};
|
||||
const string pw{"2JkJEh2vptVT"};
|
||||
const uint32_t country{CYW43_COUNTRY_GERMANY};
|
||||
|
||||
stdio_init_all();
|
||||
rtc_init();
|
||||
|
||||
WS2812 led(WS2812_PIN);
|
||||
|
||||
bool firstTry = true;
|
||||
int res = -1;
|
||||
do {
|
||||
if (firstTry) {
|
||||
res = wifi_setup(country, ssid, pw, true);
|
||||
firstTry = false;
|
||||
} else {
|
||||
printf("Setting up connection failed. Trying again after 5 sec...\n");
|
||||
sleep_ms(5000);
|
||||
res = wifi_setup(country, ssid, pw, false);
|
||||
}
|
||||
|
||||
} while (res != CYW43_LINK_UP);
|
||||
wifi_setup();
|
||||
|
||||
NtpClient::setDateTime();
|
||||
HttpClient client;
|
||||
std::string test = client.retrieveWasteDatesAsCsv();
|
||||
printf("%s\n", test.c_str());
|
||||
std::string csv = client.retrieveWasteDatesAsCsv();
|
||||
printf("%s\n", csv.c_str());
|
||||
|
||||
char datetime_buf[256];
|
||||
char *datetime_str = &datetime_buf[0];
|
||||
|
@ -101,6 +38,7 @@ int main() {
|
|||
|
||||
while (true) {
|
||||
rtc_get_datetime(&dt);
|
||||
|
||||
datetime_to_str(datetime_str, sizeof(datetime_buf), &dt);
|
||||
// printf("DateTime: %s\n", datetime_str);
|
||||
led.switchColor(Color::OFF);
|
||||
|
|
69
src/utils.cpp
Normal file
69
src/utils.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include "utils.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
int wifi_setup_impl(uint32_t country, const string &ssid, const string &pw, bool firstTry) {
|
||||
if (firstTry) {
|
||||
if (cyw43_arch_init_with_country(country)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
netif_set_hostname(netif_default, "AbfallPicoW");
|
||||
|
||||
if (cyw43_arch_wifi_connect_async(ssid.c_str(), pw.c_str(), CYW43_AUTH_WPA2_MIXED_PSK)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
int flashrate = 1000;
|
||||
int status = CYW43_LINK_UP + 1;
|
||||
|
||||
while (status >= 0 && status != CYW43_LINK_UP) {
|
||||
int status_new = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
|
||||
|
||||
if (status_new != status) {
|
||||
status = status_new;
|
||||
if (status < 0) {
|
||||
continue;
|
||||
}
|
||||
flashrate = flashrate / (status + 1);
|
||||
// printf("Connect status: %d %d\n", status, flashrate);
|
||||
}
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
sleep_ms(flashrate);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
sleep_ms(flashrate);
|
||||
}
|
||||
|
||||
if (status < 0) {
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
} else {
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
|
||||
printf("IP: %s\n", ip4addr_ntoa(netif_ip_addr4(netif_default)));
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void wifi_setup() {
|
||||
const string ssid{"Apis cerana"};
|
||||
const string pw{"2JkJEh2vptVT"};
|
||||
const uint32_t country{CYW43_COUNTRY_GERMANY};
|
||||
|
||||
bool firstTry = true;
|
||||
int res = -1;
|
||||
do {
|
||||
if (firstTry) {
|
||||
res = wifi_setup_impl(country, ssid, pw, true);
|
||||
firstTry = false;
|
||||
} else {
|
||||
printf("Setting up connection failed. Trying again after 5 sec...\n");
|
||||
sleep_ms(5000);
|
||||
res = wifi_setup_impl(country, ssid, pw, false);
|
||||
}
|
||||
|
||||
} while (res != CYW43_LINK_UP);
|
||||
}
|
8
src/utils.h
Normal file
8
src/utils.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "pico/cyw43_arch.h"
|
||||
|
||||
static int wifi_setup_impl(uint32_t country, const std::string &ssid, const std::string &pw, bool firstTry = true);
|
||||
void wifi_setup();
|
Loading…
Reference in a new issue