竹壳天气时钟(一)使用数组保存扫描到的WiFi列表信息

一、简介

准备用基于esp8266的nodemcu开发板做一个天气时钟。

一步一步记录代码编写过程。

/*竹壳天气时钟

  Bamboo shell weather clock

  使用基于esp8266的NodeMCU制作。

  计划用竹子做最后成品的外壳,所以才有了这个名称。

  第一阶段任务:

  1.实现WiFi连接;

  2.如果30秒后还没连接成功就开启混合模式,使用webserver进行设置;

  3.页面可以输入ssid和密码连接WiFi;

  4.页面可以手动扫描WiFi网络并显示出扫描到的网络列表;

  5.点击需要连接的WiFi网络后可以输入密码并连接网络;

  6.连接成功后web页面弹出连接成功的提示并显示获取到的IP地址。

  2024年9月19日开始

*/

二、我目前在使用的云服务器推荐

学Linux不搞个云服务器始终感觉不爽!
要稳定性、安全性、不差钱的可以使用阿里、腾讯等大厂的云服务器。
本人穷屌丝一枚,所以我用的是免费的“三丰云”,同时提供"免费虚拟主机"和“免费云服务器”产品,有兴趣的可以试一下。
“三丰云”我已经用了一段时间,感觉还是很不错的,速度快也很稳定。
三丰云 https://www.sanfengyun.com 链接。
大家可以点击前往查看是否需要。

三、代码
/*竹壳天气时钟
  Bamboo shell weather clock

  使用基于esp8266的NodeMCU制作。
  计划用竹子做最后成品的外壳,所以才有了这个名称。

  第一阶段任务:
  1.实现WiFi连接;
  2.如果30秒后还没连接成功就开启混合模式,使用webserver进行设置;
  3.页面可以输入ssid和密码连接WiFi;
  4.页面可以手动扫描WiFi网络并显示出扫描到的网络列表;
  5.点击需要连接的WiFi网络后可以输入密码并连接网络;
  6.连接成功后web页面弹出连接成功的提示并显示获取到的IP地址。
  2024年9月19日开始
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

#ifndef APSSID
#define APSSID "ESPap"
#define APPSK "nodemcu-esp8266"
#endif

const char *apssid = APSSID;
const char *appassword = APPSK;
// 自定义数据结构
struct Listwifi {
  String ssid;
  int32_t rssi;
  uint8_t encryptionType;
  uint8_t *bssid;
  int32_t channel;
  bool hidden;
};
// WiFi列表保存数据使用的数组
Listwifi *wifiList;

void wsf() {
  int scanResult;
  Serial.println(F("开始WiFi扫描..."));
  scanResult = WiFi.scanNetworks(/*async=*/false, /*hidden=*/true);
  if (scanResult == 0) {
    Serial.println(F("找不到WiFi网络"));
  } else if (scanResult > 0) {
    Serial.printf(PSTR("%d 扫描出的网络数量:\n"), scanResult);
    // 打印扫描出的WiFi列表
    for (int8_t i = 0; i < scanResult; i++) {
      WiFi.getNetworkInfo(i, wifiList[i].ssid, wifiList[i].encryptionType, wifiList[i].rssi, wifiList[i].bssid, wifiList[i].channel, wifiList[i].hidden);
      // 获取扩展信息
      const bss_info *bssInfo = WiFi.getScanInfoByIndex(i);
      String phyMode;
      const char *wps = "";
      if (bssInfo) {
        phyMode.reserve(12);
        phyMode = F("802.11");
        String slash;
        if (bssInfo->phy_11b) {
          phyMode += 'b';
          slash = '/';
        }
        if (bssInfo->phy_11g) {
          phyMode += slash + 'g';
          slash = '/';
        }
        if (bssInfo->phy_11n) {
          phyMode += slash + 'n';
        }
        if (bssInfo->wps) {
          wps = PSTR("WPS");
        }
      }
      Serial.printf(PSTR("  %02d: [CH %02d] [%02X:%02X:%02X:%02X:%02X:%02X] %ddBm %c %c %-11s %3S %s\n"), i, wifiList[i].channel, wifiList[i].bssid[0], wifiList[i].bssid[1], wifiList[i].bssid[2], wifiList[i].bssid[3], wifiList[i].bssid[4], wifiList[i].bssid[5], wifiList[i].rssi, (wifiList[i].encryptionType == ENC_TYPE_NONE) ? ' ' : '*', wifiList[i].hidden ? 'H' : 'V', phyMode.c_str(), wps, wifiList[i].ssid.c_str());
      yield();
    }
  } else {
    Serial.printf(PSTR("WiFi扫描出现错误 %d"), scanResult);
  }
}

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/html", "<h1>You are connected</h1>");
}

void configap(){
  Serial.println();
  Serial.print("Configuring access point...");
  // 设置WiFi模式为混合模式
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAP(apssid, appassword);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

void setup() {
  // put your setup code here, to run once:
  // 打印信息
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  // 连接WiFi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  // 连接过程中打印出读秒的数字
  int i=0;
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(i);
    i++;
    if (i > 30) {
      configap();
      return;
    }
    delay(1000);
  }
  // 连接成功后打印消息和本地IP
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
  server.handleClient();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值