2017-09-14 56 views
2

我的esp8266項目出現問題。我的目的是使用esp8266每秒傳輸信標幀,以便我的android設備或筆記本電腦可以接收它並顯示在我可以連接的AP列表中。 這裏是我的代碼,我寫道:ESP8266:發送具有深度睡眠模式的信標幀不能正常工作

#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的捕捉那些框架,是的,他們在那裏。但我仍然無法看到我的筆記本電腦和我的Android設備上的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); 
} 

有誰知道爲什麼?請幫我拿到這個,謝謝!

回答

0

首先,ESP.deepSleep(10e5)使處理器睡眠100ms,而不是一秒鐘。正確的是ESP.deepSleep(10e6)。其次,你試圖每秒發送一個數據包,這很難確保從設備讀取的無線數據包能夠確定地獲取該數據包並將其顯示在SSID列表中。從我的實驗中,我必須每200毫秒發送三個數據包,以確保它顯示在我的android手機和PC上。您可以嘗試使用您發送的數據包數量以及時間間隔來查看最適合您的數據...

此外,請務必在設置功能中正確配置ESP。當我嘗試使用類似的代碼時,我不得不使用兩條指令:wifi_set_opmode(STATION_MODE)和wifi_promiscuous_enable(1)

相關問題