Esp8266 使用 Arduino 开发(二)

这一次讲的时ESP8266WiFiScan.h文件,用来搜索可用WiFi。 int8_t scanNetworks(bool async = false, bool show_hidden = false); int8_t scanComplete();       //  查看是否搜索结束 void...

这一次讲的时ESP8266WiFiScan.h文件,用来搜索可用WiFi。

int8_t scanNetworks(bool async = false, bool show_hidden = false);


int8_t scanComplete();       //  查看是否搜索结束

void scanDelete();              //  删除上一次搜索的结果


// scan result

bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden);


String SSID(uint8_t networkItem);   //  WiFi 的名称

uint8_t encryptionType(uint8_t networkItem);

int32_t RSSI(uint8_t networkItem);  //   信号强度

uint8_t * BSSID(uint8_t networkItem);  //  

String BSSIDstr(uint8_t networkItem);

int32_t channel(uint8_t networkItem);

bool isHidden(uint8_t networkItem);


使用时只需调用WiFi.scanNetworks();     ESP8266便会搜索WiFI,并将搜索的结果存到数组中。 
官方例程在调用WiFi.scanNetworks();之前先调用了

WiFi.mode(WIFI_STA);
WiFi.disconnect();

实际上可以不重复调用,因为在scanNetworks();这个函数中官方已经调用过了:

int8_t ESP8266WiFiScanClass::scanNetworks(bool async, bool show_hidden) {
    if(ESP8266WiFiScanClass::_scanStarted) {
        return WIFI_SCAN_RUNNING;
    }

    ESP8266WiFiScanClass::_scanAsync = async;

    WiFi.enableSTA(true);
                      //  使能STA模式
            
    int status = wifi_station_get_connect_status();
    if(status != STATION_GOT_IP && status != STATION_IDLE) {
        WiFi.disconnect(false);
                // 断开已连接的WiFi
    }

    scanDelete();
                                   // 删除 上一次的搜索结果

    struct scan_config config;
    config.ssid = 0;
    config.bssid = 0;
    config.channel = 0;
    config.show_hidden = show_hidden;
    if(wifi_station_scan(&config, reinterpret_cast<scan_done_cb_t>(&ESP8266WiFiScanClass::_scanDone))) {
        ESP8266WiFiScanClass::_scanComplete = false;
        ESP8266WiFiScanClass::_scanStarted = true;

        if(ESP8266WiFiScanClass::_scanAsync) {
            delay(0); // time for the OS to trigger the scan
            return WIFI_SCAN_RUNNING;
        }

        esp_yield();
        return ESP8266WiFiScanClass::_scanCount;
    } else {
        return WIFI_SCAN_FAILED;
    }

}
  • 发表于 2020-09-30 17:01
  • 阅读 ( 1151 )

0 条评论

请先 登录 后评论
淡若清风
淡若清风

35 篇文章

作家榜 »

  1. 淡若清风 35 文章
  2. 杨杨 2 文章
  3. cgisky 1 文章
  4. vvvans 0 文章
  5. meow_sir 0 文章
  6. Scott 0 文章
  7. ddd ddd 0 文章
  8. lx 0 文章