From 3281a3ecd55a540c2ebdfaf3c3968047e608ec54 Mon Sep 17 00:00:00 2001 From: Martin Crossley Date: Thu, 2 Oct 2025 21:18:42 +0100 Subject: [PATCH 1/5] Add .md to README and clarify what example does Renamed README to README.md to improve how it renders in an IDE and when browsing the online repository. Improved the example description to clarify why the temperature updates might appear unexpectedly sporadic to the user; and document how to read the current led status. --- pico_w/wifi/mqtt/{README => README.md} | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) rename pico_w/wifi/mqtt/{README => README.md} (87%) diff --git a/pico_w/wifi/mqtt/README b/pico_w/wifi/mqtt/README.md similarity index 87% rename from pico_w/wifi/mqtt/README rename to pico_w/wifi/mqtt/README.md index e1889bb61..e3bc2ab9b 100644 --- a/pico_w/wifi/mqtt/README +++ b/pico_w/wifi/mqtt/README.md @@ -34,8 +34,8 @@ When building the code set the host name of the MQTT server, e.g. export MQTT_SERVER=myhost cmake .. ``` - -The example should publish its core temperature to the /temperature topic. You can subscribe to this topic from another machine. +The example checks its core temperature every ten seconds and if it has changed, publishes it to the +/temperature topic. You can subscribe to this topic from another machine. ``` mosquitto_sub -h $MQTT_SERVER -t '/temperature' @@ -47,6 +47,11 @@ You can turn the led on and off by publishing messages. mosquitto_pub -h $MQTT_SERVER -t '/led' -m on mosquitto_pub -h $MQTT_SERVER -t '/led' -m off ``` +You can check the current the state of the led by subscribing to the /led/state topic on another machine. +``` +mosquitto_sub -h $MQTT_SERVER -t '/led_state' +``` + # Security From 5a4555833ebdfafef6c16b31ec16253ed790d290 Mon Sep 17 00:00:00 2001 From: Martin Crossley Date: Thu, 9 Oct 2025 21:52:03 +0100 Subject: [PATCH 2/5] Improve comments in mqtt_client example --- pico_w/wifi/mqtt/mqtt_client.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pico_w/wifi/mqtt/mqtt_client.c b/pico_w/wifi/mqtt/mqtt_client.c index b70d6885d..c498311e8 100644 --- a/pico_w/wifi/mqtt/mqtt_client.c +++ b/pico_w/wifi/mqtt/mqtt_client.c @@ -243,6 +243,8 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection if (!state->connect_done) { panic("Failed to connect to mqtt server"); } + // note that the main() loop will soon terminate because mqtt_client_is_connected() + // will return false. } else { panic("Unexpected status"); @@ -368,7 +370,13 @@ int main(void) { panic("dns request failed"); } + // We are not in a callback but we can get away with calling mqtt_client_is_connected() + // because it's a read-only operation while (!state.connect_done || mqtt_client_is_connected(state.mqtt_client_inst)) { + // As supplied the example configures cyw43_arch for thread_safe_background operation + // by linking `ico_cyw43_arch_lwip_threadsafe_background` in CMakeLists.txt, so the + // following two lines are unnecessary (but do no harm). However you will need them + // if you reconfigure the build to use cyw43_arch in polling mode. cyw43_arch_poll(); cyw43_arch_wait_for_work_until(make_timeout_time_ms(10000)); } From a23bca58ddf7b6de1f9e2d5e011b5e521c12d95b Mon Sep 17 00:00:00 2001 From: Martin Crossley Date: Fri, 10 Oct 2025 14:39:47 +0100 Subject: [PATCH 3/5] fix typo in comment --- pico_w/wifi/mqtt/mqtt_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico_w/wifi/mqtt/mqtt_client.c b/pico_w/wifi/mqtt/mqtt_client.c index c498311e8..8b454eaef 100644 --- a/pico_w/wifi/mqtt/mqtt_client.c +++ b/pico_w/wifi/mqtt/mqtt_client.c @@ -374,7 +374,7 @@ int main(void) { // because it's a read-only operation while (!state.connect_done || mqtt_client_is_connected(state.mqtt_client_inst)) { // As supplied the example configures cyw43_arch for thread_safe_background operation - // by linking `ico_cyw43_arch_lwip_threadsafe_background` in CMakeLists.txt, so the + // by linking `pico_cyw43_arch_lwip_threadsafe_background` in CMakeLists.txt, so the // following two lines are unnecessary (but do no harm). However you will need them // if you reconfigure the build to use cyw43_arch in polling mode. cyw43_arch_poll(); From e46d2779a7774ab642e3e35428d1c4c831ddd9b4 Mon Sep 17 00:00:00 2001 From: Martin Crossley Date: Thu, 13 Nov 2025 11:54:47 +0000 Subject: [PATCH 4/5] protect call to lwIP mqtt_client_new() Note that start_client() may be called direct from main() - i.e. not in a callback --- pico_w/wifi/mqtt/mqtt_client.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pico_w/wifi/mqtt/mqtt_client.c b/pico_w/wifi/mqtt/mqtt_client.c index 8b454eaef..34fc6552d 100644 --- a/pico_w/wifi/mqtt/mqtt_client.c +++ b/pico_w/wifi/mqtt/mqtt_client.c @@ -259,8 +259,10 @@ static void start_client(MQTT_CLIENT_DATA_T *state) { const int port = MQTT_PORT; INFO_printf("Warning: Not using TLS\n"); #endif - + // mqtt_client_new() has LWIP_ASSERT_CORE_LOCKED(), so we should protect this call + cyw43_arch_lwip_begin(); state->mqtt_client_inst = mqtt_client_new(); + cyw43_arch_lwip_end(); if (!state->mqtt_client_inst) { panic("MQTT client instance creation error"); } From 12bbe5c154cefef55d05e52896ff1e2cf6d211ef Mon Sep 17 00:00:00 2001 From: Martin Crossley Date: Thu, 13 Nov 2025 15:41:00 +0000 Subject: [PATCH 5/5] only poll cyw43_arch() in main() if we are in polling mode --- pico_w/wifi/mqtt/mqtt_client.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pico_w/wifi/mqtt/mqtt_client.c b/pico_w/wifi/mqtt/mqtt_client.c index 34fc6552d..b5ba087cd 100644 --- a/pico_w/wifi/mqtt/mqtt_client.c +++ b/pico_w/wifi/mqtt/mqtt_client.c @@ -375,12 +375,18 @@ int main(void) { // We are not in a callback but we can get away with calling mqtt_client_is_connected() // because it's a read-only operation while (!state.connect_done || mqtt_client_is_connected(state.mqtt_client_inst)) { - // As supplied the example configures cyw43_arch for thread_safe_background operation - // by linking `pico_cyw43_arch_lwip_threadsafe_background` in CMakeLists.txt, so the - // following two lines are unnecessary (but do no harm). However you will need them - // if you reconfigure the build to use cyw43_arch in polling mode. + + #ifdef PICO_CYW43_ARCH_POLL + // if you use cyw43_arch in lwip_poll mode then you must periodically call + // cyw43_arch_poll() from your main loop (see SDK network API documentaion) cyw43_arch_poll(); cyw43_arch_wait_for_work_until(make_timeout_time_ms(10000)); + #else + // as supplied the example configures cyw43_arch for thread_safe_background + // operation (see CMakeLists.txt) so there's no need to poll + sleep_ms(10000); + #endif + } INFO_printf("mqtt client exiting\n");