XR806 客户端准备工作。
1、连接wifi
2、创建socket连接服务器。
3、创建终端接收数据线程。
wifi_connect.c
#include <stdio.h>
#include <string.h>
#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "kernel/os/os.h"
int wifi_device_connect(const char *ssid, const char *psk_key)
{
const char *ssid_want_connect = ssid;
const char *psk = psk_key;
printf("\r\n=========== Connect Test Start ===========\r\n");
if (WIFI_SUCCESS != EnableWifi()) {
printf("Error: EnableWifi fail.\r\n");
return -1;
}
printf("EnableWifi Success.\r\n");
if (WIFI_STA_ACTIVE == IsWifiActive())
printf("Wifi is active.\r\n");
OS_Sleep(1);
if (WIFI_SUCCESS != Scan()) {
printf("Error: Scan fail.\r\n");
return -1;
}
printf("Wifi Scan Success.\r\n");
OS_Sleep(1);
WifiScanInfo scan_results[30];
unsigned int scan_num = 30;
if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) {
printf("Error: GetScanInfoList fail.\r\n");
return -1;
}
WifiDeviceConfig config = { 0 };
int netId = 0;
int i;
for (i = 0; i < scan_num; i++) {
if (0 == strcmp(scan_results[i].ssid, ssid_want_connect)) {
memcpy(config.ssid, scan_results[i].ssid,
WIFI_MAX_SSID_LEN);
memcpy(config.bssid, scan_results[i].bssid,
WIFI_MAC_LEN);
strcpy(config.preSharedKey, psk);
config.securityType = scan_results[i].securityType;
config.wapiPskType = WIFI_PSK_TYPE_ASCII;
config.freq = scan_results[i].frequency;
break;
}
}
if (i >= scan_num) {
printf("Error: No found ssid in scan_results\r\n");
return -1;
}
printf("GetScanInfoList Success.\r\n");
if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) {
printf("Error: AddDeviceConfig Fail\r\n");
return -1;
}
printf("AddDeviceConfig Success.\r\n");
if (WIFI_SUCCESS != ConnectTo(netId)) {
printf("Error: ConnectTo Fail\r\n");
return -1;
}
printf("ConnectTo Success\r\n");
OS_Sleep(3);
WifiLinkedInfo get_linked_res;
if (WIFI_SUCCESS != GetLinkedInfo(&get_linked_res)) {
printf("Error: GetLinkedInfo Fail\r\n");
return -1;
}
printf("GetLinkedInfo Success.\r\n");
printf("ssid: %s\r\n", get_linked_res.ssid);
printf("bssid: ");
for (int j = 0; j < WIFI_MAC_LEN; j++) {
printf("%02X", get_linked_res.bssid[j]);
}
printf("\r\n");
printf("rssi: %d\r\n", get_linked_res.rssi);
unsigned char get_mac_res[WIFI_MAC_LEN];
if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
printf("Error: GetDeviceMacAddress Fail\r\n");
return -1;
}
printf("GetDeviceMacAddress Success.\r\n");
for (int j = 0; j < WIFI_MAC_LEN - 1; j++) {
printf("%02X:", get_mac_res[j]);
}
printf("%02X\r\n", get_mac_res[WIFI_MAC_LEN - 1]);
return 0;
}
tcp_socket.c
创建发送(send_task)和接收线程(read_task)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lwip/sockets.h"
#include "ohos_init.h"
#include "kernel/os/os.h"
int _PROT_ = 6666;
char _TCP_SERVER_IP[] = "192.168.1.109";
int sock;
int ret;
struct sockaddr_in server_addr;
char send_buffer[50] = "Hello RX806 Open_Harmany!\r\n";
char recv_buffer[50];
extern OS_Semaphore_t tcp_wd;
static OS_Thread_t send_thread;
static OS_Thread_t read_thread;
void send_task(void *argv){
while(1){
if(OS_SemaphoreWait(&tcp_wd,OS_WAIT_FOREVER)==OS_OK)
{
printf("OS_SemaphoreWait\r\n");
if((ret = send(sock,send_buffer,strlen(send_buffer),0)) == -1){
printf("send_error\r\n");
}
bzero(&send_buffer,sizeof(send_buffer));
}
OS_MSleep(50);
}
}
void read_task(void *argv){
while(1){
if((ret = recv(sock,recv_buffer,sizeof(recv_buffer),0)) != -1){
printf("recv:\r\n%s,ret=%d\r\n",recv_buffer,ret);
bzero(&recv_buffer,sizeof(recv_buffer));
}else{
printf("recv_error\r\n");
}
OS_MSleep(1000);
}
}
int tcp_demo_test(){
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket_error\r\n");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(_PROT_);
server_addr.sin_addr.s_addr = inet_addr(_TCP_SERVER_IP);
if(connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
printf("connect_error\r\n");
return -1;
}
printf("connect_server_success\r\n");
if (OS_ThreadCreate(&send_thread, "send_task", send_task, NULL,
6, 2 * 1024) != OS_OK) {
printf("[ERR] send_task Failed\n");
}
printf(" send_task \n");
if (OS_ThreadCreate(&read_thread, "read_task", read_task, NULL,
6, 2 * 1024) != OS_OK) {
printf("[ERR] read_task Failed\n");
}
printf(" read_task \n");
}
main.c
void consol_task(void *argv){
int ret;
char uart_buffer[UART_RECEIVE_DATALEN];
IoTFlashInit();
while(1){
/*读取串口接收到的数据*/
ret = IoTUartRead(0, uart_buffer, UART_RECEIVE_DATALEN);
if(ret!=0){
/*指令*/
if(strncmp(uart_buffer,"IP:",3)==0)
{
// if(uart_buffer[3] == '?')
// {
// }else{
// printf("IP:%s\r\n",uart_buffer+3);
// }
}
/*从服务器获得当前时间*/
else if(strncmp(uart_buffer,"time:",5)==0)
{
printf("time:%s\r\n",uart_buffer+5);
memcpy(send_buffer,uart_buffer,strlen(uart_buffer));
if(OS_SemaphoreRelease(&tcp_wd)!=OS_OK){
printf("OS_SemaphoreRelease_error\r\n");
}
}
/*发送数据到服务器*/
else if(strncmp(uart_buffer,"send:",5)==0)
{
printf("send:%s\r\n",uart_buffer+5);
memcpy(send_buffer,uart_buffer+5,strlen(uart_buffer)-5);
if(OS_SemaphoreRelease(&tcp_wd)!=OS_OK){
printf("OS_SemaphoreRelease_error\r\n");
}
}
/* wifi SSID以及PSK重置 并复位连接
指令格式: wifi:@:@SSID@:@PSK
*/
else if(strncmp(uart_buffer,"wifi:",5)==0){
if(uart_buffer[5] == '?'){//wifi:? 回复当前
printf("connect_wifi_ssid:%s,connect_wifi_psk:%s\r\n",connect_wifi_ssid,connect_wifi_psk);
}else{
bzero(connect_wifi_psk,sizeof(connect_wifi_psk));
bzero(connect_wifi_ssid,sizeof(connect_wifi_ssid));
printf("DisableWifi Success\n");
Find_string((char*)uart_buffer,"@:@","@:@",connect_wifi_ssid);
strcpy(connect_wifi_psk,strstr(strstr(uart_buffer, "@:@")+3,"@:@")+3);
printf("reconnect_wifi_ssid:%s,connect_wifi_psk:%s\r\n",connect_wifi_ssid,connect_wifi_psk);
if(OS_SemaphoreRelease(&wifi_reconnect)!=OS_OK){
printf("OS_SemaphoreRelease_wifi_reconnect_error\r\n");
}
/*wifi wifi SSID以及PSK存储到ROM*/
IotFlash_write(0x100000 ,connect_wifi_ssid,strlen(connect_wifi_ssid)+1);
IotFlash_write(0x100000 + 0x4000,connect_wifi_psk,strlen(connect_wifi_psk)+1);
/*重启*/
RebootDevice(0);
printf("reboot_reset_wifi\r\n");
}
}else{
printf("command error,please again! \r\n%s\n",uart_buffer);
}
bzero(uart_buffer,sizeof(uart_buffer));
}
OS_MSleep(10);
}
}
注意:如果自己定义使用UART0接收数据时,需要注释原有的终端接收#“adapter/console:app_console”,
效果:
TCP数据通信:
串口助手发送:time:? 接收获取当前时间:now datetime: 2021-12-24 23:19:55,ret=33
串口助手发送:send:hello_XR806_OPEHARMANY! 服务端接收到:hello_XR806_OPEHARMANY!
服务端发送:hello world! 客户端接收:hello world!,ret=12
wifi的SSID和PSK获取以及重启
发送wifi:? 获取此时连接wifi的SSID和PSK
发送wifi:@:@hello@:@89898989 连接SSID为hello的新wifi
以上具体代码以及服务端代码已经上传到 gitee
补充:
1、每个工程下都有对应的BUILD.gn文件(业务),用来告诉编译器工程需要用到的头文件的路径;同样的,该工程外也有一个BUILD.gn文件(模块),用来告诉编译器你要编译哪个工程;该目录外还有一个BUILD.gn文件(模块),作用也是大同小异。
2、每个人工程下static_library的命名必须以app_的前缀命名。
出现的问题:
1、hb build -f 没有反应
需要安装对应的工具,执行 python3 -m pip install --user ohos-build (首先你得有python3的环境)
相关环境配置
2、使用git时,出现以下错误
error: server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none·
说明证书校验有问题。执行export GIT_SSL_NO_VERIFY=1 可以解决
3、烧写程序一直失败。可以尝试 设置 ,勾上红色花圈部分。
4、当XR806处于连接wifi时,尝试断开wifi,连接(不是断电重启)其他wifi,此时就要重新设置SSID和PSK并初始化wifi,但是一直连接不上,我猜测传入到底层SSID和PSK值是常量,无法被修改,后面我就没往底层深究。为了实现这个功能,我利用了内部ROM空间,将SSID和PSK值存入到ROM中(掉电不丢失),再进行软件复位,通过串口输入SSID和PSK,就能成功连接到对应wifi。
如有误,请微信群联系我,谢谢!