128 lines
2.0 KiB
C
128 lines
2.0 KiB
C
/*
|
|
* mqtt_service.c
|
|
*
|
|
* Created on: 28 May 2026
|
|
* Author: Christian Lind Vie Madsen
|
|
*/
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
#include <freertos/queue.h>
|
|
#include "mqtt_service.h"
|
|
#include "mqtt_driver.h"
|
|
#include "mqtt_protocol.h"
|
|
|
|
// Not important libs..
|
|
#include "esp_log.h"
|
|
#define TAG "mqtt_service.c"
|
|
|
|
#define HOURSTOMS(hours) ((hours) * 60 * 60 * 1000)
|
|
|
|
TaskHandle_t mqtt_service_handle;
|
|
QueueHandle_t mqtt_queue;
|
|
|
|
typedef enum {
|
|
|
|
MSG_STAT = 0,
|
|
|
|
}mqtt_header_t;
|
|
|
|
typedef struct {
|
|
|
|
mqtt_header_t header;
|
|
|
|
}mqtt_service_msg_t;
|
|
/*
|
|
static void sRepeatedMqttSend(){
|
|
|
|
static uint32_t stimer = 0;
|
|
|
|
|
|
// Send Heartbeat
|
|
if( (stimer++ % HOURSTOMS(1)10000) == 0){
|
|
|
|
char topic[124] = {0};
|
|
char data[124] = {0};
|
|
|
|
char lat[24] = {0};
|
|
char lon[24] = {0};
|
|
|
|
mqtt_protocol_GenerateTopic(topic, sizeof(topic), MQTT_PROTOCOL_HEARTBEAT);
|
|
mqtt_protocol_heartbeat(data, sizeof(data), "1234", "12", lat, lon);
|
|
|
|
// For now.. just send it with GSM..
|
|
if(gps_gsm_mqtt_publish(topic, data)){
|
|
// If GSM is not available, then send over WIFI.
|
|
//mqtt_driver_publish(topic,data);
|
|
}
|
|
//ESP_LOGI(TAG,"Sending MQTT Heartbeat");
|
|
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
static void mqtt_task(void *pv){
|
|
|
|
// Init MQTT WIFI
|
|
mqtt_start();
|
|
|
|
mqtt_service_msg_t msg;
|
|
|
|
while(1){
|
|
|
|
vTaskDelay(1);
|
|
if (xQueueReceive(mqtt_queue,
|
|
&msg,
|
|
pdMS_TO_TICKS(1)))
|
|
{
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
//sRepeatedMqttSend();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int mqtt_service_init(){
|
|
|
|
// Create IO request queue
|
|
mqtt_queue = xQueueCreate(
|
|
10,
|
|
sizeof(mqtt_service_msg_t)
|
|
);
|
|
|
|
if (mqtt_queue == NULL)
|
|
{
|
|
ESP_LOGE(TAG, "Failed to create IO queue");
|
|
return 1;
|
|
}
|
|
|
|
|
|
|
|
// Create the mqtt task
|
|
xTaskCreate(
|
|
mqtt_task, // Task function
|
|
"mqtt_service", // Name
|
|
4096, // Stack size
|
|
NULL, // Parameters
|
|
1, // Priority
|
|
&mqtt_service_handle // Task handle
|
|
);
|
|
|
|
ESP_LOGI(TAG, "mqtt_service task started");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|