C#开发ESP32E(3)Wifi配置使用

1.安装Wifi配置库(nanoFramework.System.Device.Wifi)

1.1 nanoFramework.System.Device.Wifi介绍

API预览--地址:https://docs.nanoframework.net/api/System.Device.Wifi.html

该库可配置ESP32使用Wifi模块进行通信
与Wifi建立连接有如下步骤:

  1. 创建Wifi适配器
  2. 扫描Wifi列表
  3. 建立连接

1.2 核心API介绍

查找适配器列表(静态)
WifiAdapter.FindAllAdapters()

扫描Wifi完成事件
WifiAdapter.AvailableNetworksChanged

异步扫描Wifi列表(实例)
WifiAdapter.ScanAsync()

连接与断开连接(实例)
WifiAdapter.Connect()&&WifiAdapter.Disconnect()

1.3 完整代码

public class Program
    {
        const string _wifiId = "you_WifiName";
        const string _wifiPassword = "you_WifiPassword";
        public static void Main()
        {
            Debug.WriteLine("Hello from nanoFramework!");

            try
            {
                //Wifi适配器
                WifiAdapter wifi = WifiAdapter.FindAllAdapters()[0];

                //注册扫描Wifi列表完成事件
                wifi.AvailableNetworksChanged += Wifi_AvailableNetworksChanged;

                //Wait,等待初始化完成
                Thread.Sleep(10_000);

                //30s扫描一次
                while (true)
                {

                    try
                    {
                        Debug.WriteLine("starting Wi-Fi scan");
                        //异步扫描WiFi列表
                        //触发WifiAdapter.AvailableNetworksChanged事件。
                        //AvailableNetworksChanged事件中,使用WifiAdapter.NetworkReport检索可用的Wi-Fi网络列表。
                        wifi.ScanAsync();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"Failure starting a scan operation: {ex}");
                    }

                    Thread.Sleep(30000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("message:" + ex.Message);
                Debug.WriteLine("stack:" + ex.StackTrace);
            }
            Thread.Sleep(Timeout.Infinite);

        }

        private static void Wifi_AvailableNetworksChanged(WifiAdapter sender, object e)
        {
            Debug.WriteLine("Wifi_AvailableNetworksChanged - get report");

            // 获取所有Wifi报告
            WifiNetworkReport report = sender.NetworkReport;

            // 遍历所有网络
            foreach (WifiAvailableNetwork net in report.AvailableNetworks)
            {
                //SSID:Wifi名称
                //BSSID:设备Mac地址 
                //Rssi:信号强度
                //Signal:表示信号强度的条数
                Debug.WriteLine($"Net SSID :{net.Ssid},  BSSID : {net.Bsid},  Rssi : {net.NetworkRssiInDecibelMilliwatts.ToString()},  Signal : {net.SignalBars.ToString()}");

                // Wifi名一致,尝试连接
                if (net.Ssid == _wifiId)
                {
                    // 如果连接则断开
                    sender.Disconnect();

                    //尝试连接
                    WifiConnectionResult result = sender.Connect(net, WifiReconnectionKind.Automatic, _wifiPassword);

                    // 显示连接状态
                    if (result.ConnectionStatus == WifiConnectionStatus.Success)
                    {
                        Debug.WriteLine("Connected to Wifi network");
                    }
                    else
                    {
                        Debug.WriteLine($"Error {result.ConnectionStatus.ToString()} connecting o Wifi network");
                    }
                }
            }

        }
    }

参考文档:https://github.com/nanoframework/Samples/blob/main/samples/Wifi/ScanWiFi/Program.cs

posted @ 2023-07-10 20:08  SharpThreed  阅读(419)  评论(0编辑  收藏  举报