当前位置:网站首页>W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
2022-04-23 01:39:00 【Mr. Zhao】
This is the catalogue
This article uses the environment :
master control :W800-KIT ( Development board )
compatible :W800 W801 AIR101
development environment :CDK
SDK:W801/W800 Of SDK(tls library )
My lianshengde Q & a community homepage
above :
W801/W800-wifi-socket Development ( One )-UDP
W801 Bluetooth transceiver data and control design ( One )-INDICATE
W801 Bluetooth transceiver data and control design ( Two )-NOTIFY The way
this paper github engineering
This code contains some of the previous functions , So it may be messy .
Write it at the front :
This code has many places BUG, If you have any problems, please contact me to modify . For example, the transmitted data is not strictly screened , You have to re-enter your account and password every time , Data can be written to flash.... Because it's just a basic learning idea , So the code is not perfect .
One 、 Description of project
^^^^ Program function : Input via Bluetooth of mobile phone wifi Connect your password and account to the router , Use the development board I Connect the server on the computer side (UDP agreement , Use the network debugging assistant to simulate ), To transmit data . At the same time, the mobile terminal supports reconnection and stop connection .
^^^^ This paper is based on the above . We need to learn from the previous configuration ...
Two 、 Project design
1、 Overall design of the project
^^^^ The overall process is as follows . The instruction string must be strictly executed , The program only judges the first four characters of each string and selects the State .
Connect WIFI The format of the instruction is :conn+ account number + password . such as :conn+yyds+1234567890

