Compare commits

...

3 Commits

Author SHA1 Message Date
bogd
897ee0fec2 extend test message content 2024-08-29 12:28:19 +02:00
bogd
479b8968e1 mqtt test message is wrapped into generic protocol 2024-08-28 16:13:47 +02:00
bogd
dcd659e5ea added serial port settings 2024-08-26 16:10:21 +02:00
6 changed files with 144 additions and 9 deletions

View File

@ -51,6 +51,9 @@ typedef int mqtt_app_err_t;
#define UPLINK_SUBTOPIC "UPLINK" // Device publish to this topic
#define DOWNLINK_SUBTOPIC "DWLINK" // Device listen from this topic
#define MQTT1 0
#define MQTT2 1
typedef struct
{
char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH];

View File

@ -204,6 +204,9 @@ typedef struct
{
int Serialmode;
int BaudRate;
int DataBits;
int Parity;
int StopBits;
int InputBrake;
struct
{

View File

@ -161,17 +161,107 @@ esp_err_t ExternalServiceMQTTSend(char *servname, char *data, int len, int idx)
}
#define MAX_ERROR_JSON 256
/*
mqtt_app_err_t PublicTestMQTT(int idx)
{
char tmp[10];
char resp[256];
char JSONMess[512];
struct jWriteControl jwc;
jwOpen(&jwc, JSONMess, MAX_ERROR_JSON, JW_OBJECT, JW_PRETTY);
time_t now;
time(&now);
jwObj_int(&jwc, "time", (unsigned int) now);
jwObj_string(&jwc, "event", "MQTT_TEST_MESSAGE)");
strcpy(resp, "mqtt://");
strcat(resp, GetSysConf()->mqttStation[idx].ServerAddr);
itoa(GetSysConf()->mqttStation[idx].ServerPort, tmp, 10);
strcat(resp, ":");
strcat(resp, tmp);
jwObj_string(&jwc, "url", resp);
ComposeTopic(resp, idx, SERVICE_NAME, UPLINK_SUBTOPIC);
jwObj_string(&jwc, "tx_topic", resp);
ComposeTopic(resp, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
jwObj_string(&jwc, "rx_topic", resp);
jwEnd(&jwc);
jwClose(&jwc);
mqtt_app_err_t merr = API_OK;
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
merr = API_INTERNAL_ERR;
return merr;
}
*/
mqtt_app_err_t PublicTestMQTT(int idx)
{
char tmp[10];
char resp[256];
char JSONMess[512];
char JSONMess[1024];
struct jWriteControl jwc;
jwOpen(&jwc, JSONMess, MAX_ERROR_JSON, JW_OBJECT, JW_PRETTY);
jwOpen(&jwc, JSONMess, 1024 - 64, JW_OBJECT, JW_COMPACT);
jwObj_object(&jwc, "data");
time_t now;
time(&now);
jwObj_int(&jwc, "time", (unsigned int) now);
jwObj_string(&jwc, "event", "MQTT_TEST_MESSAGE)");
jwObj_int(&jwc, "msgid", (unsigned int) now);
jwObj_string(&jwc, "srcid", GetSysConf()->ID);
jwObj_string(&jwc, "dstid", "FFFFFFFF");
char time[ISO8601_TIMESTAMP_LENGTH];
GetISO8601Time(time);
jwObj_string(&jwc, "time", time);
jwObj_int(&jwc, "msgtype", DATA_MESSAGE_TYPE_COMMAND);
jwObj_int(&jwc, "payloadtype", 1000);
jwObj_object(&jwc, "payload");
jwObj_int(&jwc, "applytype", 0);
jwObj_object(&jwc, "variables");
rest_var_types tp = VAR_STRING;
GetConfVar("dev_id", resp, &tp);
jwObj_string(&jwc, "dev_id", resp);
GetConfVar("ser_num", resp, &tp);
jwObj_string(&jwc, "ser_num", resp);
GetConfVar("model_name", resp, &tp);
jwObj_string(&jwc, "model_name", resp);
GetConfVar("hw_rev", resp, &tp);
jwObj_string(&jwc, "hw_rev", resp);
tp = VAR_FUNCT;
GetConfVar("time", resp, &tp);
jwObj_raw(&jwc, "time", resp);
GetConfVar("lat", resp, &tp);
jwObj_raw(&jwc, "lat", resp);
GetConfVar("lon", resp, &tp);
jwObj_raw(&jwc, "lon", resp);
GetConfVar("uptime", resp, &tp);
jwObj_raw(&jwc, "uptime", resp);
GetConfVar("free_ram", resp, &tp);
jwObj_raw(&jwc, "free_ram", resp);
GetConfVar("free_ram_min", resp, &tp);
jwObj_raw(&jwc, "free_ram_min", resp);
GetConfVar("def_interface", resp, &tp);
jwObj_raw(&jwc, "def_interface", resp);
GetConfVar("fw_rev", resp, &tp);
jwObj_raw(&jwc, "fw_rev", resp);
GetConfVar("idf_rev", resp, &tp);
jwObj_raw(&jwc, "idf_rev", resp);
#if CONFIG_WEBGUIAPP_WIFI_ENABLE
GetConfVar("wifi_stat", resp, &tp);
jwObj_raw(&jwc, "wifi_stat", resp);
GetConfVar("wifi_level", resp, &tp);
jwObj_raw(&jwc, "wifi_level", resp);
tp = VAR_INT;
GetConfVar("wifi_mode", resp, &tp);
jwObj_raw(&jwc, "wifi_mode", resp);
tp = VAR_STRING;
GetConfVar("wifi_ap_ssid", resp, &tp);
jwObj_string(&jwc, "wifi_ap_ssid", resp);
GetConfVar("wifi_sta_ssid", resp, &tp);
jwObj_string(&jwc, "wifi_sta_ssid", resp);
#endif
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
#endif
strcpy(resp, "mqtt://");
strcat(resp, GetSysConf()->mqttStation[idx].ServerAddr);
itoa(GetSysConf()->mqttStation[idx].ServerPort, tmp, 10);
@ -182,7 +272,28 @@ mqtt_app_err_t PublicTestMQTT(int idx)
jwObj_string(&jwc, "tx_topic", resp);
ComposeTopic(resp, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
jwObj_string(&jwc, "rx_topic", resp);
jwEnd(&jwc);
jwEnd(&jwc); //close variables
jwEnd(&jwc); //close payload
jwEnd(&jwc); //close data
//calculate sha from 'data' object
char *datap = strstr(JSONMess, "\"data\":");
if (datap)
{
datap += sizeof("\"data\":") - 1;
unsigned char sha[32 + 1];
unsigned char sha_print[32 * 2 + 1];
SHA256hmacHash((unsigned char*) datap, strlen(datap), (unsigned char*) "mykey", sizeof("mykey"), sha);
BytesToStr(sha, sha_print, 32);
sha_print[32 * 2] = 0x00;
#if REAST_API_DEBUG_MODE
ESP_LOGI(TAG, "SHA256 of DATA object is %s", sha_print);
#endif
jwObj_string(&jwc, "signature", (char*) sha_print);
}
else
return ESP_ERR_NOT_FOUND;
jwClose(&jwc);
mqtt_app_err_t merr = API_OK;
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
@ -212,9 +323,9 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED client %d", idx);
#endif
ComposeTopic(topic, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
msg_id = esp_mqtt_client_subscribe(client, (char*) topic, 0);
msg_id = esp_mqtt_client_subscribe(client, (char*) topic, 0);
#if MQTT_DEBUG_MODE > 0
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
ESP_LOGI(TAG, "Subscribe to %s", topic);
#endif

View File

@ -551,6 +551,11 @@ const rest_var_t SystemVariables[] =
{ 0, "serial_bridge", &SysConfig.serialSettings.Flags.IsBridgeEnabled, VAR_BOOL, RW, 0, 1 },
{ 0, "serial_mode", &funct_serial_mode, VAR_FUNCT, R, 1, 2 },
{ 0, "serial_baud", &SysConfig.serialSettings.BaudRate, VAR_INT, RW, 1200, 4096000 },
{ 0, "serial_bits", &SysConfig.serialSettings.DataBits, VAR_INT, RW, 0, 3 },
{ 0, "serial_parity", &SysConfig.serialSettings.Parity, VAR_INT, RW, 0, 3 },
{ 0, "serial_stop", &SysConfig.serialSettings.StopBits, VAR_INT, RW, 1, 3 },
{ 0, "serial_break", &SysConfig.serialSettings.InputBrake, VAR_INT, RW, 1, 50 },
{ 0, "serial_visible", (bool*) (&VAR_TRUE), VAR_BOOL, R, 0, 1 },
#else

View File

@ -231,13 +231,22 @@ void InitSerialPort(void)
{
uart_config_t uart_config = {
.baud_rate = GetSysConf()->serialSettings.BaudRate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.data_bits = UART_DATA_7_BITS,
.parity = UART_PARITY_EVEN,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
//uart_config.data_bits = (uint8_t) GetSysConf()->serialSettings.DataBits;
//uart_config.parity = (uint8_t) GetSysConf()->serialSettings.Parity;
//uart_config.stop_bits = (uint8_t) GetSysConf()->serialSettings.StopBits;
ESP_LOGI(TAG, "UART data_bits:%d parity:%d stop_bits:%d",
GetSysConf()->serialSettings.DataBits,
GetSysConf()->serialSettings.Parity,
GetSysConf()->serialSettings.StopBits);
ESP_ERROR_CHECK(
uart_driver_install(CONFIG_WEBGUIAPP_UART_PORT_NUM, CONFIG_WEBGUIAPP_UART_BUF_SIZE * 2, 0, 20, &uart_event_queue, 0));
ESP_ERROR_CHECK(uart_param_config(CONFIG_WEBGUIAPP_UART_PORT_NUM, &uart_config));

View File

@ -33,6 +33,7 @@
#include "driver/gpio.h"
#include "driver/adc.h"
#include "driver/i2c.h"
#include <driver/uart.h>
#include "romfs.h"
#include "spifs.h"
@ -425,6 +426,9 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C
#endif
Conf->serialSettings.Serialmode = 1;
Conf->serialSettings.BaudRate = CONFIG_WEBGUIAPP_UART_BAUD_RATE;
Conf->serialSettings.DataBits = UART_DATA_8_BITS;
Conf->serialSettings.Parity = UART_PARITY_DISABLE;
Conf->serialSettings.StopBits = UART_STOP_BITS_1;
Conf->serialSettings.InputBrake = 50;
#endif