1402 views|3 replies

274

Posts

8

Resources
The OP
 

[RVB2601 Creative Application Development] Chapter 6: Building a TCP Client [Copy link]

 

The use of wifi was not smooth. The structure of the source code was very messy. I used the code jump function and found that the socket function jumped between the sal and socket header files, forming a loop. I couldn't find where it was implemented specifically, so I gave up.

Later, referring to the sharing of many forum friends, I used the functions in w800 to create a TCP client.

The creation process is as follows:

1. Connect to wifi

The entire code is modified under the "ch2601_webplayer_demo" routine. First, modify the ssid and password of your own wifi in "static void network_init()" in "init.c":

netmgr_config_wifi(app_netmgr_hdl, "ChinaNet-JvAb", strlen("ChinaNet-JvAb"), "s3fackxx", strlen("s3fackxx"));

2. Add a callback function for receiving data

The callback function is as follows:

void w800_in(int linkid, void *data, size_t len, char remote_ip[16], uint16_t remote_ports)
{
    uint8_t* d;
    uint8_t speed;
    d=(uint8_t *)data;
    printf("rev:%sn",data);
    // do sth. in here
}

Register the callback function. The registration location is also placed in "static void network_init()" in "init.c". The complete "static void network_init()" function is as follows:

static void network_init()
{
    w800_wifi_param_t w800_param;
    /* init wifi driver and network */
    w800_param.reset_pin      = PA21;
    w800_param.baud           = 1*1000000;
    w800_param.cs_pin         = PA15;
    w800_param.wakeup_pin     = PA25;
    w800_param.int_pin        = PA22;
    w800_param.channel_id     = 0;
    w800_param.buffer_size    = 4*1024;

    wifi_w800_register(NULL, &w800_param);
    app_netmgr_hdl = netmgr_dev_wifi_init();

    if (app_netmgr_hdl) {
        utask_t *task = utask_new("netmgr", 2 * 1024, QUEUE_MSG_COUNT, AOS_DEFAULT_APP_PRI);
        netmgr_service_init(task);
        netmgr_config_wifi(app_netmgr_hdl, "ChinaNet-JvAb", strlen("ChinaNet-JvAb"), "s3fackxx", strlen("s3fackxx"));
		w800_packet_input_cb_register(&w800_in);  // 注册回调函数
        netmgr_start(app_netmgr_hdl);
    }
}

3. Establish a TCP client after connecting to wifi

In "main.c" of "app_main.c", two events are registered, namely the events when the IP address is obtained and the event when the connection is disconnected.

int main(void)
{
    board_yoc_init();
    pwm_io_init();

    /* Subscribe */
    event_subscribe(EVENT_NETMGR_GOT_IP, network_event, NULL);
    event_subscribe(EVENT_NETMGR_NET_DISCON, network_event, NULL);  
}

In the event of obtaining an IP address, establish a TCP client

static void network_event(uint32_t event_id, const void *param, void *context)
{
    switch(event_id) {
    case EVENT_NETMGR_GOT_IP:
        LOGD(TAG, "net got ip");
		w800_connect_remote(0,2,REMOTE_IP,REMOTE_PORT); //连接到服务器
		aos_task_new("dht12_task",dht12_task,NULL,2*1024);  //新建线程用于处理数据
        break;
    case EVENT_NETMGR_NET_DISCON:
        LOGD(TAG, "net disconnect");
        break;
    }

    /*do exception process */
    app_exception_event(event_id);
}

void dht12_task(void *paras)
{
	char str[50];
	unsigned char buf[10];
	dht12_init();
	while(1){
		dht12_get_data(buf);
		sprintf(str,"temperature:%d.%d;humity:%d.%d;\n",buf[2],buf[3],buf[0],buf[1]);
		w800_send_data(str,strlen(str),1000); //使用此函数实现数据的发送
		aos_msleep(5000);
	}
}

Note that you need to add the header file "#include <devices/w800.h>". Use the function "int w800_send_data(const uint8_t *pdata, int len, int timeout)" to send data, and then combine it with the receiving callback function in step 2 to realize the task of TCP client.

Finally, I would like to thank the forum friends who have provided a lot of excellent experience sharing.

Latest reply

Is it difficult to connect to the Wi-Fi network?   Details Published on 2022-5-11 15:58
 
 

6818

Posts

11

Resources
2
 
It's done, keep going. Next step is to learn http

Comments

[attachimg]603623[/attachimg] I will give it a try after I finish the project  Details Published on 2022-5-4 08:59
 
 
 

274

Posts

8

Resources
3
 
lugl4313820 posted on 2022-5-3 19:48 Done, come on. Next step is to get http working

I will give it a try after I finish the project

 
 
 

149

Posts

0

Resources
4
 

Is it difficult to connect to the Wi-Fi network?

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list