|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <stddef.h> |
| 4 | +#include <string.h> |
| 5 | +#include "esp_wifi.h" |
| 6 | +#include "esp_system.h" |
| 7 | +#include "nvs_flash.h" |
| 8 | +#include "esp_event_loop.h" |
| 9 | + |
| 10 | +#include "freertos/FreeRTOS.h" |
| 11 | +#include "freertos/task.h" |
| 12 | +#include "freertos/semphr.h" |
| 13 | +#include "freertos/queue.h" |
| 14 | +#include "freertos/event_groups.h" |
| 15 | + |
| 16 | +#include "lwip/sockets.h" |
| 17 | +#include "lwip/dns.h" |
| 18 | +#include "lwip/netdb.h" |
| 19 | + |
| 20 | +#include "esp_log.h" |
| 21 | +#include "mqtt_client.h" |
| 22 | + |
| 23 | +static const char *TAG = "MQTT_EXAMPLE"; |
| 24 | + |
| 25 | +static EventGroupHandle_t wifi_event_group; |
| 26 | +const static int CONNECTED_BIT = BIT0; |
| 27 | + |
| 28 | + |
| 29 | +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) |
| 30 | +{ |
| 31 | + esp_mqtt_client_handle_t client = event->client; |
| 32 | + int msg_id; |
| 33 | + // your_context_t *context = event->context; |
| 34 | + switch (event->event_id) { |
| 35 | + case MQTT_EVENT_CONNECTED: |
| 36 | + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
| 37 | + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); |
| 38 | + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
| 39 | + |
| 40 | + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); |
| 41 | + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); |
| 42 | + |
| 43 | + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); |
| 44 | + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); |
| 45 | + |
| 46 | + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); |
| 47 | + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); |
| 48 | + break; |
| 49 | + case MQTT_EVENT_DISCONNECTED: |
| 50 | + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); |
| 51 | + break; |
| 52 | + |
| 53 | + case MQTT_EVENT_SUBSCRIBED: |
| 54 | + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
| 55 | + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); |
| 56 | + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
| 57 | + break; |
| 58 | + case MQTT_EVENT_UNSUBSCRIBED: |
| 59 | + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
| 60 | + break; |
| 61 | + case MQTT_EVENT_PUBLISHED: |
| 62 | + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
| 63 | + break; |
| 64 | + case MQTT_EVENT_DATA: |
| 65 | + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); |
| 66 | + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); |
| 67 | + printf("DATA=%.*s\r\n", event->data_len, event->data); |
| 68 | + break; |
| 69 | + case MQTT_EVENT_ERROR: |
| 70 | + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
| 71 | + break; |
| 72 | + } |
| 73 | + return ESP_OK; |
| 74 | +} |
| 75 | + |
| 76 | +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) |
| 77 | +{ |
| 78 | + switch (event->event_id) { |
| 79 | + case SYSTEM_EVENT_STA_START: |
| 80 | + esp_wifi_connect(); |
| 81 | + break; |
| 82 | + case SYSTEM_EVENT_STA_GOT_IP: |
| 83 | + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); |
| 84 | + |
| 85 | + break; |
| 86 | + case SYSTEM_EVENT_STA_DISCONNECTED: |
| 87 | + esp_wifi_connect(); |
| 88 | + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); |
| 89 | + break; |
| 90 | + default: |
| 91 | + break; |
| 92 | + } |
| 93 | + return ESP_OK; |
| 94 | +} |
| 95 | + |
| 96 | +static void wifi_init(void) |
| 97 | +{ |
| 98 | + tcpip_adapter_init(); |
| 99 | + wifi_event_group = xEventGroupCreate(); |
| 100 | + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); |
| 101 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 102 | + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
| 103 | + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
| 104 | + wifi_config_t wifi_config = { |
| 105 | + .sta = { |
| 106 | + .ssid = CONFIG_WIFI_SSID, |
| 107 | + .password = CONFIG_WIFI_PASSWORD, |
| 108 | + }, |
| 109 | + }; |
| 110 | + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 111 | + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); |
| 112 | + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); |
| 113 | + ESP_ERROR_CHECK(esp_wifi_start()); |
| 114 | + ESP_LOGI(TAG, "Waiting for wifi"); |
| 115 | + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); |
| 116 | +} |
| 117 | + |
| 118 | +static void mqtt_app_start(void) |
| 119 | +{ |
| 120 | + esp_mqtt_client_config_t mqtt_cfg = { |
| 121 | + .uri = CONFIG_BROKER_URL, |
| 122 | + .event_handle = mqtt_event_handler, |
| 123 | + // .user_context = (void *)your_context |
| 124 | + }; |
| 125 | + |
| 126 | +#if CONFIG_BROKER_URL_FROM_STDIN |
| 127 | + char line[128]; |
| 128 | + |
| 129 | + if (strcmp(mqtt_cfg.uri, "FROM_STDIN") == 0) { |
| 130 | + int count = 0; |
| 131 | + printf("Please enter url of mqtt broker\n"); |
| 132 | + while (count < 128) { |
| 133 | + int c = fgetc(stdin); |
| 134 | + if (c == '\n') { |
| 135 | + line[count] = '\0'; |
| 136 | + break; |
| 137 | + } else if (c > 0 && c < 127) { |
| 138 | + line[count] = c; |
| 139 | + ++count; |
| 140 | + } |
| 141 | + vTaskDelay(10 / portTICK_PERIOD_MS); |
| 142 | + } |
| 143 | + mqtt_cfg.uri = line; |
| 144 | + printf("Broker url: %s\n", line); |
| 145 | + } else { |
| 146 | + ESP_LOGE(TAG, "Configuration mismatch: wrong broker url"); |
| 147 | + abort(); |
| 148 | + } |
| 149 | +#endif /* CONFIG_BROKER_URL_FROM_STDIN */ |
| 150 | + |
| 151 | + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); |
| 152 | + esp_mqtt_client_start(client); |
| 153 | +} |
| 154 | + |
| 155 | +void app_main() |
| 156 | +{ |
| 157 | + ESP_LOGI(TAG, "[APP] Startup.."); |
| 158 | + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); |
| 159 | + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); |
| 160 | + |
| 161 | + esp_log_level_set("*", ESP_LOG_INFO); |
| 162 | + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); |
| 163 | + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); |
| 164 | + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); |
| 165 | + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); |
| 166 | + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); |
| 167 | + |
| 168 | + nvs_flash_init(); |
| 169 | + wifi_init(); |
| 170 | + mqtt_app_start(); |
| 171 | +} |
0 commit comments