199 lines
6.4 KiB
C++
199 lines
6.4 KiB
C++
/// © MiroZ 2024
|
|
|
|
#ifndef __MQTT_T__
|
|
#define __MQTT_T__
|
|
|
|
#include <stdint.h>
|
|
#include <esp_log.h>
|
|
#include <WiFiClientSecure.h>
|
|
#include <PubSubClient.h>
|
|
#include <functional>
|
|
|
|
#include "mqtt_client.h"
|
|
|
|
static const char *TAG = "MQTTS";
|
|
|
|
extern const uint8_t server_cert[] asm("_binary_eventgrid_azure_pem_start");
|
|
extern const uint8_t client_cert[] asm("_binary_client1_authn_ID_pem_start");
|
|
extern const uint8_t client_key[] asm("_binary_client1_authn_ID_key_start");
|
|
|
|
extern const uint8_t test_cert[] asm("_binary_mosquitto_org_crt_start");
|
|
extern const uint8_t test_client_cert[] asm("_binary_client_crt_start");
|
|
extern const uint8_t test_client_key[] asm("_binary_client_key_start");
|
|
|
|
esp_mqtt_client_handle_t client;
|
|
|
|
using namespace std::placeholders;
|
|
|
|
class Mqtt
|
|
{
|
|
#if 1
|
|
public:
|
|
|
|
static void log_error_if_nonzero(const char *message, int error_code)
|
|
{
|
|
if (error_code != 0) {
|
|
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
|
|
}
|
|
}
|
|
|
|
|
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
|
{
|
|
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
|
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
|
|
esp_mqtt_client_handle_t client = event->client;
|
|
int msg_id;
|
|
switch ((esp_mqtt_event_id_t)event_id) {
|
|
case MQTT_EVENT_CONNECTED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
|
// msg_id = esp_mqtt_client_subscribe(client, "wellnuotopics/topic1", 0);
|
|
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
|
|
|
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
|
|
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
|
|
|
// msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
|
|
// ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
|
|
break;
|
|
case MQTT_EVENT_DISCONNECTED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
|
|
break;
|
|
|
|
case MQTT_EVENT_SUBSCRIBED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
|
msg_id = esp_mqtt_client_publish(client, "wellnuotopics/topic1", "0", 0, 0, 0);
|
|
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
|
break;
|
|
case MQTT_EVENT_UNSUBSCRIBED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
|
break;
|
|
case MQTT_EVENT_PUBLISHED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
|
break;
|
|
case MQTT_EVENT_DATA:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
|
|
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
|
|
printf("DATA=%.*s\r\n", event->data_len, event->data);
|
|
break;
|
|
case MQTT_EVENT_ERROR:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
|
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
|
|
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
|
|
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
|
|
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
|
|
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
|
|
|
|
}
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
static void mqtt_app_start(void)
|
|
{
|
|
esp_mqtt_client_config_t mqtt_cfg;
|
|
memset(&mqtt_cfg, 0, sizeof(mqtt_cfg));
|
|
|
|
// test server
|
|
// mqtt_cfg.uri = "mqtts://test.mosquitto.org:8884";
|
|
// mqtt_cfg.cert_pem = (const char *)test_cert;
|
|
// mqtt_cfg.client_key_pem = (const char *)test_client_key;
|
|
// mqtt_cfg.client_cert_pem = (const char *)test_client_cert;
|
|
mqtt_cfg.uri = "mqtts://mqtt-dev-server.westus2-1.ts.eventgrid.azure.net:8883";
|
|
mqtt_cfg.username = "client1-authn-ID";
|
|
mqtt_cfg.client_id = "client1-esp";
|
|
mqtt_cfg.cert_pem = (const char *)server_cert;
|
|
mqtt_cfg.client_key_pem = (const char *)client_key;
|
|
mqtt_cfg.client_cert_pem = (const char *)client_cert;
|
|
|
|
|
|
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
|
client = esp_mqtt_client_init(&mqtt_cfg);
|
|
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
|
|
esp_mqtt_client_register_event(client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
|
esp_mqtt_client_start(client);
|
|
}
|
|
|
|
static void test()
|
|
{
|
|
mqtt_app_start();
|
|
|
|
while(true)
|
|
{
|
|
delay(5000);
|
|
int val = esp_mqtt_client_publish (client, "wellnuotopics/topic1", "mytestdata", 10, 0, false);
|
|
ESP_LOGI(TAG, "publish: %d", val);
|
|
}
|
|
}
|
|
#endif
|
|
public:
|
|
#if 0
|
|
void callback(char* topic, byte* payload, unsigned int length) {
|
|
printf("Message arrived in topic: %s", topic);
|
|
printf("Message:\n");
|
|
for (int i = 0; i < length; i++) {
|
|
printf("%c", (char)payload[i]);
|
|
}
|
|
printf("\n-----------------------\n");
|
|
}
|
|
|
|
void test()
|
|
{
|
|
const char* mqtt_broker = "91.121.93.94";//"40.64.128.45"; // "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net";
|
|
const char* topic = "test";
|
|
const int mqtt_port = 8884 ;
|
|
|
|
// Set ssl certificate
|
|
const char* root_ca = (const char*)mosq_bin;// ca_bin;
|
|
const char* client_cert = (const char *)cli_cert_bin;//cert_bin;
|
|
const char* client_key = (const char *)cli_key_bin;//key_bin;
|
|
|
|
printf(root_ca);
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
WiFiClientSecure espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
//printf("'%s'", root_ca);
|
|
|
|
espClient.setCACert(root_ca);
|
|
espClient.setCertificate(client_cert); // for client verification
|
|
espClient.setPrivateKey(client_key); // for client verification
|
|
|
|
|
|
client.setServer(mqtt_broker, mqtt_port);
|
|
client.setCallback(std::bind(&Mqtt::callback, this, _1, _2, _3));
|
|
|
|
while (!client.connected())
|
|
{
|
|
String client_id = "esp32-client";
|
|
client_id += String(WiFi.macAddress());
|
|
// Print the Name and the id of the esp32 controller
|
|
ESP_LOGI("", "The client %s connects to the public mqtt broker\n", client_id.c_str());
|
|
if (client.connect( client_id.c_str())) {
|
|
ESP_LOGI("", "Public emqx mqtt broker connected\n");
|
|
} else {
|
|
ESP_LOGI("", "failed with state %d", client.state());
|
|
delay(200000);
|
|
}
|
|
}
|
|
// Publish and Subscribe
|
|
client.publish(topic, "Hi I'm ESP32 ^^");
|
|
client.subscribe(topic);
|
|
|
|
while(true)
|
|
{
|
|
client.loop();
|
|
delay(1000);
|
|
}
|
|
|
|
}
|
|
#endif
|
|
};
|
|
|
|
#endif |