parent
970fda699d
commit
51a85c441d
@ -0,0 +1,59 @@ |
|||||||
|
/*
|
||||||
|
* application.s |
||||||
|
* |
||||||
|
* Created on: 25.11.2022 |
||||||
|
* Author: jonas |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "application.h" |
||||||
|
#include "freertos/FreeRTOS.h" |
||||||
|
#include "freertos/task.h" |
||||||
|
#include "myMqtt.h" |
||||||
|
#include "wifi.h" |
||||||
|
|
||||||
|
static TaskHandle_t appTaskHandle; |
||||||
|
|
||||||
|
static void appTask(void *pv){ |
||||||
|
if(pv != NULL){ |
||||||
|
printf("task argument: %s\n", (char*)pv); |
||||||
|
} |
||||||
|
|
||||||
|
printf("Application was started"); |
||||||
|
fflush(stdout); |
||||||
|
|
||||||
|
while(WiFi_isConnected() == false){ |
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000)); |
||||||
|
} |
||||||
|
|
||||||
|
// wifi now connected
|
||||||
|
printf("Application task detected that WiFi is connected\n"); |
||||||
|
if(MyMqtt_Init()){ |
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000)); |
||||||
|
printf("5s done\n"); |
||||||
|
|
||||||
|
MyMqtt_Subscribe("scada/status"); |
||||||
|
|
||||||
|
printf("Application endless task was started\n"); |
||||||
|
fflush(stdout); |
||||||
|
|
||||||
|
// endless loop
|
||||||
|
for(;;){ |
||||||
|
MyMqtt_Publish("scada/status", "Testmessage from ESP"); |
||||||
|
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(10000)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
printf("Init of MyMqtt failed! Quitting application task.\n"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
BaseType_t res; |
||||||
|
|
||||||
|
void Application_Start(void){ |
||||||
|
res = xTaskCreate(appTask, "task1", 4096/sizeof(StackType_t), (void*)"ARG_0", tskIDLE_PRIORITY, &appTaskHandle); |
||||||
|
if(res != pdPASS){ |
||||||
|
printf("creating myTask failed!\r\n"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
@ -0,0 +1,13 @@ |
|||||||
|
/*
|
||||||
|
* application.h |
||||||
|
* |
||||||
|
* Created on: 25.11.2022 |
||||||
|
* Author: jonas |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef MAIN_APPLICATION_H_ |
||||||
|
#define MAIN_APPLICATION_H_ |
||||||
|
|
||||||
|
void Application_Start(void); |
||||||
|
|
||||||
|
#endif /* MAIN_APPLICATION_H_ */ |
||||||
@ -0,0 +1,121 @@ |
|||||||
|
/*
|
||||||
|
* myMqtt.c |
||||||
|
* |
||||||
|
* Created on: 25.11.2022 |
||||||
|
* Author: jonas |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "myMqtt.h" |
||||||
|
#include <stdio.h> |
||||||
|
#include <stdbool.h> |
||||||
|
#include <stdint.h> |
||||||
|
#include "esp_log.h" |
||||||
|
#include "mqtt_client.h" |
||||||
|
|
||||||
|
// Settings
|
||||||
|
#define CONFIG_BROKER_URL "mqtt://10.180.254.80"
|
||||||
|
|
||||||
|
// tag for logging with ESP_LOG
|
||||||
|
static const char *TAG = "MY_MQTT"; |
||||||
|
|
||||||
|
// declarations
|
||||||
|
static void log_error_if_nonzero(const char *message, int error_code); |
||||||
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data); |
||||||
|
|
||||||
|
// variables
|
||||||
|
esp_mqtt_client_handle_t client; |
||||||
|
|
||||||
|
bool MyMqtt_Init(void){ |
||||||
|
esp_mqtt_client_config_t mqtt_cfg = { |
||||||
|
.uri = CONFIG_BROKER_URL, |
||||||
|
}; |
||||||
|
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_EVENT_ANY_ID, mqtt_event_handler, NULL); |
||||||
|
esp_err_t mqtt_error = esp_mqtt_client_start(client); |
||||||
|
|
||||||
|
return mqtt_error == ESP_OK; |
||||||
|
} |
||||||
|
|
||||||
|
void MyMqtt_Deinit(void){ |
||||||
|
client = NULL; |
||||||
|
} |
||||||
|
|
||||||
|
void MyMqtt_Publish(const char *topic, const char *data){ |
||||||
|
int msg_id = esp_mqtt_client_publish(client, topic, data, 0, 1, 0); |
||||||
|
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
||||||
|
} |
||||||
|
|
||||||
|
void MyMqtt_Subscribe(const char *topic){ |
||||||
|
int msg_id = esp_mqtt_client_subscribe(client, topic, 0); |
||||||
|
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d, success=%s", msg_id, msg_id==-1 ? "false" : "true"); |
||||||
|
} |
||||||
|
|
||||||
|
/***********/ |
||||||
|
/* HELPERS */ |
||||||
|
/***********/ |
||||||
|
|
||||||
|
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); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief Event handler registered to receive MQTT events |
||||||
|
* |
||||||
|
* This function is called by the MQTT client event loop. |
||||||
|
* |
||||||
|
* @param handler_args user data registered to the event. |
||||||
|
* @param base Event base for the handler(always MQTT Base in this example). |
||||||
|
* @param event_id The id for the received event. |
||||||
|
* @param event_data The data for the event, esp_mqtt_event_handle_t. |
||||||
|
*/ |
||||||
|
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 = event_data; |
||||||
|
// unused: esp_mqtt_client_handle_t client = event->client;
|
||||||
|
switch ((esp_mqtt_event_id_t)event_id) { |
||||||
|
case MQTT_EVENT_CONNECTED: |
||||||
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
||||||
|
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); |
||||||
|
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; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
/*
|
||||||
|
* myMqtt.h |
||||||
|
* |
||||||
|
* Created on: 25.11.2022 |
||||||
|
* Author: jonas |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef MAIN_MYMQTT_H_ |
||||||
|
#define MAIN_MYMQTT_H_ |
||||||
|
|
||||||
|
#include <stdbool.h> |
||||||
|
|
||||||
|
bool MyMqtt_Init(void); |
||||||
|
|
||||||
|
void MyMqtt_Deinit(void); |
||||||
|
|
||||||
|
void MyMqtt_Publish(const char *topic, const char *data); |
||||||
|
|
||||||
|
void MyMqtt_Subscribe(const char *topic); |
||||||
|
|
||||||
|
#endif /* MAIN_MYMQTT_H_ */ |
||||||
Loading…
Reference in new issue