separate data send structures for mqtt and lorawan

This commit is contained in:
Bogdan Pilyugin 2022-12-19 15:54:02 +02:00
parent 84a3da060d
commit 944f264f48
9 changed files with 165 additions and 85 deletions

View File

@ -11,7 +11,7 @@ idf_component_register(
"src/WiFiTransport.c" "src/WiFiTransport.c"
"src/GSMTransport.c" "src/GSMTransport.c"
"src/ETHTransport.c" "src/ETHTransport.c"
"src/LoRaWANTransport.c" "src/LoRaWAN.c"
"src/SNTP.c" "src/SNTP.c"
"src/MQTT.c" "src/MQTT.c"
"src/MQTTSysHandler.c" "src/MQTTSysHandler.c"

41
include/LoRaWAN.h Normal file
View File

@ -0,0 +1,41 @@
/*! Copyright 2022 Bogdan Pilyugin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* \file LoRaWAN.h
* \version 1.0
* \date 2022-12-19
* \author Bogdan Pilyugin
* \brief
* \details
* \copyright Apache License, Version 2.0
*/
#ifndef COMPONENTS_WEBGUIAPP_INCLUDE_LORAWAN_H_
#define COMPONENTS_WEBGUIAPP_INCLUDE_LORAWAN_H_
typedef struct
{
char *raw_data_ptr;
int data_length;
}LORA_DATA_SEND_STRUCT;
void LoRaWANInitJoinTask(void *pvParameter);
void LoRaWANStop(void);
void LoRaWANStart(void);
esp_err_t LORASendData(LORA_DATA_SEND_STRUCT *pdss);
void regLoRaUserReceiveHandler(
void (*user_handler)(const char *message, int length, int port));
#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_LORAWAN_H_ */

View File

@ -52,7 +52,7 @@ typedef struct
char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH]; char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH];
char *raw_data_ptr; char *raw_data_ptr;
int data_length; int data_length;
}DATA_SEND_STRUCT; }MQTT_DATA_SEND_STRUCT;
/** /**
* @brief wrapper around esp_mqtt_client_handle_t with additional info * @brief wrapper around esp_mqtt_client_handle_t with additional info

View File

@ -48,21 +48,8 @@ typedef struct
QueueHandle_t MQTT1MessagesQueueHandle; QueueHandle_t MQTT1MessagesQueueHandle;
QueueHandle_t MQTT2MessagesQueueHandle; QueueHandle_t MQTT2MessagesQueueHandle;
EventGroupHandle_t transport_event_group; EventGroupHandle_t transport_event_group;
typedef enum
{
MQTT = 0,
} transport_data_type;
typedef struct
{
transport_data_type dType;
char *raw_data_ptr;
int data_length;
} net_transport_data_t;
void StartTimeGet(void); void StartTimeGet(void);
void WiFiAPStart(void); void WiFiAPStart(void);
@ -70,10 +57,7 @@ void WiFiSTAStart(void);
void EthStart(void); void EthStart(void);
void WiFiTransportTask(void *prm); void WiFiTransportTask(void *prm);
void LoRaWANInitJoinTask(void *pvParameter);
void LoRaWANStop(void);
void LoRaWANStart(void);
void PPPModemColdStart(void); void PPPModemColdStart(void);
void PPPModemSoftRestart(void); void PPPModemSoftRestart(void);
@ -122,7 +106,6 @@ uint32_t GetUpTime(void);
void RegEthReset(void (*eth_rst)(uint8_t level)); void RegEthReset(void (*eth_rst)(uint8_t level));
void RegGSMReset(void (*gsm_rst)(uint8_t level)); void RegGSMReset(void (*gsm_rst)(uint8_t level));
void regLoRaUserReceiveHandler(
void (*user_handler)(const char *message, int length, int port));
#endif /* MAIN_INCLUDE_NETTRANSPORT_H_ */ #endif /* MAIN_INCLUDE_NETTRANSPORT_H_ */

View File

