【问题标题】:ESP8266: Send beacon frame with deep sleep mode does not work properlyESP8266:使用深度睡眠模式发送信标帧无法正常工作
【发布时间】:2017-09-14 16:41:44
【问题描述】:

我的 esp8266 项目有问题。我的目的是使用 esp8266 每秒传输一次信标帧,以便我的 android 设备或我的笔记本电脑可以接收它并显示在我可以连接的 AP 列表中。 这是我写的代码:

#include <ESP8266WiFi.h>
extern "C" {
  #include "user_interface.h"
}

void setup() {
  delay(500);
  sendBeacon("ESP8266");
  ESP.deepSleep(10e5);
}

void loop() {
}

void sendBeacon(char* ssid) {
    // Randomize channel //
    byte channel = 1; 
    wifi_set_channel(channel);

    uint8_t packet[128] = { 0x80, 0x00, //Frame Control 
                        0x00, 0x00, //Duration
                /*4*/   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, //Destination address 
                /*10*/  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, //Source address - overwritten later
                /*16*/  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, //BSSID - overwritten to the same as the source address
                /*22*/  0xc0, 0x6c, //Seq-ctl
                //Frame body starts here
                /*24*/  0x83, 0x51, 0xf7, 0x8f, 0x0f, 0x00, 0x00, 0x00, //timestamp - the number of microseconds the AP has been active
                /*32*/  0xFF, 0x00, //Beacon interval
                /*34*/  0x01, 0x04, //Capability info
                /* SSID */
                /*36*/  0x00
                };

    int ssidLen = strlen(ssid);
    packet[37] = ssidLen;

    for(int i = 0; i < ssidLen; i++) {
      packet[38+i] = ssid[i];
    }

    uint8_t postSSID[13] = {0x01, 0x08, 0x82, 0x84, 0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c, //supported rate
                        0x03, 0x01, 0x04 /*DSSS (Current Channel)*/ };

    for(int i = 0; i < 12; i++) {
      packet[38 + ssidLen + i] = postSSID[i];
    }

    packet[50 + ssidLen] = channel;

    // get SRC MAC
    unsigned char mac[6];
    WiFi.macAddress(mac);
    packet[10] = packet[16] = mac[0];
    packet[11] = packet[17] = mac[1];
    packet[12] = packet[18] = mac[2];
    packet[13] = packet[19] = mac[3];
    packet[14] = packet[20] = mac[4];
    packet[15] = packet[21] = mac[5];

    int packetSize = 51 + ssidLen;

    wifi_send_pkt_freedom(packet, packetSize, 0);
    delay(1);
}

我使用 tcpdump 来捕获那些帧,是的,它们就在那里。但我仍然无法在我的笔记本电脑和我的安卓设备上的 AP 列表中看到它。 如果我停止使用深度睡眠模式,我可以看到它。例如:

#include <ESP8266WiFi.h>
extern "C" {
  #include "user_interface.h"
}

void setup() {
  delay(500);
//  sendBeacon("ESP8266");
//  ESP.deepSleep(10e5);
}

void loop() {
  sendBeacon("ESP8266");
}

void sendBeacon(char* ssid) {
    // Randomize channel //
    byte channel = 1; 
    wifi_set_channel(channel);

    uint8_t packet[128] = { 0x80, 0x00, //Frame Control 
                        0x00, 0x00, //Duration
                /*4*/   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, //Destination address 
                /*10*/  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, //Source address - overwritten later
                /*16*/  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, //BSSID - overwritten to the same as the source address
                /*22*/  0xc0, 0x6c, //Seq-ctl
                //Frame body starts here
                /*24*/  0x83, 0x51, 0xf7, 0x8f, 0x0f, 0x00, 0x00, 0x00, //timestamp - the number of microseconds the AP has been active
                /*32*/  0xFF, 0x00, //Beacon interval
                /*34*/  0x01, 0x04, //Capability info
                /* SSID */
                /*36*/  0x00
                };

    int ssidLen = strlen(ssid);
    packet[37] = ssidLen;

    for(int i = 0; i < ssidLen; i++) {
      packet[38+i] = ssid[i];
    }

    uint8_t postSSID[13] = {0x01, 0x08, 0x82, 0x84, 0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c, //supported rate
                        0x03, 0x01, 0x04 /*DSSS (Current Channel)*/ };

    for(int i = 0; i < 12; i++) {
      packet[38 + ssidLen + i] = postSSID[i];
    }

    packet[50 + ssidLen] = channel;

    // get SRC MAC
    unsigned char mac[6];
    WiFi.macAddress(mac);
    packet[10] = packet[16] = mac[0];
    packet[11] = packet[17] = mac[1];
    packet[12] = packet[18] = mac[2];
    packet[13] = packet[19] = mac[3];
    packet[14] = packet[20] = mac[4];
    packet[15] = packet[21] = mac[5];

    int packetSize = 51 + ssidLen;

    wifi_send_pkt_freedom(packet, packetSize, 0);
    delay(1);
}

有人知道为什么吗?请帮我搞定,谢谢!

【问题讨论】:

    标签: arduino wifi esp8266 beacon


    【解决方案1】:

    首先,ESP.deepSleep(10e5) 使处理器休眠 100 毫秒,而不是一秒钟。正确的是 ESP.deepSleep(10e6)。其次,您尝试每秒只发送一个数据包,很难确保从设备读取的 wifi 肯定会获取该数据包并将其显示在 SSID 列表中。根据我的实验,我必须每 200 毫秒发送三个数据包,以确保它显示在我的 android 手机和 pc 上。您可以尝试使用您发送的数据包数量和间隔,看看最适合您的...

    另外,请确保在设置功能中正确配置 ESP。当我尝试类似的代码时,我不得不使用两条指令:wifi_set_opmode(STATION_MODE) 和 wifi_promiscuous_enable(1)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多