hello!!
I'm starting with the pico board, I can't find the solution to make Freertos, lwip and mqtt work.
I want to publish with a mqtt and freertos task.
With the NO_SYS= 1 mode I can publish, but after a while it hangs, or sends 10 messages in a row.
With mode NO_SYS= 0 it does not start directly, it does nothing.
I pass the code I have! If anyone can tell me what it could be or where to investigate, I would greatly appreciate it!
I've already read a lot of things and I can't find the solution!
el CMAKE
I'm starting with the pico board, I can't find the solution to make Freertos, lwip and mqtt work.
I want to publish with a mqtt and freertos task.
With the NO_SYS= 1 mode I can publish, but after a while it hangs, or sends 10 messages in a row.
With mode NO_SYS= 0 it does not start directly, it does nothing.
I pass the code I have! If anyone can tell me what it could be or where to investigate, I would greatly appreciate it!
I've already read a lot of things and I can't find the solution!

Code:
#include <FreeRTOS.h>#include <queue.h>#include <task.h>#include <stdio.h>#include "pico/stdlib.h"#include "pico/cyw43_arch.h"#include "pico/multicore.h"#include "hardware/structs/rosc.h"#include "hardware/gpio.h"#include "portable.h"//#include "hardware/adc.h"#include <string.h>#include <time.h>#include "lwip/init.h" ///**********#include "lwip/pbuf.h"#include "lwip/tcp.h"#include "lwip/dns.h"#include "lwip/altcp_tcp.h"#include "lwip/altcp_tls.h"#include "lwip/apps/mqtt.h"#include "lwip/apps/mqtt_priv.h"#ifndef RUN_FREERTOS_ON_CORE#define RUN_FREERTOS_ON_CORE 1#endif//********** mis defines *************//static QueueHandle_t xQueue = NULL;//#define PICO_CYW43_ARCH_FREERTOS 1extern unsigned char ucHeap[128*1024];#define LED_ROJO 16#define BUTTON_GPIO_17 17#define DEBUG_printf printf#define MQTT_SERVER_HOST "telemetria"#define MQTT_SERVER_PORT 1883#define PUBLISH_TOPIC "/pruebas/texto"#define WILL_TOPIC "/pruebas/uvyt" /*uvyt: ultima voluntad y testamentp*/#define WILL_MSG "sin conexion a broker" /*uvyt: ultima voluntad y testamentp*/#define MS_PUBLISH_PERIOD 5000#define MQTT_TLS 0 // needs to be 1 for AWS IoTtypedef struct MQTT_CLIENT_T_ { ip_addr_t remote_addr; mqtt_client_t *mqtt_client; u8_t receiving; u32_t received; u32_t counter; u32_t reconnect;} MQTT_CLIENT_T;void led_task(void *pvParameters);void publish_task(void* pvParameters);void dns_found(const char *name, const ip_addr_t *ipaddr, void *callback_arg);//void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status);void mqtt_pub_request_cb(void *arg, err_t err);err_t mqtt_app_connect(MQTT_CLIENT_T *client);err_t mqtt_app_publish(MQTT_CLIENT_T *client, char *payload);err_t mqtt_app_connect(MQTT_CLIENT_T *client);void perform_payload( char *p);const uint LED_PIN = 16;const uint DOOR_SWITCH_PIN = 17;// Perform initialisationstatic MQTT_CLIENT_T* mqtt_client_init(void){ MQTT_CLIENT_T *client = calloc(1, sizeof(MQTT_CLIENT_T)); if (!client) { DEBUG_printf("failed to allocate state\n"); return NULL; } client->receiving = 0; client->received = 0; return client;}void dns_found(const char *name, const ip_addr_t *ipaddr, void *callback_arg){ MQTT_CLIENT_T *client = (MQTT_CLIENT_T*)callback_arg; DEBUG_printf("DNS query finished with resolved addr of %s.\n", ip4addr_ntoa(ipaddr)); client->remote_addr = *ipaddr;}void run_dns_lookup(MQTT_CLIENT_T *client){ DEBUG_printf("Running DNS query for %s.\n", MQTT_SERVER_HOST); cyw43_arch_lwip_begin(); err_t err = dns_gethostbyname(MQTT_SERVER_HOST, &(client->remote_addr), dns_found, client); cyw43_arch_lwip_end(); if (err == ERR_ARG) { DEBUG_printf("failed to start DNS query\n"); return; } if (err == ERR_OK) { DEBUG_printf("no lookup needed"); return; } while (client->remote_addr.addr == 0) { cyw43_arch_poll(); sleep_ms(1); }}static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status){ if (status != 0) { DEBUG_printf("Error during connection: err %d.\n", status); } else { DEBUG_printf("MQTT connected.\n"); }}/* * Called when publish is complete either with success or failure */void mqtt_pub_request_cb(void *arg, err_t err) { MQTT_CLIENT_T *client = (MQTT_CLIENT_T *)arg; printf("request publish ERROR %d \n", err); client->receiving = 0; client->received++;}/* * The app publishing */err_t mqtt_app_publish(MQTT_CLIENT_T *client, char *payload){ err_t err; u8_t qos = 0; /* 0 1 or 2, see MQTT specification */ u8_t retain = 0; // cyw43_arch_lwip_begin(); // sleep_ms(2); err = mqtt_publish(client->mqtt_client, PUBLISH_TOPIC , payload, strlen(payload), qos, retain, mqtt_pub_request_cb, client); // cyw43_arch_lwip_end(); if(err != ERR_OK) { DEBUG_printf("**** Publish fail***** %d\n", err); cyw43_arch_lwip_end(); sleep_ms(10); cyw43_arch_lwip_begin(); sleep_ms(10); } return err;}/* Initiate client and connect to server, if this fails immediately an error code is returned * otherwise mqtt_connection_cb will be called with connection result after attempting to * to establish a connection with the server. For now MQTT version 3.1.1 is always used */err_t mqtt_app_connect(MQTT_CLIENT_T *client){ struct mqtt_connect_client_info_t ci; err_t err; memset(&ci, 0, sizeof(ci)); ci.client_id = "PicoW"; ci.client_user = NULL; ci.client_pass = NULL; ci.keep_alive = 0; ci.will_topic = WILL_TOPIC; ci.will_msg = WILL_MSG; ci.will_retain = 0; ci.will_qos = 2; err = mqtt_client_connect(client->mqtt_client, &(client->remote_addr), MQTT_SERVER_PORT, mqtt_connection_cb, client, &ci); if (err != ERR_OK) { DEBUG_printf("ERROR en mqtt_app_connect() %d \n",err); } return err;}void perform_payload( char *p){ time_t s; struct tm* current_time; // time in seconds s = time(NULL); // to get current time current_time = localtime(&s); sprintf( p, "%02d:%02d:%02d", current_time->tm_hour, current_time->tm_min, current_time->tm_sec );}//*******************************************************************void led_task(void *pvParameters){ while(1) { gpio_put(LED_ROJO, 1); vTaskDelay(350); gpio_put(LED_ROJO, 0); vTaskDelay(350); }}//*******************************************************************void publish_task(void *pvParameters){ MQTT_CLIENT_T *client = (MQTT_CLIENT_T *)pvParameters; int cont= 0; int aux; err_t error; char payload[16]; size_t heap; DEBUG_printf("Inicio publish_task\n"); while(true) { //cyw43_arch_poll() se llama periodicamente para procesar eventos de red cyw43_arch_poll(); sleep_ms(1); cyw43_arch_lwip_begin(); aux= mqtt_client_is_connected(client->mqtt_client); heap= xPortGetFreeHeapSize(); DEBUG_printf(" tamaño de heap: %d \n", heap); heap= xPortGetMinimumEverFreeHeapSize(); DEBUG_printf(" tamaño de heapMIN: %d \n", heap); if (aux){ perform_payload( payload ); //sprintf(payload,"%05d", client->counter); error = mqtt_app_publish(client,payload); // <-- Publish! if ( error == ERR_OK) { DEBUG_printf("%s publicacion: %d\n", payload, client->counter); client->counter++; } } else{ cont++; DEBUG_printf("sin conexion\n"); if (mqtt_app_connect(client) != ERR_OK) { DEBUG_printf("Failed mqtt_app_connect()\n"); client->reconnect++; vTaskDelay(100); } else{ DEBUG_printf("mqtt re-connected\n"); } printf("reconexion: %d error %d \n", client->reconnect,error); } cyw43_arch_lwip_end(); vTaskDelay(10000); }}/*----------------------------------- main() --------------------------------*/int main(){ err_t error; stdio_init_all(); //GPIO init gpio_init(LED_ROJO); gpio_set_dir(LED_ROJO,GPIO_OUT); if (cyw43_arch_init()) { printf("Wi-Fi init failed"); return -1; } else{ printf("Wi-Fi init success"); } cyw43_arch_enable_sta_mode(); DEBUG_printf("Connecting to WiFi...\n"); if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { DEBUG_printf("failed to connect.\n"); return 1; } else { DEBUG_printf("*** WIFI Connected.*** \n"); } // Comienza el proceso lwIP cyw43_arch_lwip_begin(); sleep_ms(100); MQTT_CLIENT_T *my_mqtt_client = mqtt_client_init(); run_dns_lookup(my_mqtt_client); my_mqtt_client->mqtt_client = mqtt_client_new(); my_mqtt_client->counter = 0; if (my_mqtt_client->mqtt_client == NULL) { DEBUG_printf("Failed to create new mqtt client\n"); } error = mqtt_app_connect(my_mqtt_client); if ( error == ERR_OK) { DEBUG_printf("mqtt_app_connect() OK\n"); } else{ DEBUG_printf("mqtt_app_connect() ERROR=%d\n", error); } xTaskCreate( publish_task, // Task to be run "PUBLISH_TASK", // Name of the Task for debugging and managing its Task Handle 2048, // Stack depth to be allocated for use with task's stack (see docs) my_mqtt_client, // Arguments needed by the Task (NULL because we don't have any) 6, // Task Priority - Higher the number the more priority [max is (configMAX_PRIORITIES - 1) provided in FreeRTOSConfig.h] NULL // Task Handle if available for managing the task ); xTaskCreate( led_task, // Task to be run "LED_task", // Name of the Task for debugging and managing its Task Handle 128, // Stack depth to be allocated for use with task's stack (see docs) NULL, // Arguments needed by the Task (NULL because we don't have any) 1, // Task Priority - Higher the number the more priority [max is (configMAX_PRIORITIES - 1) provided in FreeRTOSConfig.h] NULL // Task Handle if available for managing the task );#if 0// Create a 3rd task // Force it to run on core1 TaskHandle_t taskpublish_handle; UBaseType_t uxCoreAffinityMask; xTaskCreate(publish_task, "PUBLISH_TASK", 256,my_client, tskIDLE_PRIORITY-1, &( taskpublish_handle )); // To force to run on core0: // uxCoreAffinityMask = ( ( 1 << 0 )); uxCoreAffinityMask = ( ( 1 << 0 )); vTaskCoreAffinitySet( taskpublish_handle, uxCoreAffinityMask );#endif DEBUG_printf("*** inicio scheduler*** \n"); vTaskStartScheduler(); cyw43_arch_lwip_end(); cyw43_arch_deinit(); return 0;}
el CMAKE
Code:
cmake_minimum_required(VERSION 3.12)# Pull in SDK (must be before project)include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)include($ENV{FREERTOS_KERNEL_PATH}/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake)project(pico_examples C CXX ASM)set(CMAKE_C_STANDARD 11)set(CMAKE_CXX_STANDARD 17)#GAset(PICO_BOARD pico_w)#SET(FREERTOS_CONFIG_FILE_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/configs/FreeRTOS-Kernel" CACHE STRING "Local Config")include_directories("${FREERTOS_CONFIG_FILE_DIRECTORY}") # FreeRTOS config filesif (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0") message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")endif()#****************************# Initialize the SDKpico_sdk_init()add_compile_options(-Wall -Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int -Wno-unused-function # we have some for the docs that aren't called -Wno-maybe-uninitialized )if (PICO_CYW43_SUPPORTED) # set by BOARD=pico-w if (NOT TARGET pico_cyw43_arch) message("Skipping build as support is not available") else() set(WIFI_SSID "${WIFI_SSID}" CACHE INTERNAL "WiFi SSID") set(WIFI_PASSWORD "${WIFI_PASSWORD}" CACHE INTERNAL "WiFi password") if ("${WIFI_SSID}" STREQUAL "") message("Skipping build as WIFI_SSID is not defined") elseif ("${WIFI_PASSWORD}" STREQUAL "") message("Skipping build as WIFI_PASSWORD is not defined") else() add_executable(hello_serial hello_serial.c) pico_enable_stdio_usb(hello_serial 1) #original 0 pico_enable_stdio_uart(hello_serial 0)#original 1 target_compile_definitions(hello_serial PRIVATE WIFI_SSID=\"${WIFI_SSID}\" WIFI_PASSWORD=\"${WIFI_PASSWORD}\" # TEST_TCP_SERVER_IP=\"${TEST_TCP_SERVER_IP}\" NO_SYS=0 ) target_include_directories(hello_serial PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts ) # pull in common dependencies target_link_libraries(hello_serial #pico_cyw43_arch_lwip_poll #mqtt pico_cyw43_arch_lwip_sys_freertos pico_stdlib #pico_lwip_mbedtls #mqtt #pico_mbedtls #mqtt pico_lwip_mqtt #mqtt #hardware_adc # Hardware ADC #pico_cyw43_arch_lwip_threadsafe_background # we need Wifi #pico_cyw43_arch_none FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap #LWIP_PORT # LWIP config files #FreeRTOS-Kernel # FreeRTOS config files #pico_multicore #sacado GA #hardware_irq # pico_sync pico_lwip_iperf ) # create map/bin/hex/uf2 file etc. pico_add_extra_outputs(hello_serial) # add url via pico_set_program_url #example_auto_set_url(hello_serial) endif() endif()endif()
Statistics: Posted by Judy_ — Wed Jun 26, 2024 6:53 pm — Replies 0 — Views 17