| 2 |
raymond |
1 |
#include <MQTTRemote.h>
|
|
|
2 |
#include <driver/gpio.h>
|
|
|
3 |
#include <esp_log.h>
|
|
|
4 |
#include <freertos/FreeRTOS.h>
|
|
|
5 |
#include <freertos/task.h>
|
|
|
6 |
|
|
|
7 |
#define TAG "example"
|
|
|
8 |
|
|
|
9 |
const char mqtt_client_id[] = "example";
|
|
|
10 |
const char mqtt_host[] = "mqtts://test.mosquitto.org";
|
|
|
11 |
int mqtt_port = 8886; // mqtts using letsencrypt, see more at test.mosquitto.org
|
|
|
12 |
const char mqtt_username[] = "";
|
|
|
13 |
const char mqtt_password[] = "";
|
|
|
14 |
|
|
|
15 |
#define PIN_LED GPIO_NUM_14
|
|
|
16 |
|
|
|
17 |
extern const uint8_t isrgrootx1_pem_start[] asm("_binary_isrgrootx1_pem_start");
|
|
|
18 |
extern const uint8_t isrgrootx1_pem_end[] asm("_binary_isrgrootx1_pem_end");
|
|
|
19 |
const size_t certificate_len = isrgrootx1_pem_end - isrgrootx1_pem_start;
|
|
|
20 |
|
|
|
21 |
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
|
|
|
22 |
esp_mqtt_client_config_t::broker_t::verification_t verification = {
|
|
|
23 |
.use_global_ca_store = false,
|
|
|
24 |
.crt_bundle_attach = nullptr,
|
|
|
25 |
.certificate = (const char *)isrgrootx1_pem_start,
|
|
|
26 |
.certificate_len = certificate_len,
|
|
|
27 |
.psk_hint_key = nullptr,
|
|
|
28 |
.skip_cert_common_name_check = false,
|
|
|
29 |
.alpn_protos = nullptr,
|
|
|
30 |
.common_name = nullptr,
|
|
|
31 |
.ciphersuites_list = nullptr,
|
|
|
32 |
};
|
|
|
33 |
#else
|
|
|
34 |
esp_mqtt_client_config_t::broker_t::verification_t verification = {
|
|
|
35 |
.certificate = (const char *)isrgrootx1_pem_start,
|
|
|
36 |
.certificate_len = certificate_len,
|
|
|
37 |
};
|
|
|
38 |
#endif
|
|
|
39 |
|
|
|
40 |
MQTTRemote::Configuration configuration = {
|
|
|
41 |
.rx_buffer_size = 2048, .tx_buffer_size = 2048, .keep_alive_s = 10, .verification = verification};
|
|
|
42 |
MQTTRemote _mqtt_remote(mqtt_client_id, mqtt_host, mqtt_port, mqtt_username, mqtt_password, configuration);
|
|
|
43 |
|
|
|
44 |
void blinkAndSerialTask(void *pvParameters) {
|
|
|
45 |
bool swap = false;
|
|
|
46 |
while (1) {
|
|
|
47 |
gpio_set_level(PIN_LED, swap);
|
|
|
48 |
swap = !swap;
|
|
|
49 |
ESP_LOGI(TAG, "Hello");
|
|
|
50 |
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
void mqttMessageTask(void *pvParameters) {
|
|
|
55 |
bool retain = false;
|
|
|
56 |
uint8_t qos = 0;
|
|
|
57 |
while (1) {
|
|
|
58 |
_mqtt_remote.publishMessageVerbose(_mqtt_remote.clientId() + "/hello", "world", retain, qos);
|
|
|
59 |
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
extern "C" {
|
|
|
64 |
void app_main();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
void app_main(void) {
|
|
|
68 |
// Setup led and blinking led task
|
|
|
69 |
gpio_set_direction(PIN_LED, GPIO_MODE_OUTPUT);
|
|
|
70 |
gpio_set_level(PIN_LED, 1);
|
|
|
71 |
xTaskCreate(blinkAndSerialTask, "blinkAndSerialTask", 2048, NULL, 15, NULL);
|
|
|
72 |
|
|
|
73 |
// TODO (you): You need to connect to WiFi here first.
|
|
|
74 |
// For a simple one line utility, see https://github.com/johboh/ConnectionHelper
|
|
|
75 |
// Once connected to wifi, continue with below.
|
|
|
76 |
|
|
|
77 |
// Connect to WIFI
|
|
|
78 |
auto connected = true; // TODO (you): You need to connect to WiFi here first.
|
|
|
79 |
if (connected) {
|
|
|
80 |
// Connected to WIFI.
|
|
|
81 |
|
|
|
82 |
// Subscribe to to the /set topic under our client ID.
|
|
|
83 |
_mqtt_remote.subscribe(_mqtt_remote.clientId() + "/set", [](const std::string &topic, const std::string &message) {
|
|
|
84 |
ESP_LOGI(TAG, "Topic: %s, Message: %s", topic.c_str(), message.c_str());
|
|
|
85 |
});
|
|
|
86 |
|
|
|
87 |
// Start MQTT
|
|
|
88 |
_mqtt_remote.start([](bool connected) {
|
|
|
89 |
_mqtt_remote.publishMessageVerbose(_mqtt_remote.clientId() + "/initial_message", "oh hello!");
|
|
|
90 |
});
|
|
|
91 |
|
|
|
92 |
// Start task for periodically publishing messages.
|
|
|
93 |
xTaskCreate(mqttMessageTask, "mqttMessageTask", 2048, NULL, 15, NULL);
|
|
|
94 |
|
|
|
95 |
} else {
|
|
|
96 |
ESP_LOGE(TAG, "Failed to connect");
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Run forever.
|
|
|
100 |
while (1) {
|
|
|
101 |
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
102 |
fflush(stdout);
|
|
|
103 |
}
|
|
|
104 |
}
|