try and fix mqtt panic

This commit is contained in:
Travis Shears 2025-09-14 09:25:21 +02:00
parent dc206625bf
commit 1a25129495
3 changed files with 19 additions and 10 deletions

View file

@ -8,6 +8,7 @@
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/apps/mqtt.h"
#include "hardware/watchdog.h"
// #include <cstdio>
#include <string.h>
#include <stdio.h>
@ -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 (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) {

13
node1.c
View file

@ -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_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();
}
}

View file

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