diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f369538..3a62026 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,11 @@ pico_set_program_version(${CMAKE_PROJECT_NAME} ${PROJECT_VERSION}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-volatile") +target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE + # If the debug configuration pass the DEBUG define to the compiler + "$<$:DEBUG>" +) + # Add the standard include files to the build target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR} diff --git a/src/abfall.cpp b/src/abfall.cpp index dc4923c..aad5c27 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -1,4 +1,4 @@ -#include +// #include #include #include @@ -23,6 +23,10 @@ int main() { stdio_init_all(); rtc_init(); +#ifdef DEBUG + printf("!!! DEBUG mode!!!\n\n"); +#endif + WS2812 led(WS2812_PIN); wifi_setup(); @@ -31,16 +35,22 @@ int main() { HttpClient client; std::string csv(""); for (int i = 1; i <= 10; i++) { +#ifdef DEBUG printf("Attempt %d for retrieving data.\n", i); +#endif csv = client.retrieveWasteDatesAsCsv(); if (csv.length() > 0) { +#ifdef DEBUG printf("Data received!\n"); +#endif break; } } - + if (csv.length() == 0) { +#ifdef DEBUG printf("Error getting data. Exiting!"); +#endif cyw43_arch_deinit(); return 1; } @@ -50,13 +60,12 @@ int main() { datetime_t dt; auto dates = parseCsv(csv); +#ifdef DEBUG printf("Number of Dates: %d\n", dates.size()); - +#endif 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); diff --git a/src/http_client.cpp b/src/http_client.cpp index 8f9ece8..347b3bb 100644 --- a/src/http_client.cpp +++ b/src/http_client.cpp @@ -1,5 +1,6 @@ #include "http_client.h" +/* void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err) { printf("Transfer complete\n"); @@ -19,14 +20,15 @@ err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u return ERR_OK; } +*/ err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) { - bool *test = (bool *)arg; - //printf("..."); + bool *received = (bool *)arg; + pbuf_copy_partial(p, myBuffer, p->tot_len, 0); // printf("%s", myBuffer); - *test = true; + *received = true; return ERR_OK; } @@ -40,27 +42,27 @@ HttpClient::HttpClient() { std::string HttpClient::retrieveWasteDatesAsCsv() { uint16_t port = 80; - std::string test(""); + std::string result(""); err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv", &m_settings, body_callback, &m_received, nullptr); - //printf("Status %d\n", err); - //printf("Waiting for waste dates "); + // If there was an error, return empty result. + if (err != ERR_OK) { + return result; + } uint wait_count = 0; while (m_received == false) { ++wait_count; - //printf("."); + if (wait_count >= 10) { - return test; + return result; } sleep_ms(1000); } - //printf(" received.\n"); + result.append(myBuffer); - test.append(myBuffer); - - return test; + return result; } \ No newline at end of file diff --git a/src/http_client.h b/src/http_client.h index c4c81f0..cc8d2d0 100644 --- a/src/http_client.h +++ b/src/http_client.h @@ -4,7 +4,7 @@ #include "lwip/apps/http_client.h" #include "pico/cyw43_arch.h" -#include "pico/stdlib.h" +//#include "pico/stdlib.h" static char myBuffer[2048]; diff --git a/src/ntp_client.cpp b/src/ntp_client.cpp index c6175db..3a53db6 100644 --- a/src/ntp_client.cpp +++ b/src/ntp_client.cpp @@ -1,10 +1,11 @@ #include "ntp_client.h" +#include + #include "hardware/rtc.h" #include "pico/util/datetime.h" - // Called by lwip sntp in order to set the time void set_system_time(u32_t sec) { time_t epoch = sec; @@ -17,9 +18,14 @@ void set_system_time(u32_t sec) { datetime.min = utc->tm_min; datetime.sec = utc->tm_sec; datetime.dotw = utc->tm_wday; - if (rtc_set_datetime(&datetime) == true) { + + bool success = rtc_set_datetime(&datetime); + +#ifdef DEBUG + if (success) { printf("RTC successfully set.\n"); } +#endif } void NtpClient::setDateTime() { diff --git a/src/ntp_client.h b/src/ntp_client.h index 3b9e94c..dd4da70 100644 --- a/src/ntp_client.h +++ b/src/ntp_client.h @@ -1,7 +1,5 @@ #pragma once -#include - #include "pico/stdlib.h" #include "lwip/apps/sntp.h" diff --git a/src/utils.cpp b/src/utils.cpp index 6afcf51..fa5a245 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -51,25 +51,30 @@ std::vector parseCsv(const std::string &csv) { switch (tokenPos) { case 0: - printf("Gelber Sack: %s\n", token.c_str()); + // printf("Gelber Sack: %s\n", token.c_str()); wd.wasteTypes.push_back(Waste::GelberSack); break; case 1: - printf("Papiertonne: %s\n", token.c_str()); + // printf("Papiertonne: %s\n", token.c_str()); wd.wasteTypes.push_back(Waste::Papiertonne); break; case 2: - printf("Biotonne: %s\n", token.c_str()); + // printf("Biotonne: %s\n", token.c_str()); wd.wasteTypes.push_back(Waste::Biotonne); break; case 3: - printf("Restmüll: %s\n", token.c_str()); + // printf("Restmüll: %s\n", token.c_str()); wd.wasteTypes.push_back(Waste::Restmuell); break; case 4: - printf("Problemstoffmobil: %s\n", token.c_str()); + // printf("Problemstoffmobil: %s\n", token.c_str()); wd.wasteTypes.push_back(Waste::Problemstoffmobil); break; + default: +#ifdef DEBUG + printf("Unknown waste token detected.\n"); +#endif + break; } } @@ -78,11 +83,11 @@ std::vector parseCsv(const std::string &csv) { } } - //for(const auto& ymd: dates) { - // std::cout << "Current Year: " << static_cast(ymd.date.year()) - // << ", Month: " << static_cast(ymd.date.month()) - // << ", Day: " << static_cast(ymd.date.day()) << '\n'; - //} + // for(const auto& ymd: dates) { + // std::cout << "Current Year: " << static_cast(ymd.date.year()) + // << ", Month: " << static_cast(ymd.date.month()) + // << ", Day: " << static_cast(ymd.date.day()) << '\n'; + // } return dates; } @@ -126,8 +131,9 @@ int wifi_setup_impl(uint32_t country, const string &ssid, const string &pw, bool cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); } else { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); - +#ifdef DEBUG printf("IP: %s\n", ip4addr_ntoa(netif_ip_addr4(netif_default))); +#endif } return status; @@ -145,7 +151,9 @@ void wifi_setup() { res = wifi_setup_impl(country, ssid, pw, true); firstTry = false; } else { +#ifdef DEBUG printf("Setting up connection failed. Trying again after 5 sec...\n"); +#endif sleep_ms(5000); res = wifi_setup_impl(country, ssid, pw, false); }