Be careful : The above process is only the function used in this design , It's not the flow chart of the whole project .
3、 ... and 、 Programming
1、 Main task design
^^^^ The main task mainly creates two tasks , One for Bluetooth information analysis , One for the wifi Connection and data transmission .
//add by zxx satrt
// Create tasks
void My_task(void)
{
// Bluetooth receive message queue
if(tls_os_queue_create(&ble_q, 32)!=TLS_OS_SUCCESS)
{
printf("create queue fail\n");
return;
}
//wifi Connect to message queuing
if(tls_os_queue_create(&ble_wifi_q, 32)!=TLS_OS_SUCCESS)
{
printf("create queue fail\n");
return;
}
tls_os_task_create(NULL, NULL,
my_ble_msg_task, // Bluetooth receives tasks
NULL,
(void *)MyBLETaskStk, /* task's stack start address */
MYBLE_TASK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
MYBLE_TASK_PRIO,
0);
tls_os_task_create(NULL, NULL,
my_ble_wifi_task, //wifi Connect tasks
NULL,
(void *)MyBLEWIFITaskStk, /* task's stack start address */
MYBLE_TASK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
MYBLE_TASK_PRIO,
0);
}
//add by zxx end
2、 Bluetooth data analysis
^^^^ First, re analyze the data received by Bluetooth , This is because you have to deal with strings , So no more single byte processing , open gatt_svr_chr_demo_access_func() function , Make the following changes :
static int
gatt_svr_chr_demo_access_func(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg)
{
int i = 0;
struct os_mbuf *om = ctxt->om;
switch (ctxt->op) {
case BLE_GATT_ACCESS_OP_WRITE_CHR:
while(om) {
if(g_ble_uart_output_fptr)
{
g_ble_uart_output_fptr((uint8_t *)om->om_data, om->om_len);
}else
{
//add by zxx start
//print_bytes(om->om_data, om->om_len);
// The first byte is the length , Need to add a '\0', So byte length plus one
ble_data[0] = om->om_len+1;
// Copy the others as they are
memcpy(&ble_data[1],om->om_data,om->om_len);
printf("rec: %s len:%d\n",om->om_data,om->om_len);
// Add string end symbol
ble_data[om->om_len+1] = '\0';
if(om->om_len>0)
tls_os_queue_send(ble_q,ble_data, 0);
//add by zxx end
}
om = SLIST_NEXT(om, om_next);
}
return 0;
default:
assert(0);
return BLE_ATT_ERR_UNLIKELY;
}
}
^^^^ Define a Global variables , be used for wifi Reconnection of , The mobile terminal can send messages , Change the State :
// Global variables , Express wifi Reconnection status of ,0 Is normal ,1 Indicates reconnection
u8 wifi_reconnect_state = 0;
^^^^ Bluetooth information analysis function
// Define four states
#define MY_BLE_WIFI_STATE_START 1 // Start connecting
#define MY_BLE_WIFI_STATE_STOP 2 // Stop connecting
#define MY_BLE_WIFI_STATE_RECONNECT 3 // Reconnect the
#define MY_BLE_WIFI_STATE_CONNECT 4 // Connect wifi, And execute the sending program
void my_ble_msg_task(void *sdata)
{
u8 msg_state[4];
u8 ble_wifi_state = 0;
u8 *msg;
demo_bt_enable();
while(bt_adapter_state == WM_BT_STATE_OFF)
{
tls_os_time_delay(5000 /HZ);
}
tls_os_time_delay(5000 /HZ);
demo_ble_server_on();
printf("ble ready ok \r\n");
while(1)
{
// Receive data sent by mobile phone , Note that data is received in bytes
//msg[0] Indicates that the data length already contains '\0'
tls_os_queue_receive(ble_q,&msg, 0, 0);
printf("rev main:%s len:%d\n",&msg[1] ,msg[0]);
// Before extraction 4 position , The first four digits are status marks
strncpy(msg_state,&msg[1],4);
// Judge the status of the top four
if(!strncmp(msg_state,"star",4)) ble_wifi_state = MY_BLE_WIFI_STATE_START;
else if(!strncmp(msg_state,"stop",4)) ble_wifi_state = MY_BLE_WIFI_STATE_STOP;
else if(!strncmp(msg_state,"reco",4)) ble_wifi_state = MY_BLE_WIFI_STATE_RECONNECT;
else if(!strncmp(msg_state,"conn",4))
{
//conn The command should at least be "conn+s+p'\0'" Nine bytes
if(msg[0] < 9) // Less than 9 It means that the command is wrong
{
printf("conn err...\n");
tls_ble_server_demo_api_send_notify_msg("conn err...",sizeof("conn err..."));
ble_wifi_state = 0;
}
else
ble_wifi_state = MY_BLE_WIFI_STATE_CONNECT;
}
else ble_wifi_state = 0;
switch(ble_wifi_state)
{
case MY_BLE_WIFI_STATE_CONNECT:
tls_ble_server_demo_api_send_notify_msg("\'conn+ssid+pwd\'",sizeof("\'conn+ssid+pwd\'"));
wifi_reconnect_state = 0; // Reset
tls_os_queue_send(ble_wifi_q,&msg[6], 0);
break;
case MY_BLE_WIFI_STATE_STOP:
tls_ble_server_demo_api_send_notify_msg("stop connect",sizeof("stop connect"));
wifi_reconnect_state = 1; // disconnect
break;
case MY_BLE_WIFI_STATE_RECONNECT:
tls_ble_server_demo_api_send_notify_msg("start reconnect",sizeof("start reconnect"));
wifi_reconnect_state = 1; // disconnect
break;
// Only send messages to the mobile terminal
case MY_BLE_WIFI_STATE_START:
tls_ble_server_demo_api_send_notify_msg("ready connect",sizeof("ready connect"));
break;
default:
printf("ble_wifi_state is err \n");
break;
}
}
}
3、wifi Connect
^^^^ The connection function is relatively simple , Use the message queue to wait for the data sent by the Bluetooth parsing task , After correct reception , Enter the server connection , And send data circularly . After receiving the reconnection command , Will update wifi_reconnect_state, sign out while Sender , When reconnecting , must closesocket, Otherwise, the port cannot be bound normally .
void my_ble_wifi_task(void *sdata)
{
// An array of passwords and accounts , In fact, you can define pointers here and use malloc Flexible application , It is convenient for me to define the fixed length directly
u8 ssid[50];
u8 pwd[50];
// Test send array
u8 test_data[10] = {
0,1,2,3,4,5,6,7,8,9};
// Message queue reception
u8 *msg;
while(1)
{
printf("wait connect..\n");
// Must close socket, Otherwise, the port cannot be bound normally
close_udp_socket_demo();
// Message queue , Receive the account and password transmitted by Bluetooth resolution task
tls_os_queue_receive(ble_wifi_q,&msg, 0, 0);
printf("rev task:%s len:%d\n",msg,strlen(msg));
// The total length of the string Length without end character
u8 msg_len = strlen(msg);
u8 ssid_size = 0; // The length of the account Without closing characters
// The following cycle is mainly used to find the length of the account
for(int i=0; msg[i] != '\0'; i++)
{
if(msg[i] != '+')
ssid_size++;
else
break;
}
// Copy account name
memcpy(ssid,msg,ssid_size);
// Add end character
ssid[ssid_size] = '\0';
// Copy password , Be careful to exclude the plus sign
memcpy(pwd,&msg[ssid_size+1],msg_len-ssid_size-1); // Get rid of + Number
// Add the closing character at the end
pwd[msg_len-ssid_size-1] = '\0';
printf("ssid:%s len:%d pwd:%s len:%d \n",ssid,strlen(ssid),pwd,strlen(pwd));
// Connect WIFI
demo_connect_net(ssid,pwd);
// Time delay
tls_os_time_delay(3000);
// Connect to server
socket_udp_demo(1,10086,"192.168.1.87");
// Normally, send the program , When wifi_reconnect_state by 1 when , Indicates reconnection
while(0 == wifi_reconnect_state)
{
udp_send_data_self(test_data,10);
tls_os_time_delay(500);
}
}
}
^^^^ In the above procedure close_udp_socket_demo() For custom functions , stay wm_udp_demo.c In file .
void close_udp_socket_demo(void)
{
printf("close udp socket \n");
closesocket(demo_udp->socket_num);
}
^^^^ At the same time wm_demo_console.h Affirming .
extern void udp_send_data_self(u8 *data,int data_len);
extern void close_udp_socket_demo(void);
#endif /*__WM_DEMO_CMD_H__*/
Four 、 test
^^^^ Set the following sending instructions on the mobile phone in advance .