@ -1,41 +1,42 @@
/*! Copyright 2022 Bogdan Pilyugin /*! Copyright 2022 Bogdan Pilyugin
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* \file webguiapp.h * \file webguiapp.h
* \version 1.0 * \version 1.0
* \date 2022-08-21 * \date 2022-08-21
* \author Bogdan Pilyugin * \author Bogdan Pilyugin
* \brief * \brief
* \details * \details
* \copyright Apache License, Version 2.0 * \copyright Apache License, Version 2.0
*/ */
#ifndef COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ #ifndef COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_
#define COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ #define COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_
#include "HTTPServer.h" #include "HTTPServer.h"
#include "MQTT.h" #include "MQTT.h"
#include "LoRaWAN.h"
#include "esp_log.h"
#include "esp_event.h" #include "esp_log.h"
#include "esp_netif.h" #include "esp_event.h"
#include "esp_system.h" #include "esp_netif.h"
#include "driver/spi_master.h" #include "esp_system.h"
#include "driver/spi_master.h"
esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle, spi_transaction_t *trans_desc);
esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle, spi_transaction_t *trans_desc);
bool GetUserAppNeedReset(void);
void SetUserAppNeedReset(bool res); bool GetUserAppNeedReset(void);
void SetUserAppNeedReset(bool res);
#endif /* COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ */
#endif /* COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ */

View File

@ -22,6 +22,7 @@
*/ */
#include "HTTPServer.h" #include "HTTPServer.h"
#include "LoRaWAN.h"
static const char *TAG = "HTTPServerPost"; static const char *TAG = "HTTPServerPost";
@ -104,26 +105,26 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData)
if (httpd_query_key_value(PostData, "ethen", tmp, sizeof(tmp)) == ESP_OK) if (httpd_query_key_value(PostData, "ethen", tmp, sizeof(tmp)) == ESP_OK)
{ {
if (!strcmp((const char*) tmp, (const char*) "1")) if (!strcmp((const char*) tmp, (const char*) "1"))
TempIsETHEnabled = true; TempIsETHEnabled = true;
} }
if (httpd_query_key_value(PostData, "dhcp", tmp, sizeof(tmp)) == ESP_OK) if (httpd_query_key_value(PostData, "dhcp", tmp, sizeof(tmp)) == ESP_OK)
{ {
if (!strcmp((const char*) tmp, (const char*) "1")) if (!strcmp((const char*) tmp, (const char*) "1"))
TempIsETHDHCPEnabled = true; TempIsETHDHCPEnabled = true;
} }
if (httpd_query_key_value(PostData, "ipa", tmp, 15) == ESP_OK) if (httpd_query_key_value(PostData, "ipa", tmp, 15) == ESP_OK)
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr); esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr);
if (httpd_query_key_value(PostData, "mas", tmp, 15) == ESP_OK) if (httpd_query_key_value(PostData, "mas", tmp, 15) == ESP_OK)
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask); esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask);
if (httpd_query_key_value(PostData, "gte", tmp, 15) == ESP_OK) if (httpd_query_key_value(PostData, "gte", tmp, 15) == ESP_OK)
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway); esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway);
if (httpd_query_key_value(PostData, "dns1", tmp, 15) == ESP_OK) if (httpd_query_key_value(PostData, "dns1", tmp, 15) == ESP_OK)
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1); esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1);
if (httpd_query_key_value(PostData, "dns2", tmp, 15) == ESP_OK) if (httpd_query_key_value(PostData, "dns2", tmp, 15) == ESP_OK)
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2); esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2);
if (httpd_query_key_value(PostData, "dns3", tmp, 15) == ESP_OK) if (httpd_query_key_value(PostData, "dns3", tmp, 15) == ESP_OK)
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3); esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3);
#endif #endif
@ -445,6 +446,58 @@ static HTTP_IO_RESULT HTTPPostSystemSettings(httpd_req_t *req, char *PostData)
} }
if (httpd_query_key_value(PostData, "cmd", tmp, 4) == ESP_OK)
{
if (!strcmp(tmp, (const char*) "1"))
{
ESP_LOGI(TAG, "Got command F1 send test lora");
const char test[] = {"LoRaWAN test message"};
LORA_DATA_SEND_STRUCT dss;
dss.raw_data_ptr = test;
dss.data_length = sizeof(test)+1;
LORASendData(&dss);
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "2"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "3"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "4"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "5"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "6"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "7"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "8"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "9"))
{
return HTTP_IO_DONE_NOREFRESH;
}
else if (!strcmp(tmp, (const char*) "10"))
{
return HTTP_IO_DONE_NOREFRESH;
}
}
return HTTP_IO_DONE; return HTTP_IO_DONE;
} }

View File

