|
| 1 | +/* Hello World 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 "sdkconfig.h" |
| 11 | +#include "freertos/FreeRTOS.h" |
| 12 | +#include "freertos/task.h" |
| 13 | +#include "esp_system.h" |
| 14 | + |
| 15 | +#include "wifi.h" |
| 16 | +#include <nvs_flash.h> |
| 17 | +#include "mqtt_client.h" |
| 18 | +#include <esp_log.h> |
| 19 | + |
| 20 | +static const char *TAGM = "MQTT_EXAMPLE"; |
| 21 | + |
| 22 | +/** @brief HUB IOT connection string */ |
| 23 | +#define IOTHUB_NAME "iiot-manutencao-preditiva" |
| 24 | +#define IOTHUB_DEVID "mqtt-device-01" |
| 25 | +#define IOTHUB_KEY "SharedAccessSignature sr=iiot-manutencao-preditiva.azure-devices.net%2Fdevices%2Fmqtt-device-01&sig=tmSjkW48mwkiiz3IJ0YNcaGvzT9BtnT3vTkrbUhrkr8%3D&se=1674592121" |
| 26 | + |
| 27 | +esp_mqtt_client_handle_t client; |
| 28 | + |
| 29 | + |
| 30 | +static void log_error_if_nonzero(const char * message, int error_code) |
| 31 | +{ |
| 32 | + if (error_code != 0) { |
| 33 | + ESP_LOGE(TAGM, "Last error %s: 0x%x", message, error_code); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) |
| 38 | +{ |
| 39 | + esp_log_level_set("*", ESP_LOG_INFO); |
| 40 | + esp_mqtt_client_handle_t client = event->client; |
| 41 | + int msg_id; |
| 42 | + // your_context_t *context = event->context; |
| 43 | + switch (event->event_id) { |
| 44 | + case MQTT_EVENT_CONNECTED: |
| 45 | + ESP_LOGI(TAGM, "MQTT_EVENT_CONNECTED"); |
| 46 | + msg_id = esp_mqtt_client_publish(client, "devices/" IOTHUB_DEVID "/messages/events/", "data_3", 0, 1, 1); |
| 47 | + // ESP_LOGI(TAGM, "sent publish successful, msg_id=%d", msg_id); |
| 48 | + |
| 49 | + break; |
| 50 | + case MQTT_EVENT_DISCONNECTED: |
| 51 | + ESP_LOGI(TAGM, "MQTT_EVENT_DISCONNECTED"); |
| 52 | + break; |
| 53 | + |
| 54 | + case MQTT_EVENT_SUBSCRIBED: |
| 55 | + // ESP_LOGI(TAGM, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
| 56 | + // msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); |
| 57 | + // ESP_LOGI(TAGM, "sent publish successful, msg_id=%d", msg_id); |
| 58 | + break; |
| 59 | + case MQTT_EVENT_UNSUBSCRIBED: |
| 60 | + ESP_LOGI(TAGM, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
| 61 | + break; |
| 62 | + case MQTT_EVENT_PUBLISHED: |
| 63 | + ESP_LOGI(TAGM, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
| 64 | + break; |
| 65 | + case MQTT_EVENT_DATA: |
| 66 | + ESP_LOGI(TAGM, "MQTT_EVENT_DATA"); |
| 67 | + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); |
| 68 | + printf("DATA=%.*s\r\n", event->data_len, event->data); |
| 69 | + break; |
| 70 | + case MQTT_EVENT_ERROR: |
| 71 | + ESP_LOGI(TAGM, "MQTT_EVENT_ERROR"); |
| 72 | + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { |
| 73 | + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); |
| 74 | + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); |
| 75 | + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); |
| 76 | + ESP_LOGI(TAGM, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); |
| 77 | + |
| 78 | + } |
| 79 | + break; |
| 80 | + default: |
| 81 | + ESP_LOGI(TAGM, "Other event id:%d", event->event_id); |
| 82 | + |
| 83 | + |
| 84 | + break; |
| 85 | + } |
| 86 | + return ESP_OK; |
| 87 | +} |
| 88 | + |
| 89 | +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { |
| 90 | + ESP_LOGD(TAGM, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); |
| 91 | + mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data); |
| 92 | +} |
| 93 | + |
| 94 | +static void mqtt_app_start(void) |
| 95 | +{ |
| 96 | + ESP_LOGD(TAGM, "inicializando cliente mqtt"); |
| 97 | + const char* username = IOTHUB_NAME".azure-devices.net/"IOTHUB_DEVID"/?api-version=2021-04-12"; |
| 98 | + ESP_LOGD(TAGM, "mqtt username: %s", username); |
| 99 | + esp_mqtt_client_config_t mqtt_cfg = { |
| 100 | + .uri = "mqtt://iiot-manutencao-preditiva.azure-devices.net", |
| 101 | + .port=8883, |
| 102 | + .username = username, |
| 103 | + .client_id = IOTHUB_DEVID, |
| 104 | + .password = IOTHUB_KEY, |
| 105 | + .keepalive = 120, |
| 106 | + .use_secure_element = true, |
| 107 | + |
| 108 | + }; |
| 109 | + |
| 110 | + client = esp_mqtt_client_init(&mqtt_cfg); |
| 111 | + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); |
| 112 | + esp_mqtt_client_start(client); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +void app_main(void) |
| 117 | +{ |
| 118 | + esp_err_t ret = nvs_flash_init(); |
| 119 | + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 120 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 121 | + ret = nvs_flash_init(); |
| 122 | + ESP_LOGE("main", "-> NVS ERASED"); |
| 123 | + } |
| 124 | + ESP_ERROR_CHECK(ret); |
| 125 | + |
| 126 | + // wifi_connection_t conn = {"TP-Link", "kwe12345" }; |
| 127 | + wifi_connection_t conn = {"brisa-2816643", "9oqgl1e0" }; |
| 128 | + |
| 129 | + wifi_register_connection(conn); |
| 130 | + |
| 131 | + wifi_init_sta(); |
| 132 | + mqtt_app_start(); |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +} |
0 commit comments