2024-05-22 11:54:43 -07:00

227 lines
7.0 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 0
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 1
void callback(char* topic, byte* payload, unsigned int length) {
printf("Message arrived in topic: %s\n", topic);
printf("Message:'");
for (int i = 0; i < length; i++) {
printf("%c", (char)payload[i]);
}
printf("'\n");
printf("-----------------------\n");
}
#include "time.h"
void printLocalTime()
{
struct tm timeinfo;
char buf[48];
if(!getLocalTime(&timeinfo)){
printf("Failed to obtain time\n");
return;
}
strftime(buf, 26, "%Y-%m-%d %H:%M:%S", &timeinfo);
ESP_LOGI(TAG, "%s", buf);
}
void test()
{
const char* mqtt_broker = "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net";
const char* topic = "wellnuotopics/topic1";
const int mqtt_port = 8883 ;
WiFiClientSecure espClient;
PubSubClient client(espClient);
espClient.setCACert((const char*)server_cert);
espClient.setCertificate((const char *)client_cert); // for client verification
espClient.setPrivateKey((const char *)client_key); // for client verification
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(std::bind(&Mqtt::callback, this, _1, _2, _3));
printLocalTime();
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("", "connecting client %s mqtt broker...", client_id.c_str());
if (client.connect("Esp32 client", "client1-authn-ID", NULL)) {
ESP_LOGI("", "Public emqx mqtt broker connected");
} else {
ESP_LOGE("", "failed with state %d", client.state());
delay(5000);
}
}
bool val;
// val = client.subscribe(topic);
// if(val)
// ESP_LOGI(TAG, "sub ok");
// else
// ESP_LOGI(TAG, "sub failed");
// Publish and Subscribe
// val = client.publish(topic, "Hi I'm ESP32 ^^");
// if(val)
// ESP_LOGI(TAG, "publish ok");
// else
// ESP_LOGI(TAG, "publish failed");
int n = 0;
while(true)
{
// client.loop();
delay(2500);
val = client.publish(topic, "Hi I'm ESP32 ^^");
if(val)
ESP_LOGI(TAG, "%d publish ok", n);
else
ESP_LOGE(TAG, "%d publish failed", n);
n++;
}
}
#endif
};
#endif