connect via MQTTS
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -3,4 +3,6 @@ idf_component_register(
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
persistence-manager
|
||||
my_mqtt_client
|
||||
app_update
|
||||
)
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#include "message_manager.h"
|
||||
#include "my_mqtt_client.h"
|
||||
#include <esp_app_desc.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_mac.h>
|
||||
#include <esp_system.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
#include <freertos/task.h>
|
||||
#include <persistence_manager.h>
|
||||
#include <sdkconfig.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MESSAGE_QUEUE_LENGTH 16
|
||||
@@ -98,6 +103,15 @@ static void message_manager_task(void *param)
|
||||
message_listeners[i](&msg);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||
char topic[60];
|
||||
snprintf(topic, sizeof(topic), "device/%s/%02x%02x", app_desc->project_name, mac[4], mac[5]);
|
||||
|
||||
char *data = "{\"key\":\"value\"}";
|
||||
mqtt_client_publish(topic, data, strlen(data), 0, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
7
firmware/components/my_mqtt_client/CMakeLists.txt
Normal file
7
firmware/components/my_mqtt_client/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
idf_component_register(
|
||||
SRCS "src/my_mqtt_client.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
mqtt
|
||||
app_update
|
||||
)
|
||||
21
firmware/components/my_mqtt_client/Kconfig
Normal file
21
firmware/components/my_mqtt_client/Kconfig
Normal file
@@ -0,0 +1,21 @@
|
||||
menu "MQTT Client Settings"
|
||||
|
||||
config MQTT_CLIENT_BROKER_URL
|
||||
string "MQTT Broker URL (TLS)"
|
||||
default "mqtts://example.com:8883"
|
||||
help
|
||||
Die Adresse des MQTT-Brokers (z.B. mqtts://broker.example.com:8883)
|
||||
|
||||
config MQTT_CLIENT_USERNAME
|
||||
string "MQTT Username"
|
||||
default "user"
|
||||
help
|
||||
Benutzername für die Authentifizierung (optional)
|
||||
|
||||
config MQTT_CLIENT_PASSWORD
|
||||
string "MQTT Password"
|
||||
default "password"
|
||||
help
|
||||
Passwort für die Authentifizierung (optional)
|
||||
|
||||
endmenu
|
||||
18
firmware/components/my_mqtt_client/README.md
Normal file
18
firmware/components/my_mqtt_client/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# MQTT Client Component for ESP-IDF
|
||||
|
||||
Diese Komponente stellt eine einfache MQTT-Client-Implementierung bereit, die Daten an einen TLS-gesicherten MQTT-Broker sendet.
|
||||
|
||||
## Dateien
|
||||
- mqtt_client.c: Implementierung des MQTT-Clients
|
||||
- mqtt_client.h: Header-Datei
|
||||
- CMakeLists.txt: Build-Konfiguration
|
||||
- Kconfig: Konfiguration für die Komponente
|
||||
|
||||
## Abhängigkeiten
|
||||
- ESP-IDF (empfohlen: >= v4.0)
|
||||
- Komponenten: esp-mqtt, esp-tls
|
||||
|
||||
## Nutzung
|
||||
1. Füge die Komponente in dein Projekt ein.
|
||||
2. Passe die Konfiguration in `Kconfig` an.
|
||||
3. Binde die Komponente in deinem Code ein und nutze die API aus `mqtt_client.h`.
|
||||
2
firmware/components/my_mqtt_client/idf_component.yml
Normal file
2
firmware/components/my_mqtt_client/idf_component.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
dependencies:
|
||||
espressif/mqtt: ^1.0.0
|
||||
14
firmware/components/my_mqtt_client/include/my_mqtt_client.h
Normal file
14
firmware/components/my_mqtt_client/include/my_mqtt_client.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void mqtt_client_start(void);
|
||||
void mqtt_client_publish(const char *topic, const char *data, size_t len, int qos, bool retain);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
122
firmware/components/my_mqtt_client/src/my_mqtt_client.c
Normal file
122
firmware/components/my_mqtt_client/src/my_mqtt_client.c
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "my_mqtt_client.h"
|
||||
#include "esp_app_desc.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_interface.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_system.h"
|
||||
#include "mqtt_client.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static const char *TAG = "mqtt_client";
|
||||
static esp_mqtt_client_handle_t client = NULL;
|
||||
|
||||
extern const uint8_t isrgrootx1_pem_start[] asm("_binary_isrgrootx1_pem_start");
|
||||
extern const uint8_t isrgrootx1_pem_end[] asm("_binary_isrgrootx1_pem_end");
|
||||
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
esp_mqtt_event_handle_t event = event_data;
|
||||
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, "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, return code=0x%02x ", event->msg_id, (uint8_t)*event->data);
|
||||
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:");
|
||||
ESP_LOGI(TAG, "TOPIC=%.*s\r\n", event->topic_len, event->topic);
|
||||
ESP_LOGI(TAG, "DATA=%.*s\r\n", event->data_len, event->data);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_ERROR:
|
||||
ESP_LOGE(TAG, "MQTT_EVENT_ERROR");
|
||||
if (event->error_handle)
|
||||
{
|
||||
ESP_LOGE(TAG, "error_type: %d", event->error_handle->error_type);
|
||||
ESP_LOGE(TAG, "esp-tls error code: 0x%x", event->error_handle->esp_tls_last_esp_err);
|
||||
ESP_LOGE(TAG, "tls_stack_err: 0x%x", event->error_handle->esp_tls_stack_err);
|
||||
ESP_LOGE(TAG, "transport_sock_errno: %d", event->error_handle->esp_transport_sock_errno);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_client_start(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Starte MQTT-Client mit URI: %s", CONFIG_MQTT_CLIENT_BROKER_URL);
|
||||
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||
char client_id[60];
|
||||
snprintf(client_id, sizeof(client_id), "%s/%02x%02x", app_desc->project_name, mac[4], mac[5]);
|
||||
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.broker.address.uri = CONFIG_MQTT_CLIENT_BROKER_URL,
|
||||
.broker.verification.certificate = (const char *)isrgrootx1_pem_start,
|
||||
.broker.verification.certificate_len = isrgrootx1_pem_end - isrgrootx1_pem_start,
|
||||
.credentials.username = CONFIG_MQTT_CLIENT_USERNAME,
|
||||
.credentials.client_id = client_id,
|
||||
.credentials.authentication.password = CONFIG_MQTT_CLIENT_PASSWORD,
|
||||
};
|
||||
client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
if (client == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Fehler bei esp_mqtt_client_init!");
|
||||
return;
|
||||
}
|
||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
||||
esp_err_t err = esp_mqtt_client_start(client);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_mqtt_client_start fehlgeschlagen: %s", esp_err_to_name(err));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "MQTT-Client gestartet");
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_client_publish(const char *topic, const char *data, size_t len, int qos, bool retain)
|
||||
{
|
||||
if (client)
|
||||
{
|
||||
int msg_id = esp_mqtt_client_publish(client, topic, data, len, qos, retain);
|
||||
ESP_LOGI(TAG, "Publish: topic=%s, msg_id=%d, qos=%d, retain=%d, len=%d", topic, msg_id, qos, retain, (int)len);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "Publish aufgerufen, aber Client ist nicht initialisiert!");
|
||||
}
|
||||
}
|
||||
@@ -372,7 +372,8 @@ void start_simulation_task(void)
|
||||
{
|
||||
stop_simulation_task();
|
||||
|
||||
simulation_config_t *config = (simulation_config_t *)heap_caps_malloc(sizeof(simulation_config_t), 0);
|
||||
simulation_config_t *config =
|
||||
(simulation_config_t *)heap_caps_malloc(sizeof(simulation_config_t), MALLOC_CAP_DEFAULT);
|
||||
if (config == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for simulation config.");
|
||||
|
||||
Reference in New Issue
Block a user