This post was last edited by dirty on 2023-12-15 00:58
This article implements nRF7002-DK MQTT publishing and subscription and realizes lighting control.
1. Understanding MQTT
MQTT is a client-server architecture publish/subscribe messaging protocol, which is widely used in data exchange between IoT cloud and device. MQTT uses publish/subscribe messaging mode, a combination of Topic and Playload, and has three message publishing service qualities QoS. The following figure shows the MQTT network structure.
Figure 1: MQTT network structure
2. Engineering Code
Refer to the official ncs\v2.5.0\nrf\samples\net\mqtt project code here, configure it and make appropriate modifications.
1. Add the wifi account and password in pri.conf to connect to the server.
CONFIG_WIFI_CREDENTIALS_STATIC_SSID="XXX"
CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD="XXX"
CONFIG_WIFI_CREDENTIALS_STATIC_TYPE_PSK=y
2. Add the code to control the LED light. Since the network connection status uses LED2, here the MQTT subscription control uses LED1. Add the code as follows.
src\modules\led\led.c
#define LED0 0
void led0_on(void)
{
int err = 0;
err = led_on(led_device, LED0);
if (err) {
LOG_ERR("led_on, error: %d", err);
}
}
void led0_off(void)
{
int err = 0;
err = led_off(led_device, LED0);
if (err) {
LOG_ERR("led_off, error: %d", err);
}
}
3. Retrieve keywords in the MQTT subscription, here are "ON", "OFF", and control LED1 on and off respectively. The code is as follows
src\modules\transport\transport.c
static void on_mqtt_publish(struct mqtt_helper_buf topic, struct mqtt_helper_buf payload)
{
LOG_INF("Received payload: %.*s on topic: %.*s", payload.size,
payload.ptr,
topic.size,
topic.ptr);
//检索控制LED1
if(strcmp(payload.ptr,"ON")==0)
{
LOG_INF("Light on LED0 \r\n");
led0_on();
}
else if(strcmp(payload.ptr,"OFF")==0)
{
LOG_INF("Light off LED0 \r\n");
led0_off();
}
}
4.Configure, compile and burn
Compile configuration, TLS encryption configuration is used here, step 1 needs to be configured, otherwise the compilation will fail. The compilation configuration is shown in the figure below, and it will be burned after the compilation is completed.
Figure 2: Compile configuration
5. Open the serial port and press the reset button. You can see that the development board is connected to the server test.mosquitto.org and publishes messages at regular intervals. You can also actively trigger the release by pressing Button1. The following figure is the MQTT connection and message publishing log. We can see the MQTT host, Client ID, Port, and publish and subscribe topics. It is useful for the subsequent MQTT proxy tool configuration.
Figure 3: MQTT connection and message publishing
3.MQTT publish-subscribe test and light control
1. MQTT proxy tool preparation and configuration connection
Here we use the MQTT.fx tool and the configuration is as shown below
Figure 4: MQTT tool configuration
2. Tool connection and subscription device-side message publishing
After the connection is completed, a green dot will appear on the right side of the tool. Fill in the topic in the subscription tab and subscribe. Press button1 on the device to actively publish and subscribe, and subscribe to receive the published message on the MQTT side. As shown in the figure below
Figure 5: MQTT connection and message subscription
3. MQTT message publishing, light control
In the MQTT tool publishing tab, fill in the publishing topic, write the message, click publish, and observe that the device receives the subscription message and LED1 turns on/off according to the message. As shown in the figure below
Figure 6: MQTT publishes a message and the device subscribes
LED1 on the development board is controlled accordingly to the subscribed messages.
Figure 7: Device MQTT subscription LED1 lighting effect
4. Summary
Nordic SDK MQTT has relatively complete function integration. By mastering its use through practice, you can quickly and easily implement customized requirements according to application needs. It is very easy to use and useful in interactive communication with the cloud.