diff --git a/platformio.ini b/platformio.ini index dfcaf32..9bd14ea 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,6 +10,7 @@ [env:lolin_d32_pro] platform = espressif32 +;platform = https://github.com/platformio/platform-espressif32.git#feature/stage framework = arduino board = lolin_d32_pro @@ -22,6 +23,9 @@ build_flags = -DWIFI_PASSWD="\"${sysenv.WIFI_PASSWD}\"" -DWIFI_SSID2="\"${sysenv.WIFI_SSID}\"" -DWIFI_PASSWD2="\"${sysenv.WIFI_PASSWD}\"" + -DMQTT_BROKER_URI="\"${sysenv.MQTT_BROKER_URI}\"" + -DMQTT_USERNAME="\"${sysenv.MQTT_USERNAME}\"" + -DMQTT_PASSWORD="\"${sysenv.MQTT_PASSWORD}\"" lib_deps = GxEPD2 diff --git a/src/XD0MQTT.cpp b/src/XD0MQTT.cpp new file mode 100644 index 0000000..9688835 --- /dev/null +++ b/src/XD0MQTT.cpp @@ -0,0 +1,105 @@ +#include "XD0MQTT.h" + +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "tcpip_adapter.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" +#include "esp_tls.h" + +static const char *TAG = "MQTT"; + +XD0MQTT::XD0MQTT() {} + +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + //int mbedtls_err; esp_err_t err; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 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, "/topic/qos0", "data", 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"); + //mbedtls_err = 0; + //err = esp_tls_get_and_clear_last_error(event->error_handle, &mbedtls_err, NULL); + //ESP_LOGI(TAG, "Last esp error code: 0x%x", err); + //ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } + return ESP_OK; +} + +//static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { + esp_mqtt_client_handle_t client = event->client; + mqtt_event_handler_cb(event); + //ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + //mqtt_event_handler_cb(event_data); +} + +bool XD0MQTT::begin(void) { + esp_mqtt_client_config_t mqtt_cfg; + mqtt_cfg.uri = MQTT_BROKER_URI; + mqtt_cfg.event_handle = mqtt_event_handler; + mqtt_cfg.cert_pem = (const char *)rootCACertificate; + mqtt_cfg.username = MQTT_USERNAME; + mqtt_cfg.password = MQTT_PASSWORD; + mqtt_cfg.user_context = (void*)this; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + //esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + esp_mqtt_client_start(client); + + return true; +} diff --git a/src/XD0MQTT.h b/src/XD0MQTT.h new file mode 100644 index 0000000..245cb03 --- /dev/null +++ b/src/XD0MQTT.h @@ -0,0 +1,46 @@ +#ifndef _XD0MQTT_H +#define _XD0MQTT_H + +#include + +class XD0MQTT { + public: + XD0MQTT(void); + bool begin(void); + private: + // openssl s_client -showcerts -connect home.xd0.de:8883 /dev/null|openssl x509 -outform PEM >mqtt_xd0.de.pem + const char* rootCACertificate = \ + "-----BEGIN CERTIFICATE-----\n" \ + "MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \ + "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ + "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \ + "WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \ + "ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n" \ + "MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n" \ + "h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0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