From de60c82683d6d0765b8f5467ae6ebc404b0dfe1a Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Wed, 3 Jan 2024 10:45:22 +0300 Subject: [PATCH] Improve Improv-wifi (#343) --- samples/Bluetooth/ImprovWifi/Improv.cs | 19 +++++++++-- .../Bluetooth/ImprovWifi/ImprovWifi.nfproj | 33 +++++++------------ samples/Bluetooth/ImprovWifi/README.md | 2 +- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/samples/Bluetooth/ImprovWifi/Improv.cs b/samples/Bluetooth/ImprovWifi/Improv.cs index 397056ad..793c987e 100644 --- a/samples/Bluetooth/ImprovWifi/Improv.cs +++ b/samples/Bluetooth/ImprovWifi/Improv.cs @@ -11,6 +11,7 @@ using System.Device.Wifi; using nanoFramework.Networking; using nanoFramework.Device.Bluetooth.Advertisement; +using System.Threading; namespace ImprovWifi { @@ -524,11 +525,23 @@ public bool ConnectWiFi(string ssid, string password) WifiAdapter wa = WifiAdapter.FindAllAdapters()[0]; wa.Disconnect(); - System.Threading.CancellationTokenSource cs = new(30000); + CancellationTokenSource cs = new(30000); Console.WriteLine("ConnectDHCP"); - bool success = WifiNetworkHelper.ConnectDhcp(ssid, password, requiresDateTime: true, token: cs.Token); + WifiNetworkHelper.Disconnect(); + bool success; + + success = WifiNetworkHelper.ConnectDhcp(ssid, password, WifiReconnectionKind.Automatic, true, token: cs.Token); + + if (!success) + { + wa.Disconnect(); + // Network helper only allow 1 configuration, we've most likely try to connect before, let's make it manual + var res = wa.Connect(ssid, WifiReconnectionKind.Automatic, password); + // If we still arenot connected, it means, it's not good! + success = res.ConnectionStatus == WifiConnectionStatus.Success; + } + Console.WriteLine($"ConnectDHCP exit {success}"); - cs.Cancel(); return success; } diff --git a/samples/Bluetooth/ImprovWifi/ImprovWifi.nfproj b/samples/Bluetooth/ImprovWifi/ImprovWifi.nfproj index da0eadb6..c2d7737f 100644 --- a/samples/Bluetooth/ImprovWifi/ImprovWifi.nfproj +++ b/samples/Bluetooth/ImprovWifi/ImprovWifi.nfproj @@ -27,49 +27,38 @@ - + packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll - True - + packages\nanoFramework.Device.Bluetooth.1.1.60\lib\nanoFramework.Device.Bluetooth.dll - True - + packages\nanoFramework.Runtime.Events.1.11.15\lib\nanoFramework.Runtime.Events.dll - True - + packages\nanoFramework.Runtime.Native.1.6.12\lib\nanoFramework.Runtime.Native.dll - True - + packages\nanoFramework.System.Collections.1.5.31\lib\nanoFramework.System.Collections.dll - True - + packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll - True - + packages\nanoFramework.System.Device.Wifi.1.5.71\lib\System.Device.Wifi.dll - True - + packages\nanoFramework.System.IO.Streams.1.1.52\lib\System.IO.Streams.dll - True - + packages\nanoFramework.System.Net.1.10.64\lib\System.Net.dll - True - + packages\nanoFramework.System.Net.Http.1.5.113\lib\System.Net.Http.dll - True - + packages\nanoFramework.System.Threading.1.1.32\lib\System.Threading.dll - True diff --git a/samples/Bluetooth/ImprovWifi/README.md b/samples/Bluetooth/ImprovWifi/README.md index 51ea0d13..43525a35 100644 --- a/samples/Bluetooth/ImprovWifi/README.md +++ b/samples/Bluetooth/ImprovWifi/README.md @@ -1,6 +1,6 @@ # 🌶️🌶️🌶️ - Improv Wifi provisioning -This sample shows the use of the `Improv` class to provision the Wifi credentials for an ESP32 device via Bluetooth LE. +This sample shows the use of the `Improv` class to provision the Wifi credentials for an ESP32 device via Bluetooth LE. To setup your device, go to the page [](https://www.improv-wifi.com/). The device will advertise with the name `Improv sample` and support the `Improv Service` allowing the Wifi Credentials to be setup directly from the Improv test page or any other web page.