Difference between revisions of "Mqtt notes"
m (→^ Tau and AT, iDRX and cDRX parameters) |
m (Add link to MQTT Last Will and Testament.) |
||
(One intermediate revision by the same user not shown) | |||
Line 273: | Line 273: | ||
RRC mode: Idle | RRC mode: Idle | ||
</pre> | </pre> | ||
+ | |||
+ | |||
+ | == edit point == | ||
+ | |||
+ | 2021-12-21 Note: parameter `aws_iot_config *const config` can be as the value NULL: | ||
+ | |||
+ | <pre> | ||
+ | 989 static int connect_client(struct aws_iot_config *const config) | ||
+ | 990 { | ||
+ | 991 int err; | ||
+ | 992 | ||
+ | 993 err = client_broker_init(&client); | ||
+ | 994 if (err) { | ||
+ | 995 LOG_ERR("client_broker_init, error: %d", err); | ||
+ | 996 return err; | ||
+ | 997 } | ||
+ | 998 | ||
+ | 999 err = mqtt_connect(&client); | ||
+ | 1000 if (err) { | ||
+ | 1001 LOG_ERR("mqtt_connect, error: %d", err); | ||
+ | 1002 err = connect_error_translate(err); | ||
+ | 1003 return err; | ||
+ | 1004 } | ||
+ | 1005 | ||
+ | 1006 if (IS_ENABLED(CONFIG_AWS_IOT_SEND_TIMEOUT)) { | ||
+ | 1007 struct timeval timeout = { | ||
+ | 1008 .tv_sec = CONFIG_AWS_IOT_SEND_TIMEOUT_SEC | ||
+ | 1009 }; | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | and in file [west_workspace]/nrf/ext/curl/lib/mqtt.c . . . | ||
+ | |||
+ | <pre> | ||
+ | 140 static CURLcode mqtt_connect(struct connectdata *conn) | ||
+ | 141 { | ||
+ | 142 CURLcode result = CURLE_OK; | ||
+ | 143 const size_t client_id_offset = 14; | ||
+ | 144 const size_t packetlen = client_id_offset + MQTT_CLIENTID_LEN; | ||
+ | 145 char client_id[MQTT_CLIENTID_LEN + 1] = "curl"; | ||
+ | 146 const size_t clen = strlen("curl"); | ||
+ | 147 char packet[32] = { | ||
+ | 148 MQTT_MSG_CONNECT, /* packet type */ | ||
+ | 149 0x00, /* remaining length */ | ||
+ | 150 0x00, 0x04, /* protocol length */ | ||
+ | 151 'M','Q','T','T', /* protocol name */ | ||
+ | 152 0x04, /* protocol level */ | ||
+ | 153 0x02, /* CONNECT flag: CleanSession */ | ||
+ | 154 0x00, 0x3c, /* keep-alive 0 = disabled */ | ||
+ | 155 0x00, 0x00 /* payload1 length */ | ||
+ | 156 }; | ||
+ | 157 packet[1] = (packetlen - 2) & 0x7f; | ||
+ | 158 packet[client_id_offset - 1] = MQTT_CLIENTID_LEN; | ||
+ | 159 | ||
+ | 160 result = Curl_rand_hex(conn->data, (unsigned char *)&client_id[clen], | ||
+ | 161 MQTT_CLIENTID_LEN - clen + 1); | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | == MQTT LWT == | ||
+ | |||
+ | * https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/ |
Latest revision as of 22:04, 24 October 2024
Contents
Overview
Notes on MQTT
2021-12-20 - On devzone.nordicsemi.com creating public forum post with title "aws_iot MQTT subscriber keep alive", following suggested posts as solutions come up:
-
A possible Zephyr k_work_submit() solution:
^ Tau and AT, iDRX and cDRX parameters
In response to post on Nordic Devzone:
Searching for reference document "3GPP":
A little further along with references to 3GPP Specification 27.007, and definition of User Equipment (UE):
- https://infocenter.nordicsemi.com/pdf/nrf91_at_commands_v1.4.1.pdf
- https://portal.3gpp.org/ChangeRequests.aspx?q=1&versionId=75070&release=192
^ MQTT Persistent Sessions References
^ aws_iot Run-time Message Excerpts
Looks like in Nordic Semi's aws_iot sample app, the following enumerated events represent a firmware instance receiving subscribed messages from an MQTT broker:
- 1108 DEV - aws_iot event handler called, AWS_IOT_EVT_READY - 1108 DEV - aws_iot event handler called, AWS_IOT_EVT_DATA_RECEIVED
s3://iot-poc/hello/world/
Ah, trouble when attempting to add MQTT topics:
*** Booting Zephyr OS build v2.6.0-rc1-ncs1 *** I: Modem library is not yet initialized, AT commands not sent I: Configuration of MAGPIO and COEX0 is left to drivers The AWS IoT sample started, version: v1.0.0 ZZZ ZZZ ZZZ ZZZ - preparing to subscribe to hello/world topic, ZZZ ZZZ ZZZ - calling routine to subscribe to MQTT topics . . . ZZZ E: Application subscription list count mismatch aws_iot_subscription_topics_add, error: -122 ZZZ ZZZ ZZZ ZZZ Adding application specific topics failed, error: -122 Summary of callback pointers for 'set flag' events:
Appears to come from `./nrf/subsys/net/lib/aws_iot/src/aws_iot.c:1168: LOG_ERR("Application subscription list count mismatch");`
Note 3 - code excert from `nrf/subsys/net/lib/aws_iot/src/aws_iot.c`:
689 if (!mqtt_evt->param.connack.session_present_flag || 690 IS_ENABLED(CONFIG_MQTT_CLEAN_SESSION)) { 691 err = topic_subscribe(); 692 693 if (err < 0) { 694 aws_iot_evt.type = AWS_IOT_EVT_ERROR; 695 aws_iot_evt.data.err = err; 696 aws_iot_notify_event(&aws_iot_evt); 697 break; 698 } 699 if (err == 0) { 700 /* There were not topics to subscribe to. */ 701 aws_iot_evt.type = AWS_IOT_EVT_READY; 702 aws_iot_notify_event(&aws_iot_evt); 703 } /* else: wait for SUBACK */ 704 } else { 705 /* pre-existing session: 706 * subscription is already established. 707 */ 708 aws_iot_evt.type = AWS_IOT_EVT_READY; 709 aws_iot_notify_event(&aws_iot_evt); 710 711 if (IS_ENABLED(
951 #if defined(CONFIG_AWS_IOT_LAST_WILL) 952 static struct mqtt_topic last_will_topic = { 953 .topic.utf8 = CONFIG_AWS_IOT_LAST_WILL_TOPIC, 954 .topic.size = sizeof(CONFIG_AWS_IOT_LAST_WILL_TOPIC) - 1, 955 .qos = MQTT_QOS_0_AT_MOST_ONCE 956 }; 957 958 static struct mqtt_utf8 last_will_message = { 959 .utf8 = CONFIG_AWS_IOT_LAST_WILL_MESSAGE, 960 .size = sizeof(CONFIG_AWS_IOT_LAST_WILL_MESSAGE) - 1 961 }; 962 963 client->will_topic = &last_will_topic; 964 client->will_message = &last_will_message; 965 #endif 966 967 static sec_tag_t sec_tag_list[] = { CONFIG_AWS_IOT_SEC_TAG }; 968 struct mqtt_sec_config *tls_cfg = &(client->transport).tls.config; 969 970 tls_cfg->peer_verify = 2; 971 tls_cfg->cipher_count = 0; 972 tls_cfg->cipher_list = NULL; 973 tls_cfg->sec_tag_count = ARRAY_SIZE(sec_tag_list); 974 tls_cfg->sec_tag_list = sec_tag_list; 975 tls_cfg->hostname = CONFIG_AWS_IOT_BROKER_HOST_NAME; 976 tls_cfg->session_cache = TLS_SESSION_CACHE_DISABLED; 977 978 #if !defined(CONFIG_NRF_MODEM_LIB) 979 err = certificates_provision(); 980 if (err) { 981 LOG_ERR("Could not provision certificates, error: %d", err); 982 return err; 983 } 984 #endif /* !defined(CONFIG_NRF_MODEM_LIB) */ 985 986 return err; 987 }
z4-sandbox-kionix-work/nrf/subsys/net/lib/aws_iot/src$ grep -n 'struct mqtt_topic' ./*.* 451: const struct mqtt_topic aws_iot_rx_list[] = { 546: .list = (struct mqtt_topic *)&aws_iot_rx_list, 952: static struct mqtt_topic last_will_topic = {
edit point
./zephyr/include/net/mqtt.h:178:struct mqtt_topic { /** @brief Abstracts UTF-8 encoded strings. */ struct mqtt_utf8 { const uint8_t *utf8; /**< Pointer to UTF-8 string. */ uint32_t size; /**< Size of UTF string, in bytes. */ }; /** @brief Abstracts MQTT UTF-8 encoded topic that can be subscribed * to or published. */ struct mqtt_topic { /** Topic on to be published or subscribed to. */ struct mqtt_utf8 topic; /** Quality of service requested for the subscription. * @ref mqtt_qos for details. */ uint8_t qos; };
A link to review:
2021-12-14 Tue
- https://forums.aws.amazon.com/index.jspa
- https://aws.amazon.com/blogs/iot/getting-started-mqtt-retained-messages-aws-iot-core/
- https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html#mqtt-retain
2021-12-08
* https://github.com/aws/aws-iot-device-sdk-embedded-C/blob/main/demos/mqtt/mqtt_demo_basic_tls/mqtt_demo_basic_tls.c
+++
* https://docs.aws.amazon.com/iot/latest/developerguide/topics.html
Unsorted references to MQTT:
# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/samples/net/mqtt_publisher/README.html # https://www.hivemq.com/blog/mqtt-essentials-part2-publish-subscribe/ # https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/samples/index.html # https://www.circuito.io/blog/arduino-code/ # https://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages/ # https://www.hivemq.com/blog/mqtt-essentials-part-3-client-broker-connection-establishment/
# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.6.1/nrf/examples.html # https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.6.1/nrf/ug_tfm.html#ug-tfm # https://ci-builds.trustedfirmware.org/static-files/UxIoKYsDyPQ0ojvJOv9EX6-hZNDZd6IPGfcm05Bk1b0xNjM3MTA4NjMxODUxOjk6YW5vbnltb3VzOmpvYi90Zi1tLWJ1aWxkLWRvY3MtbmlnaHRseS9sYXN0U3RhYmxlQnVpbGQvYXJ0aWZhY3Q=/trusted-firmware-m/build/docs/user_guide/html/index.html
Nordic AWS FOTA example:
# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.6.1/nrf/samples/nrf9160/aws_fota/README.html
^ successful publish to hello/world topic
Created JSON message but not publishing to AWS IoT broker, - 1110 DEV - new file scoped flag for aws_iot holds 0, message is: { "version": "512", "deviceType": "PulseV2", "deviceSerialNumber": "352656101125105", "state": { "reported": { "ts": 1639641106680, "vrms": "0.406", "alert_vrms_threshhold_1": "no_alert", "latitude": "45.40494423760174", "longitude": "-122.5310215439456", "altitude": "0.0" } } } RRC mode: Connected - 1108 DEV - aws_iot event handler called, AWS_IOT_EVT_DATA_RECEIVED Data received from AWS IoT console: Topic: hello/world Message: { "version": "513", "note": "test 010", "KEYNAME001": "*** **** *** **** ***", "KEYNAME002": "*** **** *** **** ***", "KEYNAME003": "*** **** *** **** ***" } adm: power_time_domain=0.003, power_freq_domain=0.002, diff=0.001, ratio=1.604 - 1105 - calculated VRMS of 0.000000 - MARK 7 - at every 100 CLI loop iterations button press count at 0 adm: power_time_domain=0.002, power_freq_domain=0.001, diff=0.001, ratio=1.631 - 1105 - calculated VRMS of 0.798562 RRC mode: Idle
edit point
2021-12-21 Note: parameter `aws_iot_config *const config` can be as the value NULL:
989 static int connect_client(struct aws_iot_config *const config) 990 { 991 int err; 992 993 err = client_broker_init(&client); 994 if (err) { 995 LOG_ERR("client_broker_init, error: %d", err); 996 return err; 997 } 998 999 err = mqtt_connect(&client); 1000 if (err) { 1001 LOG_ERR("mqtt_connect, error: %d", err); 1002 err = connect_error_translate(err); 1003 return err; 1004 } 1005 1006 if (IS_ENABLED(CONFIG_AWS_IOT_SEND_TIMEOUT)) { 1007 struct timeval timeout = { 1008 .tv_sec = CONFIG_AWS_IOT_SEND_TIMEOUT_SEC 1009 };
and in file [west_workspace]/nrf/ext/curl/lib/mqtt.c . . .
140 static CURLcode mqtt_connect(struct connectdata *conn) 141 { 142 CURLcode result = CURLE_OK; 143 const size_t client_id_offset = 14; 144 const size_t packetlen = client_id_offset + MQTT_CLIENTID_LEN; 145 char client_id[MQTT_CLIENTID_LEN + 1] = "curl"; 146 const size_t clen = strlen("curl"); 147 char packet[32] = { 148 MQTT_MSG_CONNECT, /* packet type */ 149 0x00, /* remaining length */ 150 0x00, 0x04, /* protocol length */ 151 'M','Q','T','T', /* protocol name */ 152 0x04, /* protocol level */ 153 0x02, /* CONNECT flag: CleanSession */ 154 0x00, 0x3c, /* keep-alive 0 = disabled */ 155 0x00, 0x00 /* payload1 length */ 156 }; 157 packet[1] = (packetlen - 2) & 0x7f; 158 packet[client_id_offset - 1] = MQTT_CLIENTID_LEN; 159 160 result = Curl_rand_hex(conn->data, (unsigned char *)&client_id[clen], 161 MQTT_CLIENTID_LEN - clen + 1);