1 /* MQTT over Secure 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 = "MQTTWSS_EXAMPLE";
33
34
35 #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1
36 static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----";
37 #else
38 extern const uint8_t mqtt_eclipseprojects_io_pem_start[] asm("_binary_mqtt_eclipseprojects_io_pem_start");
39 #endif
40 extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclipseprojects_io_pem_end");
41
mqtt_event_handler_cb(esp_mqtt_event_handle_t event)42 static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
43 {
44 esp_mqtt_client_handle_t client = event->client;
45 int msg_id;
46 // your_context_t *context = event->context;
47 switch (event->event_id) {
48 case MQTT_EVENT_CONNECTED:
49 ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
50 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
51 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
52
53 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
54 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
55
56 msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
57 ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
58 break;
59 case MQTT_EVENT_DISCONNECTED:
60 ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
61 break;
62
63 case MQTT_EVENT_SUBSCRIBED:
64 ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
65 msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
66 ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
67 break;
68 case MQTT_EVENT_UNSUBSCRIBED:
69 ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
70 break;
71 case MQTT_EVENT_PUBLISHED:
72 ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
73 break;
74 case MQTT_EVENT_DATA:
75 ESP_LOGI(TAG, "MQTT_EVENT_DATA");
76 printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
77 printf("DATA=%.*s\r\n", event->data_len, event->data);
78 break;
79 case MQTT_EVENT_ERROR:
80 ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
81 break;
82 default:
83 ESP_LOGI(TAG, "Other event id:%d", event->event_id);
84 break;
85 }
86 return ESP_OK;
87 }
88
mqtt_event_handler(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)89 static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
90 {
91 /* The argument passed to esp_mqtt_client_register_event can de accessed as handler_args*/
92 ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
93 mqtt_event_handler_cb(event_data);
94 }
95
mqtt_app_start(void)96 static void mqtt_app_start(void)
97 {
98 const esp_mqtt_client_config_t mqtt_cfg = {
99 .uri = CONFIG_BROKER_URI,
100 .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start,
101 };
102
103 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
104 esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
105 /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
106 esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
107
108 esp_mqtt_client_start(client);
109 }
110
app_main(void)111 void app_main(void)
112 {
113 ESP_LOGI(TAG, "[APP] Startup..");
114 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
115 ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
116
117 esp_log_level_set("*", ESP_LOG_INFO);
118 esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
119 esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
120 esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
121 esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
122 esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
123
124 ESP_ERROR_CHECK(nvs_flash_init());
125 ESP_ERROR_CHECK(esp_netif_init());
126 ESP_ERROR_CHECK(esp_event_loop_create_default());
127
128 /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
129 * Read "Establishing Wi-Fi or Ethernet Connection" section in
130 * examples/protocols/README.md for more information about this function.
131 */
132 ESP_ERROR_CHECK(example_connect());
133
134 mqtt_app_start();
135 }
136