优化了一下网络连接,

This commit is contained in:
小马_666 2025-06-10 17:22:59 +08:00
parent a26e8a73b5
commit b274562f53
4 changed files with 571 additions and 509 deletions

View File

@ -2,7 +2,7 @@
* @Author: mbw * @Author: mbw
* @Date: 2024-10-23 17:14:16 * @Date: 2024-10-23 17:14:16
* @LastEditors: mbw && 1600520629@qq.com * @LastEditors: mbw && 1600520629@qq.com
* @LastEditTime: 2025-06-06 08:48:12 * @LastEditTime: 2025-06-06 14:00:40
* @FilePath: \ble_bjq_ch303rct6_ml307\applications\main.c * @FilePath: \ble_bjq_ch303rct6_ml307\applications\main.c
* @Descrt_thread_ * @Descrt_thread_
* *
@ -302,7 +302,7 @@ int main(void)
LOG_D("工作时长:%d", work_duration); LOG_D("工作时长:%d", work_duration);
// 读取历史记录总数 // 读取历史记录总数
g_Calibration_status = Flash_Get_Calibration_State(); g_Calibration_status = Flash_Get_Calibration_State();
rt_thread_mdelay(100); rt_thread_mdelay(10);
} }
else else
{ {

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
* @Author: mbw * @Author: mbw
* @Date: 2024-11-30 15:46:21 * @Date: 2024-11-30 15:46:21
* @LastEditors: mbw && 1600520629@qq.com * @LastEditors: mbw && 1600520629@qq.com
* @LastEditTime: 2025-06-06 08:37:07 * @LastEditTime: 2025-06-06 14:02:27
* @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\bsp_ml307.c * @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\bsp_ml307.c
* @Description: * @Description:
* *
@ -26,7 +26,7 @@
#if IOT_MODULE_SWITCH == 1 #if IOT_MODULE_SWITCH == 1
#define ML307_THREAD_STACK_SIZE (4096 + 2048) #define ML307_THREAD_STACK_SIZE (4096 + 2048)
#define ML307_RECV_THREAD_STACK_SIZE (4096 + 2048) #define ML307_RECV_THREAD_STACK_SIZE (4096 + 2048)
#define ML307_LIFE_THREAD_STACK_SIZE (4096+1024) #define ML307_LIFE_THREAD_STACK_SIZE (4096 + 1024)
#define ML307_THREAD_PRIORITY 25 #define ML307_THREAD_PRIORITY 25
#define ML307_THREAD_TICKS 50 #define ML307_THREAD_TICKS 50
#define ML307_RECV_THREAD_PRIORITY 24 #define ML307_RECV_THREAD_PRIORITY 24
@ -305,25 +305,24 @@ int BSP_Ml307_Init (struct Ml307_Ops *ops, rt_uint8_t version)
#include <stdint.h> #include <stdint.h>
// 将十进制转换为BCD格式例如33 -> 0x33 // 将十进制转换为BCD格式例如33 -> 0x33
uint8_t dec_to_bcd(uint8_t dec) uint8_t dec_to_bcd (uint8_t dec)
{ {
return ((dec / 10) << 4) | (dec % 10); return ((dec / 10) << 4) | (dec % 10);
} }
int test (int argc, char **argv)
int test(int argc, char **argv)
{ {
uint8_t a = atoi(argv[1]); uint8_t a = atoi (argv[1]);
rt_uint8_t b = a; rt_uint8_t b = a;
LOG_D("in = 0x%02X, %d", a, a); LOG_D ("in = 0x%02X, %d", a, a);
b = a; b = a;
b = dec_to_bcd(b); b = dec_to_bcd (b);
LOG_D("in = 0x%02X, %d", b, b); LOG_D ("in = 0x%02X, %d", b, b);
return b; return b;
} }
MSH_CMD_EXPORT(test, "test");
MSH_CMD_EXPORT (test, "test");
int _Update_Valve_Data (struct Ml307_Ops *ops) int _Update_Valve_Data (struct Ml307_Ops *ops)
{ {
@ -338,7 +337,7 @@ int _Update_Valve_Data (struct Ml307_Ops *ops)
LOG_D ("valve_num: %d", ops->body->valve_num); LOG_D ("valve_num: %d", ops->body->valve_num);
rt_memcpy (&ops->body->valve_data[0], &valve[0], sizeof (struct valve_t)); rt_memcpy (&ops->body->valve_data[0], &valve[0], sizeof (struct valve_t));
// rt_memset(&valve[0].valve_connct_status, 0, (sizeof(struct valve_t) - 7)); // 清空阀门包数据,等待下次更新,如果没更新说明断开了 // rt_memset(&valve[0].valve_connct_status, 0, (sizeof(struct valve_t) - 7)); // 清空阀门包数据,等待下次更新,如果没更新说明断开了
ops->body->valve_data[0].valve_voltage = dec_to_bcd(ops->body->valve_data[0].valve_voltage); ops->body->valve_data[0].valve_voltage = dec_to_bcd (ops->body->valve_data[0].valve_voltage);
} }
return RT_EOK; return RT_EOK;
} }
@ -975,7 +974,7 @@ int Ml307_Process_Events (Ml307Event ml307_recv_event, struct at_device *device,
if (event) // 处理该事件 if (event) // 处理该事件
{ {
// 打印事件的名称 // 打印事件的名称
ml307_disconnect_retry_flag = 0;//有事件,则断连重试标志清空 ml307_disconnect_retry_flag = 0; // 有事件,则断连重试标志清空
ml307_current_evnet = event->event_flag; ml307_current_evnet = event->event_flag;
for (size_t i = 0; i < max_retry_count; i++) for (size_t i = 0; i < max_retry_count; i++)
{ {
@ -986,6 +985,7 @@ int Ml307_Process_Events (Ml307Event ml307_recv_event, struct at_device *device,
#ifdef TEST_ENABLE #ifdef TEST_ENABLE
RTC_ShowTime(); // 每次发送打印下时间,容易定位问题 RTC_ShowTime(); // 每次发送打印下时间,容易定位问题
#endif #endif
cnt++;
LOG_D ("%s上报, cnt :%d", event->event_name, cnt); LOG_D ("%s上报, cnt :%d", event->event_name, cnt);
int result = event->send_func (device, ml307_ops); int result = event->send_func (device, ml307_ops);
@ -1008,7 +1008,6 @@ int Ml307_Process_Events (Ml307Event ml307_recv_event, struct at_device *device,
{ {
// 收不到说明在平台未注册或者信号不行,此时ml307_connect_sever_flag 为0但ml307_conncet_tcp_flag = 1此时不发起重连等待事件触发就行 // 收不到说明在平台未注册或者信号不行,此时ml307_connect_sever_flag 为0但ml307_conncet_tcp_flag = 1此时不发起重连等待事件触发就行
ret = RT_ERROR; ret = RT_ERROR;
} }
} }
} }
@ -1049,12 +1048,12 @@ int Ml307_Process_Events (Ml307Event ml307_recv_event, struct at_device *device,
if (!ml307_disconnect_retry_flag) if (!ml307_disconnect_retry_flag)
{ {
LOG_D("与服务器断开,重连网络中"); LOG_D ("与服务器断开,重连网络中");
rt_sem_release (ml307_disconnect_sem); rt_sem_release (ml307_disconnect_sem);
} }
else else
{ {
LOG_D ("与服务区断开连接,等待连接中......\n"); LOG_D ("与服务区断开连接,等待连接中......\n");
} }
} }
} }
@ -1814,7 +1813,7 @@ static void Ml307_Send_Thread_Entry (void *param)
{ {
LOG_E ("ml307 send data failed result = [%d]\n", result); LOG_E ("ml307 send data failed result = [%d]\n", result);
} }
rt_thread_mdelay(5000); rt_thread_mdelay (5000);
Ml307_Send_Event (kMl307HeartbeatEvent); Ml307_Send_Event (kMl307HeartbeatEvent);
} }
else if (ml307_recv_event & Ml307_Get_Event_Flag (KMl307ValveDisconnectEvent)) else if (ml307_recv_event & Ml307_Get_Event_Flag (KMl307ValveDisconnectEvent))
@ -2020,12 +2019,38 @@ static void Ml307_Life_Thread_Entry (void *parameter)
result = -RT_ERROR; result = -RT_ERROR;
goto __exit; goto __exit;
} }
if (at_obj_exec_cmd(device->client, resp, "AT+CEREG=1") != RT_EOK) if (at_obj_exec_cmd (device->client, resp, "AT+CEREG=1") != RT_EOK)
{ {
result = -RT_ERROR; result = -RT_ERROR;
goto __exit; goto __exit;
} }
rt_uint8_t i = 0; rt_uint8_t i = 0;
/* check the GPRS network is registered */
for (i = 0; i < 10; i++)
{
if (at_obj_exec_cmd (device->client, resp, "AT+CEREG?") == RT_EOK)
{
int link_stat = 0;
if (at_resp_parse_line_args_by_kw (resp, "+CEREG:", "+CEREG: %*d,%d", &link_stat) > 0)
{
if ((link_stat == 1) || (link_stat == 5))
{
LOG_D ("%s device GPRS is registered", device->name);
break;
}
}
}
rt_thread_mdelay (1000);
}
if (i == 10)
{
LOG_E ("%s device GPRS is register failed", device->name);
result = -RT_ERROR;
goto __exit;
}
/* check SIM card */ /* check SIM card */
for (i = 0; i < 60; i++) for (i = 0; i < 60; i++)
{ {
@ -2072,7 +2097,28 @@ static void Ml307_Life_Thread_Entry (void *parameter)
result = -RT_ERROR; result = -RT_ERROR;
goto __exit; goto __exit;
} }
for (i = 0; i < 60; i++)
{
if (at_obj_exec_cmd (device->client, resp, "AT+CGACT=1") == RT_EOK)
{
LOG_D ("pdp激活成功");
break;
}
else
{
// AT+CGACT=0,1 //去激活 PDP
if (at_obj_exec_cmd (device->client, resp, "AT+CGACT=0") == RT_EOK)
{
LOG_D ("pdp去激活成功");
}
}
rt_thread_mdelay (5000);
}
if (i == 60)
{
result = -RT_ERROR;
goto __exit;
}
/* check the GPRS network is registered */ /* check the GPRS network is registered */
for (i = 0; i < 60; i++) for (i = 0; i < 60; i++)
{ {

View File

@ -134,7 +134,7 @@
#define RT_USING_AT #define RT_USING_AT
// #define NETDEV_USING_PING//用来监测和平台之间的延时 // #define NETDEV_USING_PING//用来监测和平台之间的延时
#ifdef ENABLE_LOG_ALL #ifdef ENABLE_LOG_ALL
// #define AT_DEBUG #define AT_DEBUG
#endif #endif
#define AT_USING_CLIENT #define AT_USING_CLIENT