private void ProcessRequest(HttpListenerContext context) { var request = context.Request; var response = context.Response; string responseString; string ssid = null; string password = null; bool isApSet = false; switch (request.HttpMethod) { case "GET": string[] url = request.RawUrl.Split('?'); if (url[0] == "/favicon.ico") { response.ContentType = "image/png"; byte[] responseBytes = Resources.GetBytes(Resources.BinaryResources.favicon); OutPutByteResponse(response, responseBytes); } else { response.ContentType = "text/html"; responseString = ReplaceMessage(Resources.GetString(Resources.StringResources.main), ""); OutPutResponse(response, responseString); } break; case "POST": // Pick up POST parameters from Input Stream Hashtable hashPars = ParseParamsFromStream(request.InputStream); ssid = (string)hashPars["ssid"]; password = (string)hashPars["password"]; Debug.WriteLine($"Wireless parameters SSID:{ssid} PASSWORD:{password}"); string message = "<p>New settings saved.</p><p>Rebooting device to put into normal mode</p>"; responseString = CreateMainPage(message); OutPutResponse(response, responseString); isApSet = true; break; } response.Close(); if (isApSet && (!string.IsNullOrEmpty(ssid)) && (!string.IsNullOrEmpty(password))) { // Enable the Wireless station interface Wireless80211.Configure(ssid, password); // Disable the Soft AP WirelessAP.Disable(); Thread.Sleep(200); Power.RebootDevice(); } }
public static void Main() { Debug.WriteLine("Welcome to WiFI Soft AP world!"); GpioPin setupButton = GpioController.GetDefault().OpenPin(SETUP_PIN); setupButton.SetDriveMode(GpioPinDriveMode.InputPullUp); // If Wireless station is not enabled then start Soft AP to allow Wireless configuration // or Button pressed if (!Wireless80211.IsEnabled() || (setupButton.Read() == GpioPinValue.Low)) { Wireless80211.Disable(); if (WirelessAP.Setup() == false) { // Reboot device to Activate Access Point on restart Debug.WriteLine($"Setup Soft AP, Rebooting device"); Power.RebootDevice(); } Debug.WriteLine($"Running Soft AP, waiting for client to connect"); Debug.WriteLine($"Soft AP IP address :{WirelessAP.GetIP()}"); // Link up Network event to show Stations connecting/disconnecting to Access point. NetworkChange.NetworkAPStationChanged += NetworkChange_NetworkAPStationChanged;; } else { Debug.WriteLine($"Running in normal mode, connecting to Access point"); string IpAdr = Wireless80211.WaitIP(); Debug.WriteLine($"Connected as {IpAdr}"); } // Just wait for now // Here you would have the reset of your program using the client WiFI link Thread.Sleep(Timeout.Infinite); }
public static void Main() { Debug.WriteLine("Welcome to WiFI Soft AP world!"); var gpioController = new GpioController(); GpioPin setupButton = gpioController.OpenPin(SETUP_PIN, PinMode.InputPullUp); // If Wireless station is not enabled then start Soft AP to allow Wireless configuration // or Button pressed if (!Wireless80211.IsEnabled() || (setupButton.Read() == PinValue.High)) { Wireless80211.Disable(); if (WirelessAP.Setup() == false) { // Reboot device to Activate Access Point on restart Debug.WriteLine($"Setup Soft AP, Rebooting device"); Power.RebootDevice(); } Debug.WriteLine($"Running Soft AP, waiting for client to connect"); Debug.WriteLine($"Soft AP IP address :{WirelessAP.GetIP()}"); // Link up Network event to show Stations connecting/disconnecting to Access point. //NetworkChange.NetworkAPStationChanged += NetworkChange_NetworkAPStationChanged; // Now that the normal Wifi is deactivated, that we have setup a static IP // We can start the Web server server.Start(); } else { Debug.WriteLine($"Running in normal mode, connecting to Access point"); var conf = Wireless80211.GetConfiguration(); bool success; // For devices like STM32, the password can't be read if (string.IsNullOrEmpty(conf.Password)) { // In this case, we will let the automatic connection happen success = WiFiNetworkHelper.Reconnect(requiresDateTime: true, token: new CancellationTokenSource(60000).Token); } else { // If we have access to the password, we will force the reconnection // This is mainly for ESP32 which will connect normaly like that. success = WiFiNetworkHelper.ConnectDhcp(conf.Ssid, conf.Password, requiresDateTime: true, token: new CancellationTokenSource(60000).Token); } if (success) { Debug.WriteLine($"Connection is {success}"); Debug.WriteLine($"We have a valid date: {DateTime.UtcNow}"); } else { Debug.WriteLine($"Something wrong happened, can't connect at all"); } } // Just wait for now // Here you would have the reset of your program using the client WiFI link Thread.Sleep(Timeout.Infinite); }