ESP32 now publishes on broker

This commit is contained in:
2025-12-27 17:45:08 +01:00
parent 0ec10f765d
commit 19ad13843c
217 changed files with 5166 additions and 26 deletions

View File

@@ -33,6 +33,16 @@ dependencies:
registry_url: https://components.espressif.com/
type: service
version: 2.5.5
espressif/mqtt:
component_hash: ffdad5659706b4dc14bc63f8eb73ef765efa015bf7e9adf71c813d52a2dc9342
dependencies:
- name: idf
require: private
version: '>=5.3'
source:
registry_url: https://components.espressif.com/
type: service
version: 1.0.0
idf:
source:
type: idf
@@ -47,8 +57,9 @@ dependencies:
version: '*'
direct_dependencies:
- espressif/esp_ot_cli_extension
- espressif/mqtt
- idf
- ot_led
manifest_hash: 963824e0a62a27ce0c2b82c605b67acbe18d1e3adb4ad12c98b0302b37ad6174
manifest_hash: 133dc153f57b0466d9170ed20188e209c7ee22bbe2cc755c0f48394b1735e808
target: esp32h2
version: 2.0.0

View File

@@ -1,5 +1,5 @@
idf_component_register(
SRCS "esp_ot_cli.c"
INCLUDE_DIRS "."
REQUIRES esp_http_client openthread esp_netif freertos nvs_flash
REQUIRES mqtt esp_http_client openthread esp_netif freertos nvs_flash
)

View File

@@ -39,6 +39,7 @@
#include "openthread/logging.h"
#include "openthread/tasklet.h"
#include "esp_http_client.h"
#include "mqtt_client.h"
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
#include "ot_led_strip.h"
@@ -151,11 +152,78 @@ static void ot_task_worker(void *aContext)
vTaskDelete(NULL);
}
static bool mqtt_connected = false;
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;
switch (event->event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT Connected");
mqtt_connected = true;
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT Disconnected");
mqtt_connected = false;
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "Message published, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_ERROR:
ESP_LOGE(TAG, "MQTT Error");
mqtt_connected = false;
break;
default:
break;
}
}
static esp_mqtt_client_handle_t client = NULL;
void mqtt_init(void)
{
esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = "mqtt://192.168.15.119:1883",
.credentials = {
.username = NULL,
.client_id = NULL,
.set_null_client_id = true
},
.session.keepalive = 30,
.session.disable_clean_session = false,
};
client = esp_mqtt_client_init(&mqtt_cfg);
if (!client) {
ESP_LOGE(TAG, "Failed to init MQTT client");
return;
}
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
ESP_LOGI(TAG, "Starting MQTT client...");
esp_mqtt_client_start(client);
}
void send_gps_data(const char *topic, const char *json)
{
if (!client || !json) return;
if (!mqtt_connected) {
ESP_LOGW(TAG, "MQTT not connected, skipping GPS publish");
return;
}
int msg_id = esp_mqtt_client_publish(client, topic, json, 0, 1, 0);
if (msg_id < 0) {
ESP_LOGE(TAG, "Failed to publish GPS");
} else {
ESP_LOGI(TAG, "Published GPS: msg_id=%d", msg_id);
}
}
#define GPS_UART_NUM UART_NUM_1
#define GPS_TX_PIN 4
#define GPS_RX_PIN 5
#define BUF_SIZE 1024
static uint8_t gps_buffer[BUF_SIZE];
void init_gps() {
const uart_config_t uart_config = {
@@ -206,14 +274,12 @@ char* gps_to_json(char* gps_data) {
}
}
// Créer le JSON avec des valeurs valides même si GPS pas fixé
snprintf(json, sizeof(json), "{\"data\":{\"latitude\":%.6f,\"longitude\":%.6f}}",
latitude, longitude);
return json;
}
char* read_gps() {
int len = uart_read_bytes(GPS_UART_NUM, gps_buffer, BUF_SIZE - 1, 100 / portTICK_PERIOD_MS);
if (len > 0) {
@@ -223,32 +289,16 @@ char* read_gps() {
return NULL;
}
void send_gps_data(const char* server_url, const char* json)
{
if (!json) return;
ESP_LOGI(TAG, "Sending JSON: %s", json);
esp_http_client_config_t config = { .url = server_url };
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, json, strlen(json));
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_perform(client);
esp_http_client_cleanup(client);
}
void send_position_task(void *pvParameters)
{
const char* position_endpoint = "http://192.168.15.117:5000/devices/notify-position";
const char* gps_topic = "/board-mate/gps/notify";
while (1) {
char* json;
char* raw_data = read_gps();
if (raw_data) {
char* json = gps_to_json(raw_data);
send_gps_data(position_endpoint, json);
send_gps_data(gps_topic, json);
} else {
ESP_LOGI(TAG, "NO GPS DATA");
}
@@ -271,7 +321,8 @@ void app_main(void)
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
mqtt_init();
init_gps();
xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);

View File

@@ -1,8 +1,9 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_ot_cli_extension:
version: "~1.2.0"
version: ~1.2.0
idf:
version: ">=4.1.0"
version: '>=4.1.0'
ot_led:
path: ${IDF_PATH}/examples/openthread/ot_common_components/ot_led
espressif/mqtt: '*'