working with DEBUG

This commit is contained in:
Martin Brodbeck 2023-01-03 08:57:39 +01:00
parent 25c11e465e
commit 258a0c051f
7 changed files with 61 additions and 33 deletions

View file

@ -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
"$<$<CONFIG:DEBUG>:DEBUG>"
)
# Add the standard include files to the build
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}

View file

@ -1,4 +1,4 @@
#include <cstdio>
// #include <cstdio>
#include <ctime>
#include <string>
@ -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);

View file

@ -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;
}

View file

@ -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];

View file

@ -1,10 +1,11 @@
#include "ntp_client.h"
#include <ctime>
#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() {

View file

@ -1,7 +1,5 @@
#pragma once
#include <ctime>
#include "pico/stdlib.h"
#include "lwip/apps/sntp.h"

View file

@ -51,25 +51,30 @@ std::vector<WasteDate> 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;
}
}
@ -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);
}