ESP8266的WiFi异常速率问题

yuzu
Posts: 5
Joined: Sun Oct 06, 2019 2:34 pm

ESP8266的WiFi异常速率问题

Postby yuzu » Mon Oct 28, 2019 8:54 pm

我使用的是NodeMCU v1.0 模块,其中上面的WiFi模块为Doit的ESP8266MOD。

遇到了个很奇怪的问题。相同代码,正常情况下,ESP8266向电脑传输速率为100~200KB/s左右,但是
只要我用大拇指同时接触WiFi模块上的PCB天线和顶盖,ESP8266向电脑传输速率可以非常容易的突破1MB/s,峰值达到1.2MB/s以上,速度奇稳无比

以上“问题”稳定复现 =。=

我想请问下有没有方法可以使其能稳定达到?

路由器:斐讯E1 刷OP
电脑客户端代码

Code: Select all

var net = require('net');

function TCPTest(ip, port) {
    let recivedBytes = 0;

    var client = new net.Socket();
    client
        .on('data', (dat) => {
            recivedBytes += dat.length;
        })
        .on('error', () => { })
        .setKeepAlive(true)
        .connect(port,ip,()=>{
            console.log('已连接到服务器');
            client.write('.')
        })

    setInterval(() => {
        console.log(`Port ${port} recived ${recivedBytes} Bytes @${(new Date()).getTime()}`);
        recivedBytes = 0;
    }, 1000)
}

TCPTest('192.168.1.250',6666)


user_main.c

Code: Select all

#include "osapi.h"
#include "user_interface.h"

extern void user_tcpserver_init(uint32 local_port);

void WiFi_init() {
    //设置CPU为160Mhz
    system_update_cpu_freq(160);
    //注册wifi事件回调
    //wifi_set_event_handler_cb(wifi_handle_event_cb);
    //配置物理层协议
    wifi_set_phy_mode(PHY_MODE_11N);

    //配置为纯Station模式
    wifi_set_opmode_current(STATION_MODE);

    /* WiFi速率控制
        wifi_set_user_rate_limit(RC_LIMIT_11N,STATION_IF,RATE_11N_MCS7S,RATE_11N_MCS7);
        wifi_set_user_limit_rate_mask(LIMIT_RATE_MASK_STA);
    //*/

    //禁止wifi休眠
    wifi_set_sleep_type(NONE_SLEEP_T);
    //* 配置要连接的wifi
    struct station_config conf = {
        .ssid = "He",
        .password = "zzzzzzzz",
        .bssid_set = 0,
    };
    wifi_station_set_config(&conf);
    //*/

    //* 配置静态IP加速连接
    wifi_station_dhcpc_stop();
    struct ip_info info;
    IP4_ADDR(&info.ip, 192, 168, 1, 250);
    IP4_ADDR(&info.gw, 192, 168, 1, 1);
    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
    wifi_set_ip_info(STATION_IF, &info);
    //*/

    os_printf("init done\n");
}

void ICACHE_FLASH_ATTR user_init(void) {
    WiFi_init();
    user_tcpserver_init(6666);
}


TCPSERVER.c

Code: Select all

#include "mem.h"
#include "osapi.h"
#include "user_interface.h"

#include "espconn.h"

typedef struct espconn espconn_t;

espconn_t conn;
esp_tcp tcp;

char fakedata[2920];

void tcp_server_recon_cb(void *arg, sint8 err) {
    os_printf("tcpserver connect error, error number:%d\r\n", err);
}

void tcp_server_recv_cb(void *arg, char *pdata, unsigned short len) {
    // os_printf("tcpserver_recvdata:\t%s\n", pdata);
    espconn_send((espconn_t *)arg, fakedata,
                 sizeof(fakedata));  // 发送消息
}

void tcp_server_send_cb(void *arg) {
    // os_printf("tcpserver_send succeed!\n");
    // espconn_send((espconn_t*)arg, fakedata,
    //             sizeof(fakedata));  // 发送消息
}
void tcp_server_write_finish_cb(void *arg) {
    espconn_send((espconn_t *)arg, fakedata,
                 sizeof(fakedata));  // 发送消息
}
void tcp_server_discon_cb(void *arg) { os_printf("tcp_client_disconnect!\n"); }

void tcp_server_listen_cb(void *arg) {
    espconn_set_opt((espconn_t *)arg,
                    ESPCONN_NODELAY | ESPCONN_COPY | ESPCONN_KEEPALIVE);
    espconn_regist_recvcb((espconn_t *)arg,
                          tcp_server_recv_cb);  // 注册接收回调函数
    //espconn_regist_sentcb((espconn_t *)arg,
    //                      tcp_server_send_cb);  // 注册发送回调函数
    espconn_regist_disconcb((espconn_t *)arg,
                            tcp_server_discon_cb);  // 注册断连回调函数
    espconn_regist_write_finish((espconn_t *)arg, tcp_server_write_finish_cb);
    os_printf("tcp_client_connected!\n");
}

void user_tcpserver_init(uint32 local_port) {
    conn.type = ESPCONN_TCP;
    conn.state = ESPCONN_NONE;

    conn.proto.tcp = &tcp;

    conn.proto.tcp->local_port = local_port;

    espconn_regist_connectcb(&conn, tcp_server_listen_cb);
    espconn_regist_reconcb(&conn, tcp_server_recon_cb);

    espconn_accept(&conn);               //建立服务器
    espconn_regist_time(&conn, 180, 0);  //设置超时
}


user_rf_cal_sector_set 用的是PIO中自带的函数

Code: Select all

#include "osapi.h"
#include "user_interface.h"

/******************************************************************************
 * FunctionName : user_rf_cal_sector_set
 * Description  : SDK just reversed 4 sectors, used for rf init data and paramters.
 *                We add this function to force users to set rf cal sector, since
 *                we don't know which sector is free in user's application.
 *                sector map for last several sectors : ABBBCDDD
 *                A : rf cal
 *                B : at parameters
 *                C : rf init data
 *                D : sdk parameters
 * Parameters   : none
 * Returns      : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            break;
        default:
            rf_cal_sec = 0;
            break;
    }
    return rf_cal_sec;
}

yuzu
Posts: 5
Joined: Sun Oct 06, 2019 2:34 pm

Re: ESP8266的WiFi异常速率问题

Postby yuzu » Tue Oct 29, 2019 8:04 pm

补充,4线程下,速率最高可以达到2.4MB/s
电脑nodejs客户端代码

Code: Select all

var net = require('net');

function TCPTest(ip, port, thread = 1) {
    let recivedBytes = 0;
    if (thread < 1) {
        thread = 1;
    }
    for (i = 0; i < thread; i++) {
        let client=new net.Socket();
        client
            .on('data', (dat) => {
                recivedBytes += dat.length;
            })
            .on('error', () => { })
            .setKeepAlive(true)
            .connect(port, ip, () => {
                console.log('已连接到服务器');
                client.write('.')
            })
    }
    setInterval(() => {
        let unit_c=['B','KB','MB','GB','TB','PB']
        let unit=0
        while(recivedBytes>1024){
            recivedBytes/=1024;
            unit++
        }
        console.log(`Port ${port} recived ${recivedBytes} ${unit_c[unit]}/s @${(new Date()).getTime()}`);
        recivedBytes = 0;
    }, 1000)

}

TCPTest('192.168.1.250', 6666,4)


Who is online

Users browsing this forum: No registered users and 3 guests