当前位置:网站首页>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
边栏推荐
- Chapter 6 uses Matplotlib to draw thermodynamic diagram
- Summary of LSF usage
- Self taught programming, don't read theory books foolishly, programmer: it's all left over by others
- Garlic customer: equilateral triangle (DFS)
- Counting garlic guest: the solution of the equation
- [course summary] Hello harmonyos series of courses, take you to zero foundation introduction
- Slow response of analysis API
- New functions of ai2022, introduction to new functions of illustrator 2022
- Soatest preliminary understanding
- Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
猜你喜欢

Custom numeric input control

(product resources) mingdeyang ad8488 module high performance digital X-ray FMC interface 128 analog channel high-speed ADC chip

Technology cloud report: cloud computing has entered the "second half". Where is the way out for domestic cloud?

Vscode + PHP debug + namespace guidelines

科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
![[registration] tf54: engineer growth map and excellent R & D organization building](/img/c6/ae2c6427bd6af764ac614d59be54db.png)
[registration] tf54: engineer growth map and excellent R & D organization building

Unrelated interprocess communication -- creation and use of named pipes

DFS parity pruning

gin -get请求的小示例2-Handle处理post请求

Solve the problem when installing MySQL
随机推荐
Text justify, orientation, combine text attributes
DFS parity pruning
Unity combines itextsharp to generate PDF preparation DLL
Gbase 8s客户端与服务器的通信
gin框架的学习--golang
Introduction to gbase 8s storage structure and space management
GBase 8s查询处理和优化
Jerry's CPU performance test [chapter]
Jerry's AI server [chapter]
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Gbase 8s 共享内存段删除
Jerry's CPU performance test [chapter]
安装mysql出问题求解决
2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation
Introduction to gbase 8s checkpoint
iTextSharp 页面设置
Slow response of analysis API
Gbase 8s fragment table management operation
哪些代码需要做单元测试?
Blocking granularity of gbase 8s concurrency control