1 /* MQTT Mutual Authentication Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stddef.h>
12 #include <string.h>
13 #include "esp_wifi.h"
14 #include "esp_system.h"
15 #include "nvs_flash.h"
16 #include "esp_event.h"
17 #include "esp_netif.h"
18 #include "protocol_examples_common.h"
19
20 #include "freertos/FreeRTOS.h"
21 #include "freertos/task.h"
22 #include "freertos/semphr.h"
23 #include "freertos/queue.h"
24
25 #include "lwip/sockets.h"
26 #include "lwip/dns.h"
27 #include "lwip/netdb.h"
28
29 #include "esp_log.h"
30 #include "mqtt_client.h"
31
32 static const char *TAG = "MQTTS_EXAMPLE";
33
34 extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start");
35 extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end");
36 extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start");
37 extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end");
38 extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start");
39 extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end");
40
log_error_if_nonzero(const char * message,int error_code)41 static void log_error_if_nonzero(const char *message, int error_code)
42 {
43 if (error_code != 0) {
44 ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
45 }
46 }
47
48 /*
49 * @brief Event handler registered to receive MQTT events
50 *
51 * This function is called by the MQTT client event loop.
52 *
53 * @param handler_args user data registered to the event.
54 * @param base Event base for the handler(always MQTT Base in this example).
55 * @param event_id The id for the received event.
56 * @param event_data The data for the event, esp_mqtt_event_handle_t.
57 */
mqtt_event_handler(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)58 static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
59 {
60 ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
61 esp_mqtt_event_handle_t event = event_data;
62 esp_mqtt_client_handle_t client = event->client;
63 int msg_id;
64 switch ((esp_mqtt_event_id_t)event_id) {
65 case MQTT_EVENT_CONNECTED:
66 ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
67 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
68 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
69
70 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
71 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
72
73 msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
74 ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
75 break;
76 case MQTT_EVENT_DISCONNECTED:
77 ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
78 break;
79
80 case MQTT_EVENT_SUBSCRIBED:
81 ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
82 msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
83 ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
84 break;
85 case MQTT_EVENT_UNSUBSCRIBED:
86 ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
87 break;
88 case MQTT_EVENT_PUBLISHED:
89 ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
90 break;
91 case MQTT_EVENT_DATA:
92 ESP_LOGI(TAG, "MQTT_EVENT_DATA");
93 printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
94 printf("DATA=%.*s\r\n", event->data_len, event->data);
95 break;
96 case MQTT_EVENT_ERROR:
97 ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
98 if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
99 log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
100 log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
101 log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
102 ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
103
104 }
105 break;
106 default:
107 ESP_LOGI(TAG, "Other event id:%d", event->event_id);
108 break;
109 }
110 }
111
mqtt_app_start(void)112 static void mqtt_app_start(void)
113 {
114 const esp_mqtt_client_config_t mqtt_cfg = {
115 .uri = "mqtts://test.mosquitto.org:8884",
116 .client_cert_pem = (const char *)client_cert_pem_start,
117 .client_key_pem = (const char *)client_key_pem_start,
118 .cert_pem = (const char *)server_cert_pem_start,
119 };
120
121 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
122 esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
123 /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
124 esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
125 esp_mqtt_client_start(client);
126 }
127
app_main(void)128 void app_main(void)
129 {
130 ESP_LOGI(TAG, "[APP] Startup..");
131 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
132 ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
133
134 esp_log_level_set("*", ESP_LOG_INFO);
135 esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
136 esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
137 esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
138 esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
139
140 ESP_ERROR_CHECK(nvs_flash_init());
141 ESP_ERROR_CHECK(esp_netif_init());
142 ESP_ERROR_CHECK(esp_event_loop_create_default());
143
144 /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
145 * Read "Establishing Wi-Fi or Ethernet Connection" section in
146 * examples/protocols/README.md for more information about this function.
147 */
148 ESP_ERROR_CHECK(example_connect());
149
150 mqtt_app_start();
151 }
152