Skip to content

Commit 3031fa0

Browse files
committed
mqtt-azure
0 parents  commit 3031fa0

File tree

14 files changed

+3243
-0
lines changed

14 files changed

+3243
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"idf.adapterTargetName": "esp32",
3+
"files.associations": {
4+
"wifi.h": "c"
5+
},
6+
"idf.flashType": "UART",
7+
"idf.portWin": "COM3"
8+
}

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(hello_world)

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := hello_world
7+
8+
include $(IDF_PATH)/make/project.mk

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Hello World Example
2+
3+
Starts a FreeRTOS task to print "Hello World".
4+
5+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
6+
7+
## How to use example
8+
9+
Follow detailed instructions provided specifically for this example.
10+
11+
Select the instructions depending on Espressif chip installed on your development board:
12+
13+
- [ESP32 Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/stable/get-started/index.html)
14+
- [ESP32-S2 Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/get-started/index.html)
15+
16+
17+
## Example folder contents
18+
19+
The project **hello_world** contains one source file in C language [hello_world_main.c](main/hello_world_main.c). The file is located in folder [main](main).
20+
21+
ESP-IDF projects are built using CMake. The project build configuration is contained in `CMakeLists.txt` files that provide set of directives and instructions describing the project's source files and targets (executable, library, or both).
22+
23+
Below is short explanation of remaining files in the project folder.
24+
25+
```
26+
├── CMakeLists.txt
27+
├── example_test.py Python script used for automated example testing
28+
├── main
29+
│   ├── CMakeLists.txt
30+
│   ├── component.mk Component make file
31+
│   └── hello_world_main.c
32+
├── Makefile Makefile used by legacy GNU Make
33+
└── README.md This is the file you are currently reading
34+
```
35+
36+
For more information on structure and contents of ESP-IDF projects, please refer to Section [Build System](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html) of the ESP-IDF Programming Guide.
37+
38+
## Troubleshooting
39+
40+
* Program upload failure
41+
42+
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
43+
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
44+
45+
## Technical support and feedback
46+
47+
Please use the following feedback channels:
48+
49+
* For technical queries, go to the [esp32.com](https://esp32.com/) forum
50+
* For a feature request or bug report, create a [GitHub issue](https://github.com/espressif/esp-idf/issues)
51+
52+
We will get back to you as soon as possible.

example_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import division, print_function, unicode_literals
4+
5+
import ttfw_idf
6+
7+
8+
@ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32s2', 'esp32c3'], ci_target=['esp32'])
9+
def test_examples_hello_world(env, extra_data):
10+
app_name = 'hello_world'
11+
dut = env.get_dut(app_name, 'examples/get-started/hello_world')
12+
dut.start_app()
13+
res = dut.expect(ttfw_idf.MINIMUM_FREE_HEAP_SIZE_RE)
14+
if not res:
15+
raise ValueError('Maximum heap size info not found')
16+
ttfw_idf.print_heap_size(app_name, dut.app.config_name, dut.TARGET, res[0])
17+
18+
19+
if __name__ == '__main__':
20+
test_examples_hello_world()

main/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/main/*.*)
2+
3+
idf_component_register(SRCS ${app_sources}
4+
INCLUDE_DIRS ""
5+
PRIV_REQUIRES nvs_flash mqtt
6+
)
7+

main/component.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#
2+
# "main" pseudo-component makefile.
3+
#
4+
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

main/hello_world_main.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)