^^^^ Cell phone Bluetooth APP as follows :

4.1、 Connect the test
^^^^ Download the program to the development board , Click on Account + password . The program runs normally , Distribution network success , And send data to the server normally .

The server

4.2 Reconnection test
^^^^ Click on Reconnection The rear development board is disconnected , And wait for a new connection .

^^^^ Send... Again Account + password Instructions .

4.3 Stop test
^^^^ Click on stop it , The development board is disconnected

4.4 Power consumption test

版权声明
本文为[Mr. Zhao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230136121237.html
边栏推荐
- Futr3d: a unified 3D detection framework for sensor fusion
- Summary of commonly used commands of LSF
- Deployment of mask detection flash for yolov5
- Solve the problem when installing MySQL
- 数字藏品平台入驻 数字藏品平台开发 数字藏品SaaS平台
- gin -get请求的小示例2-Handle处理post请求
- Redis实现分布式锁
- .NET单元测试第一篇:常见.NET单元测试框架有哪些?
- LSF的常用使用命令总结
- Gbase 8s shared memory segment deletion
猜你喜欢

Innovative practice of short video content understanding and generation technology in meituan

Unrelated interprocess communication -- creation and use of named pipes

VSCODE + PHP Debug + 名字空间指引

代码实现发邮件---sendemails

GBase 8s 备份介绍

FL studio20.8最新中文版本安装下载图文教程

gin--hello

Soatest preliminary understanding

Small example of gin - get request 2-handle handles post requests

彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
随机推荐
JSP基础知识总结
NR polar code VII - SCL (successful cancellation list coding)
Introduction à la structure de stockage de gbase 8S et à la gestion de l'espace
Prince saves Princess (DFS)
GBASE 8s分片表管理操作
Use yolov4 on colab
.NET(C#) MySQL conn.Open()报错:SSL Connection error的解决方法
The most understandable life cycle of dependency injection
In the context of Internet plus, how can enterprises innovate customer service?
DFS奇偶性剪枝
UVC camera encapsulation class
Solve the problem when installing MySQL
Full Permutation (DFS and next_permutation solution)
Introduction and management of gbase 8s database log
Gbase 8s存儲結構簡介及空間管理
"Self abuse artifact" exploded overnight: control your face with a handle, take your own code, and bear the consequences
2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation
四级城市地区表 xlsx, sql文件,国内,中国省市县街道乡镇四级地址 (名称,联动ID,层级,是否末级(1-是))
Traversal of ladder race l2-6 tree
[course summary] Hello harmonyos series of courses, take you to zero foundation introduction