lorawan moved to webguiapp
This commit is contained in:
parent
e8287d8746
commit
e181984c8e
|
|
@ -11,6 +11,7 @@ idf_component_register(
|
|||
"src/WiFiTransport.c"
|
||||
"src/GSMTransport.c"
|
||||
"src/ETHTransport.c"
|
||||
"src/LoRaWANTransport.c"
|
||||
"src/SNTP.c"
|
||||
"src/MQTT.c"
|
||||
"src/MQTTSysHandler.c"
|
||||
|
|
@ -33,4 +34,5 @@ idf_component_register(
|
|||
esp_https_ota
|
||||
app_update
|
||||
jReadWrite
|
||||
ttn-esp32
|
||||
)
|
||||
44
Kconfig
44
Kconfig
|
|
@ -420,8 +420,6 @@ menu "WebGUIApp"
|
|||
endif
|
||||
endmenu
|
||||
|
||||
|
||||
|
||||
menu "GPRS settings"
|
||||
config WEBGUIAPP_GPRS_ENABLE
|
||||
bool "Enabled GPRS PPP interface"
|
||||
|
|
@ -577,6 +575,48 @@ menu "WebGUIApp"
|
|||
endif
|
||||
endmenu
|
||||
|
||||
menu "LoRaWAN settings"
|
||||
config WEBGUIAPP_LORAWAN_ENABLE
|
||||
bool "Enabled LoRaWAN interface"
|
||||
default n
|
||||
help
|
||||
Set enabled LoRaWAN module
|
||||
|
||||
if WEBGUIAPP_LORAWAN_ENABLE
|
||||
config LORA_SPI_CS_GPIO
|
||||
int "SPI CS GPIO number for LoRa module"
|
||||
range 0 33
|
||||
default 4
|
||||
help
|
||||
Set the GPIO number used by SPI chip select for LoRa module.
|
||||
config LORA_DIO0_GPIO
|
||||
int "DIO0 GPIO number for LoRa module"
|
||||
range 0 39
|
||||
default 34
|
||||
help
|
||||
Set the GPIO number used by .
|
||||
|
||||
config LORA_DIO1_GPIO
|
||||
int "DIO1 GPI1 number for LoRa module"
|
||||
range 0 39
|
||||
default 35
|
||||
help
|
||||
Set the GPIO number used by .
|
||||
config LORA_APP_ID
|
||||
string "Default LoRaWAN Application ID"
|
||||
default "BBBBBBBBBBBBBBBB"
|
||||
help
|
||||
Set default application ID for LoRaWAN network
|
||||
|
||||
config LORA_APP_KEY
|
||||
string "Default LoRaWAN Application key"
|
||||
default "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
|
||||
help
|
||||
Set default application key for LoRaWAN network
|
||||
endif
|
||||
endmenu
|
||||
|
||||
|
||||
menu "MQTT settings"
|
||||
config WEBGUIAPP_MQTT_ENABLE
|
||||
bool "Enabled MQTT transport"
|
||||
|
|
|
|||
|
|
@ -46,18 +46,25 @@ typedef struct
|
|||
|
||||
//#define DEFAULT_FALLBACK_DNS "8.8.8.8"
|
||||
|
||||
|
||||
QueueHandle_t MQTT1MessagesQueueHandle;
|
||||
QueueHandle_t MQTT2MessagesQueueHandle;
|
||||
|
||||
|
||||
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 WiFiAPStart(void);
|
||||
void WiFiSTAStart(void);
|
||||
void EthStart(void);
|
||||
|
|
|
|||
|
|
@ -76,11 +76,6 @@
|
|||
#define DEFAULT_DNS2 "4.4.8.8"
|
||||
#define DEFAULT_DNS3 "1.1.1.1"
|
||||
|
||||
/*GSM DEFAULT SETTINGS*/
|
||||
#define DEFAULT_GSM_GLOBAL_ENABLED true
|
||||
|
||||
|
||||
//#define LOCK_RELAY_ON
|
||||
|
||||
// Application-dependent structure used to contain address information
|
||||
|
||||
|
|
@ -240,6 +235,27 @@
|
|||
} gsmSettings;
|
||||
#endif
|
||||
|
||||
struct
|
||||
{
|
||||
char DevEui[8];
|
||||
char AppEui[8];
|
||||
char AppKey[16];
|
||||
|
||||
struct
|
||||
{
|
||||
char b0 :1;
|
||||
char b1 :1;
|
||||
char b2 :1;
|
||||
char b3 :1;
|
||||
char b4 :1;
|
||||
char b5 :1;
|
||||
char b6 :1;
|
||||
char bIsLoRaWANEnabled :1;
|
||||
} Flags1; // Flag structure
|
||||
|
||||
} lorawanSettings;
|
||||
|
||||
|
||||
} SYS_CONFIG;
|
||||
|
||||
esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf);
|
||||
|
|
|
|||
|
|
@ -461,6 +461,41 @@ void HTTPPrint_gsmmac(char *VarData, void *arg)
|
|||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
/*LORAWAN settings*/
|
||||
void HTTPPrint_lren(char *VarData, void *arg)
|
||||
{
|
||||
PrintCheckbox(VarData, arg, GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled);
|
||||
}
|
||||
void HTTPPrint_lrstat(char *VarData, void *arg)
|
||||
{
|
||||
if (isLORAConnected())
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "CONNECTED");
|
||||
else
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "DISCONNECTED");
|
||||
}
|
||||
void HTTPPrint_lrdvid(char *VarData, void *arg)
|
||||
{
|
||||
uint8_t temp[16];
|
||||
BytesToStr((unsigned char*) &GetSysConf()->lorawanSettings.DevEui, temp, 8);
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", temp);
|
||||
}
|
||||
|
||||
void HTTPPrint_lrapid(char *VarData, void *arg)
|
||||
{
|
||||
uint8_t temp[16];
|
||||
BytesToStr((unsigned char*) &GetSysConf()->lorawanSettings.AppEui, temp, 8);
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", temp);
|
||||
}
|
||||
void HTTPPrint_lrapkey(char *VarData, void *arg)
|
||||
{
|
||||
uint8_t temp[32];
|
||||
BytesToStr((unsigned char*) &GetSysConf()->lorawanSettings.AppKey, temp, 16);
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", temp);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
void HTTPPrint_mqen1(char *VarData, void *arg)
|
||||
{
|
||||
|
|
@ -564,26 +599,45 @@ static void HTTPPrint_mqtt2st(char *VarData, void *arg)
|
|||
snprintf(VarData, MAX_DYNVAR_LENGTH, "DISCONNECTED");
|
||||
}
|
||||
|
||||
|
||||
/* Pass build configuration to web interface*/
|
||||
static void HTTPPrint_ifc_gprs(char *VarData, void *arg)
|
||||
static void HTTPPrint_hide_gprs(char *VarData, void *arg)
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "1");
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, " ");
|
||||
#else
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "0");
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "hide");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void HTTPPrint_ifc_mq2(char *VarData, void *arg)
|
||||
static void HTTPPrint_hide_mqtt1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, " ");
|
||||
}
|
||||
|
||||
static void HTTPPrint_hide_mqtt2(char *VarData, void *arg)
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "1");
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, " ");
|
||||
#else
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "0");
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "hide");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void HTTPPrint_hide_lora(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, " ");
|
||||
}
|
||||
|
||||
static void HTTPPrint_hide_eth(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, " ");
|
||||
}
|
||||
|
||||
static void HTTPPrint_hide_wifi(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, " ");
|
||||
}
|
||||
|
||||
static void HTTPPrint_testvariable(char *VarData, void *arg)
|
||||
{
|
||||
static int counter = 1;
|
||||
|
|
@ -673,6 +727,14 @@ dyn_var_handler_t HANDLERS_ARRAY[] = {
|
|||
{ "gsmmac", sizeof("gsmmac") - 1, &HTTPPrint_gsmmac },
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
/*LORAWAN settings*/
|
||||
{ "lren", sizeof("lren") - 1, &HTTPPrint_lren },
|
||||
{ "lrstat", sizeof("lrstat") - 1, &HTTPPrint_lrstat },
|
||||
{ "lrdvid", sizeof("lrdvid") - 1, &HTTPPrint_lrdvid },
|
||||
{ "lrapid", sizeof("lrapid") - 1, &HTTPPrint_lrapid },
|
||||
{ "lrapkey", sizeof("lrapkey") - 1, &HTTPPrint_lrapkey },
|
||||
#endif
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
/*MQTT*/
|
||||
{ "mqen1", sizeof("mqen1") - 1, &HTTPPrint_mqen1 },
|
||||
|
|
@ -706,8 +768,13 @@ dyn_var_handler_t HANDLERS_ARRAY[] = {
|
|||
/*ERROR report*/
|
||||
{ "status_fail", sizeof("status_fail") - 1, &HTTPPrint_status_fail },
|
||||
|
||||
{ "ifc_gprs", sizeof("ifc_gprs") - 1, &HTTPPrint_ifc_gprs },
|
||||
{ "ifc_mq2", sizeof("ifc_mq2") - 1, &HTTPPrint_ifc_mq2 },
|
||||
{ "hide_eth", sizeof("hide_eth") - 1, &HTTPPrint_hide_eth },
|
||||
{ "hide_wifi", sizeof("hide_wifi") - 1, &HTTPPrint_hide_wifi },
|
||||
{ "hide_lora", sizeof("hide_lora") - 1, &HTTPPrint_hide_lora },
|
||||
{ "hide_gprs", sizeof("hide_gprs") - 1, &HTTPPrint_hide_gprs },
|
||||
{ "hide_mqtt1", sizeof("hide_mqtt1") - 1, &HTTPPrint_hide_mqtt1 },
|
||||
{ "hide_mqtt2", sizeof("hide_mqtt2") - 1, &HTTPPrint_hide_mqtt2 },
|
||||
|
||||
{ "testvariable", sizeof("testvariable") - 1, &HTTPPrint_testvariable },
|
||||
|
||||
};
|
||||
|
|
|
|||
195
src/LoRaWANTransport.c
Normal file
195
src/LoRaWANTransport.c
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
/* 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 name: LoRaWANTransport.c
|
||||
* Project: MStation2Firmware
|
||||
* Created on: 2022-12-17
|
||||
* Author: bogd
|
||||
* Description:
|
||||
*/
|
||||
|
||||
#include "ttn.h"
|
||||
#include "driver/spi_common.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "Helpers.h"
|
||||
#include "SystemConfiguration.h"
|
||||
#include "NetTransport.h"
|
||||
#include "webguiapp.h"
|
||||
|
||||
// Pins and other resources
|
||||
/*Defined in global configuration*/
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
|
||||
#define TTN_SPI_HOST CONFIG_SPI_HOST
|
||||
#define TTN_PIN_NSS CONFIG_LORA_SPI_CS_GPIO
|
||||
#define TTN_PIN_RXTX TTN_NOT_CONNECTED
|
||||
#define TTN_PIN_RST TTN_NOT_CONNECTED
|
||||
#define TTN_PIN_DIO0 CONFIG_LORA_DIO0_GPIO
|
||||
#define TTN_PIN_DIO1 CONFIG_LORA_DIO1_GPIO
|
||||
|
||||
#define LORAWAN_DELIVERY_RETRY_PERIOD 10
|
||||
#define MESSAGE_LENGTH 32
|
||||
#define TAG "LoRaWANApp"
|
||||
#define LORAWAN_APP_LOG_ENABLED 1
|
||||
#define LORAWAN_MESSAGE_BUFER_LENTH 32
|
||||
|
||||
static const int LORA_WAIT_DELIVERY_BIT = BIT2;
|
||||
|
||||
QueueHandle_t LORAMessagesQueueHandle;
|
||||
static StaticQueue_t xStaticLoRaMessagesQueue;
|
||||
uint8_t LoRaMessagesQueueStorageArea[LORAWAN_MESSAGE_BUFER_LENTH
|
||||
* sizeof(DATA_SEND_STRUCT)];
|
||||
|
||||
esp_err_t LORASendData(DATA_SEND_STRUCT *pdss)
|
||||
{
|
||||
char *ptr = (char*) malloc(MESSAGE_LENGTH);
|
||||
if (ptr)
|
||||
{
|
||||
memcpy(ptr, pdss->raw_data_ptr, MESSAGE_LENGTH);
|
||||
DATA_SEND_STRUCT DSS;
|
||||
DSS.raw_data_ptr = ptr;
|
||||
DSS.data_length = MESSAGE_LENGTH;
|
||||
|
||||
if (xQueueSend(LORAMessagesQueueHandle, &DSS,
|
||||
pdMS_TO_TICKS(1000)) == pdPASS)
|
||||
return ESP_OK;
|
||||
else
|
||||
{
|
||||
free(ptr);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
}
|
||||
else
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
void messageReceived(const uint8_t *message, size_t length, ttn_port_t port)
|
||||
{
|
||||
#if LORAWAN_APP_LOG_ENABLED == 1
|
||||
if (length == MESSAGE_LENGTH && port == 1)
|
||||
{
|
||||
char P[MESSAGE_LENGTH * 2 + 1];
|
||||
P[MESSAGE_LENGTH * 2] = 0x00;
|
||||
BytesToStr((unsigned char*) message, (unsigned char*) P, length);
|
||||
ESP_LOGI(TAG, "Received=%s", P);
|
||||
}
|
||||
#endif
|
||||
//TODO Here registered from application handler must be called
|
||||
|
||||
}
|
||||
|
||||
void LoRaWANTransportTask(void *pvParameter)
|
||||
{
|
||||
DATA_SEND_STRUCT DSS;
|
||||
while (!LORAMessagesQueueHandle)
|
||||
vTaskDelay(pdMS_TO_TICKS(300)); //wait for LORA queue ready
|
||||
while (1)
|
||||
{
|
||||
while (!ttn_is_connected())
|
||||
vTaskDelay(pdMS_TO_TICKS(300));
|
||||
|
||||
if (ttn_is_provisioned())
|
||||
{
|
||||
xEventGroupClearBits(transport_event_group, LORA_WAIT_DELIVERY_BIT);
|
||||
xQueuePeek(LORAMessagesQueueHandle, &DSS, portMAX_DELAY);
|
||||
|
||||
#if LORAWAN_APP_LOG_ENABLED == 1
|
||||
char P[MESSAGE_LENGTH * 2 + 1];
|
||||
BytesToStr((unsigned char*) DSS.raw_data_ptr, (unsigned char*) P, MESSAGE_LENGTH);
|
||||
P[MESSAGE_LENGTH * 2] = 0x00;
|
||||
ESP_LOGI(TAG, "Sent=%s", P);
|
||||
#endif
|
||||
ttn_transmit_message((const uint8_t*) DSS.raw_data_ptr, MESSAGE_LENGTH, 1, true);
|
||||
xQueueReceive(LORAMessagesQueueHandle, &DSS, 0);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
#if LORAWAN_APP_LOG_ENABLED == 1
|
||||
ESP_LOGW(TAG, "Transmit fail, transport not ready");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LoRaWANRejoin(void)
|
||||
{
|
||||
ttn_rejoin();
|
||||
}
|
||||
|
||||
void LoRaWANInitJoinTask(void *pvParameter)
|
||||
{
|
||||
LORAMessagesQueueHandle = NULL;
|
||||
if (GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled)
|
||||
LORAMessagesQueueHandle = xQueueCreateStatic(LORAWAN_MESSAGE_BUFER_LENTH,
|
||||
sizeof(DATA_SEND_STRUCT),
|
||||
LoRaMessagesQueueStorageArea,
|
||||
&xStaticLoRaMessagesQueue);
|
||||
|
||||
ttn_init();
|
||||
// Configure the SX127x pins
|
||||
ttn_configure_pins(TTN_SPI_HOST, TTN_PIN_NSS, TTN_PIN_RXTX, -1,
|
||||
TTN_PIN_DIO0,
|
||||
TTN_PIN_DIO1);
|
||||
|
||||
char devEui[17], appEui[17], appKey[33];
|
||||
BytesToStr((unsigned char*) &GetSysConf()->lorawanSettings.DevEui,
|
||||
(unsigned char*) devEui,
|
||||
8);
|
||||
BytesToStr((unsigned char*) &GetSysConf()->lorawanSettings.AppEui,
|
||||
(unsigned char*) appEui,
|
||||
8);
|
||||
BytesToStr((unsigned char*) &GetSysConf()->lorawanSettings.AppKey,
|
||||
(unsigned char*) appKey,
|
||||
16);
|
||||
|
||||
// Register callback for received messages
|
||||
ttn_on_message(messageReceived);
|
||||
|
||||
while (!ttn_join(devEui, appEui, appKey))
|
||||
{
|
||||
};
|
||||
|
||||
xTaskCreate(LoRaWANTransportTask, "LoRaWANTransportTask", 1024 * 4,
|
||||
(void*) 0,
|
||||
3,
|
||||
NULL);
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void LoRaWANStart(void)
|
||||
{
|
||||
#ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
xTaskCreate(LoRaWANInitJoinTask, "LoRaWANInitJoinTask", 1024 * 4, (void*) 0,
|
||||
3,
|
||||
NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool isLORAConnected(void)
|
||||
{
|
||||
#ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
return ttn_is_connected();
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
|
@ -109,6 +109,14 @@ esp_err_t WebGuiAppInit(void)
|
|||
PPPModemStart();
|
||||
#endif
|
||||
|
||||
/*LoRaWAN start if enabled*/
|
||||
#if !CONFIG_TTN_LORA_FREQ_DISABLED
|
||||
if (GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled)
|
||||
{
|
||||
LoRaWANStart();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE
|
||||
/*Start Ethernet connection*/
|
||||
if (GetSysConf()->ethSettings.Flags1.bIsETHEnabled)
|
||||
|
|
@ -318,6 +326,19 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C
|
|||
Conf->sntpClient.Flags1.bIsWifiEnabled = DEFAULT_SNTP_WIFI_IS_ENABLED;
|
||||
Conf->sntpClient.Flags1.bIsGlobalEnabled = DEFAULT_SNTP_GLOBAL_ENABLED;
|
||||
Conf->sntpClient.TimeZone = DEFAULT_SNTP_TIMEZONE;
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
Conf->lorawanSettings.Flags1.bIsLoRaWANEnabled = true;
|
||||
Conf->Flags1.bIsLoRaConfirm = false;
|
||||
unsigned char temp[16] = { 0 };
|
||||
GetChipId((uint8_t*) temp + 4);
|
||||
memcpy(Conf->lorawanSettings.DevEui, temp, 8);
|
||||
StrToBytes((unsigned char*) CONFIG_LORA_APP_KEY, temp);
|
||||
memcpy(Conf->lorawanSettings.AppKey, temp, 16);
|
||||
StrToBytes((unsigned char*) CONFIG_LORA_APP_ID, temp);
|
||||
memcpy(Conf->lorawanSettings.AppEui, temp, 8);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user