diff --git a/mqtt_client.c b/mqtt_client.c index 8aa917f..b06210c 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -8,6 +8,7 @@ #include "pico/stdlib.h" #include "pico/cyw43_arch.h" #include "lwip/apps/mqtt.h" +#include "hardware/watchdog.h" // #include #include #include @@ -65,9 +66,15 @@ static void start_client(MQTT_CLIENT_DATA_T *state) { } void mqtt_client_pub_message(mqtt_client_config *config, const char *message) { - if (mqtt_publish(config->state.mqtt_client_inst, config->topic, message, strlen(message), MQTT_PUBLISH_QOS, MQTT_PUBLISH_RETAIN, pub_request_cb, NULL) != ERR_OK) { - panic("MQTT publish error"); - } + + if (config->state.connect_done || !mqtt_client_is_connected(config->state.mqtt_client_inst)) { + printf("Can't send MQTT message because client is not connected\n"); + } + cyw43_arch_lwip_begin(); + if (mqtt_publish(config->state.mqtt_client_inst, config->topic, message, strlen(message), MQTT_PUBLISH_QOS, MQTT_PUBLISH_RETAIN, pub_request_cb, NULL) != ERR_OK) { + panic("MQTT publish error"); + } + cyw43_arch_lwip_end(); } void mqtt_client_init(mqtt_client_config *config, const char *topic, const char *device_name) { diff --git a/node1.c b/node1.c index 603778d..b48d814 100644 --- a/node1.c +++ b/node1.c @@ -15,7 +15,8 @@ // 5 sec loop is for testing // #define LOOP_INTERVAL_MS 5000 -#define LOOP_INTERVAL_MS 30000 +#define LOOP_INTERVAL_MS 10000 +// #define LOOP_INTERVAL_MS 30000 /** * Balcony Weather Station Node 1 @@ -35,17 +36,15 @@ bool comms_led_state = false; void comms_led_blink() { printf("COMMS LED BLINK COUNT: %d\n", comms_led_blink_count); comms_led_blink_count++; - if(!comms_led_state) { - gpio_put(16, true); - comms_led_state = true; - } + comms_led_state = true; comms_led_off_time = make_timeout_time_ms(1000); + gpio_put(16, comms_led_state); } void comms_led_update() { - if (comms_led_state && time_reached(comms_led_off_time)) { - gpio_put(16, false); + if (time_reached(comms_led_off_time)) { comms_led_state = false; + gpio_put(16, comms_led_state); } } @@ -137,6 +136,7 @@ static bool cb_30(__unused struct repeating_timer *t) { int main() { stdio_init_all(); + watchdog_enable(60000, 1); // Initialize communication LED comms_led_init(); @@ -158,5 +158,6 @@ int main() { sleep_us(100); tight_loop_contents(); mqtt_client_do_network_stuff(&mqtt_config); + watchdog_update(); } } diff --git a/pms5003.c b/pms5003.c index 1ec09e7..f2813b8 100644 --- a/pms5003.c +++ b/pms5003.c @@ -62,6 +62,7 @@ static void on_uart_rx() { // end of message if (rx_i == 31) { readings[reading_i] = extract_pm_values_from_rx_buf(); + printf("pm1: %f, pm2.5: %f, pm10: %f\n", readings[reading_i].pm1, readings[reading_i].pm2_5, readings[reading_i].pm10); reading_i++; }