├── README.md └── mqtt_tcp_pub_sub.c /README.md: -------------------------------------------------------------------------------- 1 | # MQTT-ESP-IDF 2 | 3 | ESP IDF MQTT Publish and Subscribe via Mosquitto broker for esp32 4 | -------------------------------------------------------------------------------- /mqtt_tcp_pub_sub.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "esp_wifi.h" 6 | #include "esp_system.h" 7 | #include "nvs_flash.h" 8 | #include "esp_event.h" 9 | #include "esp_netif.h" 10 | #include "my_data.h" 11 | 12 | #include "freertos/FreeRTOS.h" 13 | #include "freertos/task.h" 14 | #include "freertos/semphr.h" 15 | #include "freertos/queue.h" 16 | 17 | #include "lwip/sockets.h" 18 | #include "lwip/dns.h" 19 | #include "lwip/netdb.h" 20 | 21 | #include "esp_log.h" 22 | #include "mqtt_client.h" 23 | 24 | static const char *TAG = "MQTT_TCP"; 25 | 26 | static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) 27 | { 28 | switch (event_id) 29 | { 30 | case WIFI_EVENT_STA_START: 31 | printf("WiFi connecting ... \n"); 32 | break; 33 | case WIFI_EVENT_STA_CONNECTED: 34 | printf("WiFi connected ... \n"); 35 | break; 36 | case WIFI_EVENT_STA_DISCONNECTED: 37 | printf("WiFi lost connection ... \n"); 38 | break; 39 | case IP_EVENT_STA_GOT_IP: 40 | printf("WiFi got IP ... \n\n"); 41 | break; 42 | default: 43 | break; 44 | } 45 | } 46 | 47 | void wifi_connection() 48 | { 49 | // 1 - Wi-Fi/LwIP Init Phase 50 | esp_netif_init(); // TCP/IP initiation s1.1 51 | esp_event_loop_create_default(); // event loop s1.2 52 | esp_netif_create_default_wifi_sta(); // WiFi station s1.3 53 | wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT(); 54 | esp_wifi_init(&wifi_initiation); // s1.4 55 | // 2 - Wi-Fi Configuration Phase 56 | esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL); 57 | esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL); 58 | wifi_config_t wifi_configuration = { 59 | .sta = { 60 | .ssid = SSID, 61 | .password = PASS}}; 62 | esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration); 63 | // 3 - Wi-Fi Start Phase 64 | esp_wifi_start(); 65 | // 4- Wi-Fi Connect Phase 66 | esp_wifi_connect(); 67 | } 68 | 69 | static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) 70 | { 71 | esp_mqtt_client_handle_t client = event->client; 72 | switch (event->event_id) 73 | { 74 | case MQTT_EVENT_CONNECTED: 75 | ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); 76 | esp_mqtt_client_subscribe(client, "my_topic", 0); 77 | esp_mqtt_client_publish(client, "my_topic", "Hi to all from ESP32 .........", 0, 1, 0); 78 | break; 79 | case MQTT_EVENT_DISCONNECTED: 80 | ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); 81 | break; 82 | case MQTT_EVENT_SUBSCRIBED: 83 | ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->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("\nTOPIC=%.*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 | break; 99 | default: 100 | ESP_LOGI(TAG, "Other event id:%d", event->event_id); 101 | break; 102 | } 103 | return ESP_OK; 104 | } 105 | 106 | static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) 107 | { 108 | ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); 109 | mqtt_event_handler_cb(event_data); 110 | } 111 | 112 | static void mqtt_app_start(void) 113 | { 114 | esp_mqtt_client_config_t mqtt_cfg = { 115 | .uri = "mqtt://mqtt.eclipseprojects.io", 116 | }; 117 | esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); 118 | esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); 119 | esp_mqtt_client_start(client); 120 | } 121 | 122 | void app_main(void) 123 | { 124 | nvs_flash_init(); 125 | wifi_connection(); 126 | 127 | vTaskDelay(2000 / portTICK_PERIOD_MS); 128 | printf("WIFI was initiated ...........\n"); 129 | 130 | mqtt_app_start(); 131 | } --------------------------------------------------------------------------------