@ -28,6 +28,7 @@
#include "SystemConfiguration.h" #include "SystemConfiguration.h"
#include "NetTransport.h" #include "NetTransport.h"
#include "webguiapp.h" #include "webguiapp.h"
#include "LoRaWAN.h"
// Pins and other resources // Pins and other resources
/*Defined in global configuration*/ /*Defined in global configuration*/
@ -52,7 +53,7 @@ static const int LORA_WAIT_DELIVERY_BIT = BIT2;
QueueHandle_t LORAMessagesQueueHandle; QueueHandle_t LORAMessagesQueueHandle;
static StaticQueue_t xStaticLoRaMessagesQueue; static StaticQueue_t xStaticLoRaMessagesQueue;
uint8_t LoRaMessagesQueueStorageArea[LORAWAN_MESSAGE_BUFER_LENTH uint8_t LoRaMessagesQueueStorageArea[LORAWAN_MESSAGE_BUFER_LENTH
* sizeof(DATA_SEND_STRUCT)]; * sizeof(LORA_DATA_SEND_STRUCT)];
void (*LoRaUserReceiveHandler)(const char *message, int length, int port); void (*LoRaUserReceiveHandler)(const char *message, int length, int port);
void regLoRaUserReceiveHandler( void regLoRaUserReceiveHandler(
@ -62,13 +63,13 @@ void regLoRaUserReceiveHandler(
} }
esp_err_t LORASendData(DATA_SEND_STRUCT *pdss) esp_err_t LORASendData(LORA_DATA_SEND_STRUCT *pdss)
{ {
char *ptr = (char*) malloc(MESSAGE_LENGTH); char *ptr = (char*) malloc(MESSAGE_LENGTH);
if (ptr) if (ptr)
{ {
memcpy(ptr, pdss->raw_data_ptr, MESSAGE_LENGTH); memcpy(ptr, pdss->raw_data_ptr, MESSAGE_LENGTH);
DATA_SEND_STRUCT DSS; LORA_DATA_SEND_STRUCT DSS;
DSS.raw_data_ptr = ptr; DSS.raw_data_ptr = ptr;
DSS.data_length = MESSAGE_LENGTH; DSS.data_length = MESSAGE_LENGTH;
@ -96,13 +97,14 @@ void messageReceived(const uint8_t *message, size_t length, ttn_port_t port)
ESP_LOGI(TAG, "Received=%s", P); ESP_LOGI(TAG, "Received=%s", P);
} }
#endif #endif
//TODO Here registered from application handler must be called if(LoRaUserReceiveHandler != NULL)
LoRaUserReceiveHandler((char*)message, length, (int)port);
} }
void LoRaWANTransportTask(void *pvParameter) void LoRaWANTransportTask(void *pvParameter)
{ {
DATA_SEND_STRUCT DSS; LORA_DATA_SEND_STRUCT DSS;
while (!LORAMessagesQueueHandle) while (!LORAMessagesQueueHandle)
vTaskDelay(pdMS_TO_TICKS(300)); //wait for LORA queue ready vTaskDelay(pdMS_TO_TICKS(300)); //wait for LORA queue ready
while (1) while (1)
@ -146,7 +148,7 @@ void LoRaWANInitJoinTask(void *pvParameter)
LORAMessagesQueueHandle = NULL; LORAMessagesQueueHandle = NULL;
if (GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled) if (GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled)
LORAMessagesQueueHandle = xQueueCreateStatic(LORAWAN_MESSAGE_BUFER_LENTH, LORAMessagesQueueHandle = xQueueCreateStatic(LORAWAN_MESSAGE_BUFER_LENTH,
sizeof(DATA_SEND_STRUCT), sizeof(LORA_DATA_SEND_STRUCT),
LoRaMessagesQueueStorageArea, LoRaMessagesQueueStorageArea,
&xStaticLoRaMessagesQueue); &xStaticLoRaMessagesQueue);

View File

@ -25,7 +25,7 @@
#include "NetTransport.h" #include "NetTransport.h"
#include "MQTT.h" #include "MQTT.h"
#define CH_MESSAGE_BUFER_LENTH 32 //size of mqtt queue #define MQTT_MESSAGE_BUFER_LENTH 32 //size of mqtt queue
#define MQTT_RECONNECT_CHANGE_ADAPTER 3 #define MQTT_RECONNECT_CHANGE_ADAPTER 3
#if CONFIG_WEBGUIAPP_MQTT_ENABLE #if CONFIG_WEBGUIAPP_MQTT_ENABLE
@ -34,8 +34,8 @@ static SemaphoreHandle_t xSemaphoreMQTTHandle = NULL;
static StaticSemaphore_t xSemaphoreMQTTBuf; static StaticSemaphore_t xSemaphoreMQTTBuf;
static StaticQueue_t xStaticMQTT1MessagesQueue; static StaticQueue_t xStaticMQTT1MessagesQueue;
static StaticQueue_t xStaticMQTT2MessagesQueue; static StaticQueue_t xStaticMQTT2MessagesQueue;
uint8_t MQTT1MessagesQueueStorageArea[CH_MESSAGE_BUFER_LENTH * sizeof(DATA_SEND_STRUCT)]; uint8_t MQTT1MessagesQueueStorageArea[MQTT_MESSAGE_BUFER_LENTH * sizeof(MQTT_DATA_SEND_STRUCT)];
uint8_t MQTT2MessagesQueueStorageArea[CH_MESSAGE_BUFER_LENTH * sizeof(DATA_SEND_STRUCT)]; uint8_t MQTT2MessagesQueueStorageArea[MQTT_MESSAGE_BUFER_LENTH * sizeof(MQTT_DATA_SEND_STRUCT)];
mqtt_client_t mqtt[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM] = { 0 }; mqtt_client_t mqtt[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM] = { 0 };
@ -207,7 +207,7 @@ void MQTTReconnect(void)
void MQTTTaskTransmit(void *pvParameter) void MQTTTaskTransmit(void *pvParameter)
{ {
DATA_SEND_STRUCT DSS; MQTT_DATA_SEND_STRUCT DSS;
int idx = *(int*) pvParameter; int idx = *(int*) pvParameter;
while (!mqtt[idx].mqtt_queue) while (!mqtt[idx].mqtt_queue)
vTaskDelay(pdMS_TO_TICKS(300)); //wait for MQTT queue ready vTaskDelay(pdMS_TO_TICKS(300)); //wait for MQTT queue ready
@ -283,16 +283,16 @@ void MQTTRun(void)
MQTT1MessagesQueueHandle = NULL; MQTT1MessagesQueueHandle = NULL;
MQTT2MessagesQueueHandle = NULL; MQTT2MessagesQueueHandle = NULL;
if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled) if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled)
MQTT1MessagesQueueHandle = xQueueCreateStatic(CH_MESSAGE_BUFER_LENTH, MQTT1MessagesQueueHandle = xQueueCreateStatic(MQTT_MESSAGE_BUFER_LENTH,
sizeof(DATA_SEND_STRUCT), sizeof(MQTT_DATA_SEND_STRUCT),
MQTT1MessagesQueueStorageArea, MQTT1MessagesQueueStorageArea,
&xStaticMQTT1MessagesQueue); &xStaticMQTT1MessagesQueue);
mqtt[0].mqtt_queue = MQTT1MessagesQueueHandle; mqtt[0].mqtt_queue = MQTT1MessagesQueueHandle;
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2 #if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
if (GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled) if (GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled)
MQTT2MessagesQueueHandle = xQueueCreateStatic(CH_MESSAGE_BUFER_LENTH, MQTT2MessagesQueueHandle = xQueueCreateStatic(MQTT_MESSAGE_BUFER_LENTH,
sizeof(DATA_SEND_STRUCT), sizeof(MQTT_DATA_SEND_STRUCT),
MQTT2MessagesQueueStorageArea, MQTT2MessagesQueueStorageArea,
&xStaticMQTT2MessagesQueue); &xStaticMQTT2MessagesQueue);
mqtt[1].mqtt_queue = MQTT2MessagesQueueHandle; mqtt[1].mqtt_queue = MQTT2MessagesQueueHandle;

View File

@ -130,7 +130,7 @@ static mqtt_app_err_t ResponceWithError(int idx,
if (buf) if (buf)
{ {
memcpy(buf, JSONErrorMess, strlen(JSONErrorMess)); memcpy(buf, JSONErrorMess, strlen(JSONErrorMess));
DATA_SEND_STRUCT DSS; MQTT_DATA_SEND_STRUCT DSS;
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK"); ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
DSS.raw_data_ptr = buf; DSS.raw_data_ptr = buf;
DSS.data_length = strlen(JSONErrorMess); DSS.data_length = strlen(JSONErrorMess);
@ -217,7 +217,7 @@ static mqtt_app_err_t ResponceWithFile(int idx, espfs_file_t *file,
const char tail[] = "}"; const char tail[] = "}";
strcat((fdata + readBytes), tail); strcat((fdata + readBytes), tail);
free(filebuf); free(filebuf);
DATA_SEND_STRUCT DSS; MQTT_DATA_SEND_STRUCT DSS;
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK"); ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
DSS.raw_data_ptr = outbuf; DSS.raw_data_ptr = outbuf;
DSS.data_length = (fdata - outbuf) + readBytes + strlen(tail); DSS.data_length = (fdata - outbuf) + readBytes + strlen(tail);