This is an example on how to create an IoT Device with the Raspberry PI Pico WiFi.
In order to be able to build this code, 2 modifications have to be made in order to avoid "PANIC":
- The LWIP_NUM_SYS_TIMEOUT has to increased by 1 as mentioned in this thread: raspberrypi/pico-sdk#1281
-
The MQTT_OUTPUT_RINGBUF_SIZE has also to be increased, otherwise a memory execption will be triggered.
How the Code works:
The MQTT library takes care of the basic functionality for the mqtt to work (connect, publish, subscribe, etc.)
The IoT device library gives all the function that enable the user to define and update in a easy way the different entities for home assistant.
In order to configure the Device have first a look in the "IoT_device_confih.h", from here it is possible to configure all the entities details.
Then move to the "IoT_device.c", here two functions are defined in order to initialize and update the status of all the entities in home assistant.
The IoT_device_config_pub_init gives the flexibility to instanciate a generic entity, given the proper configuration function as parameter.
In this example 3 voltage sensors and one output switch are being configured.
In order to update the status of the entities, customize the "IoT_device_state_update_pub" function, by adding all the entities status by using the functions provided by the parson library
In your main function just call the "IoT_device_config_pub_init(mqtt->mqtt_client_inst, mqtt);" upon connection and the "IoT_device_config_pub_init(mqtt->mqtt_client_inst, mqtt);" every time there is need to update the status of the sensors.
The configuration of the MQTT broker are defined in the "mqttExample.h" file
When subscribed to a topic, the callback function "mqtt_incoming_data_cb" is called. Here, all the device commands handling done inside the function "IoT_device_cb"
In this example, the callback will listen to the command topic of the Output 1 entity, and will change its status according to the command
The device output in home assistant will be the following