1 /* MQTT over Websockets 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 = "MQTTWS_EXAMPLE";
33
log_error_if_nonzero(const char * message,int error_code)34 static void log_error_if_nonzero(const char *message, int error_code)
35 {
36 if (error_code != 0) {
37 ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
38 }
39 }
40
41 /*
42 * @brief Event handler registered to receive MQTT events
43 *
44 * This function is called by the MQTT client event loop.
45 *
46 * @param handler_args user data registered to the event.
47 * @param base Event base for the handler(always MQTT Base in this example).
48 * @param event_id The id for the received event.
49 * @param event_data The data for the event, esp_mqtt_event_handle_t.
50 */
mqtt_event_handler(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)51 static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
52 {
53 ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
54 esp_mqtt_event_handle_t event = event_data;
55 esp_mqtt_client_handle_t client = event->client;
56 int msg_id;
57 switch ((esp_mqtt_event_id_t)event_id) {
58 case MQTT_EVENT_CONNECTED:
59 ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
60 msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
61 ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
62 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
63 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
64
65 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
66 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
67
68 msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
69 ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
70 break;
71 case MQTT_EVENT_DISCONNECTED:
72 ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
73 break;
74
75 case MQTT_EVENT_SUBSCRIBED:
76 ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
77 msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
78 ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
79 break;
80 case MQTT_EVENT_UNSUBSCRIBED:
81 ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
82 break;
83 case MQTT_EVENT_PUBLISHED:
84 ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
85 break;
86 case MQTT_EVENT_DATA:
87 ESP_LOGI(TAG, "MQTT_EVENT_DATA");
88 printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
89 printf("DATA=%.*s\r\n", event->data_len, event->data);
90 break;
91 case MQTT_EVENT_ERROR:
92 ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
93 if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
94 log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
95 log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
96 log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
97 ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
98
99 }
100 break;
101 default:
102 ESP_LOGI(TAG, "Other event id:%d", event->event_id);
103 break;
104 }
105 }
106
mqtt_app_start(void)107 static void mqtt_app_start(void)
108 {
109 const esp_mqtt_client_config_t mqtt_cfg = {
110 .uri = CONFIG_BROKER_URI,
111 };
112
113 esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
114 /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
115 esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
116 esp_mqtt_client_start(client);
117 }
118
app_main(void)119 void app_main(void)
120 {
121 ESP_LOGI(TAG, "[APP] Startup..");
122 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
123 ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
124
125 esp_log_level_set("*", ESP_LOG_INFO);
126 esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
127 esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
128 esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
129 esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE);
130 esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
131 esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
132
133 ESP_ERROR_CHECK(nvs_flash_init());
134 ESP_ERROR_CHECK(esp_netif_init());
135 ESP_ERROR_CHECK(esp_event_loop_create_default());
136
137 /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
138 * Read "Establishing Wi-Fi or Ethernet Connection" section in
139 * examples/protocols/README.md for more information about this function.
140 */
141 ESP_ERROR_CHECK(example_connect());
142
143 mqtt_app_start();
144 }
145