Skip to content

Commit f6a4ba7

Browse files
authored
Soft AP sample (#23)
1 parent 568df52 commit f6a4ba7

15 files changed

+842
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Feel free to browse, take what you like and contribute back if you want.
6363
<tr>
6464
<td><a href="samples/HardwareEsp32">Hardware Esp32 Test</a></td>
6565
<td><a href="samples/Wifi">WiFi</a></td>
66+
<td><a href="samples/WifiAP">WiFi Soft AP</a></td>
6667
<td><!--<a href="Utility/util3">Utility Three</a>--></td>
6768
</tr>
6869
</table>

README.zh-cn.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<tr>
6464
<td><a href="samples/HardwareEsp32">Esp32 硬件</a></td>
6565
<td><a href="samples/Wifi">WiFi</a></td>
66+
<td><a href="samples/WifiAP">WiFi Soft AP</a></td>
6667
<td><!--<a href="Utility/util3">Utility Three</a>--></td>
6768
</tr>
6869
</table>

samples/WiFiAP/Program.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Threading;
3+
using System.Net.NetworkInformation;
4+
using nanoFramework.Runtime.Native;
5+
using Windows.Devices.Gpio;
6+
7+
namespace WiFiAP
8+
{
9+
public class Program
10+
{
11+
// Start Simple WebServer
12+
static WebServer server = new WebServer();
13+
14+
// Connected Station count
15+
static int connectedCount = 0;
16+
17+
// Gpio pin used to put device into AP setup mode
18+
const int SETUP_PIN = 5;
19+
20+
public static void Main()
21+
{
22+
Console.WriteLine("Welcome to WiFI Soft AP world!");
23+
24+
GpioPin setupButton = GpioController.GetDefault().OpenPin(SETUP_PIN);
25+
setupButton.SetDriveMode(GpioPinDriveMode.InputPullUp);
26+
27+
// If Wireless station is not enabled then start Soft AP to allow Wireless configuration
28+
// or Button pressed
29+
if (!Wireless80211.IsEnabled() || (setupButton.Read() == GpioPinValue.Low))
30+
{
31+
Wireless80211.Disable();
32+
33+
if (WirelessAP.Setup() == false)
34+
{
35+
// Reboot device to Activate Access Point on restart
36+
Console.WriteLine($"Setup Soft AP, Rebooting device");
37+
Power.RebootDevice();
38+
}
39+
40+
Console.WriteLine($"Running Soft AP, waiting for client to connect");
41+
Console.WriteLine($"Soft AP IP address :{WirelessAP.GetIP()}");
42+
43+
// Link up Network event to show Stations connecting/disconnecting to Access point.
44+
NetworkChange.NetworkAPStationChanged += NetworkChange_NetworkAPStationChanged; ;
45+
}
46+
else
47+
{
48+
Console.WriteLine($"Running in normal mode, connecting to Access point");
49+
string IpAdr = Wireless80211.WaitIP();
50+
Console.WriteLine($"Connected as {IpAdr}");
51+
}
52+
53+
54+
// Just wait
55+
Thread.Sleep(Timeout.Infinite);
56+
}
57+
58+
/// <summary>
59+
/// Event handler for Stations connecting or Disconnecting
60+
/// </summary>
61+
/// <param name="NetworkIndex">The index of Network Interface raising event</param>
62+
/// <param name="e">Event argument</param>
63+
private static void NetworkChange_NetworkAPStationChanged(int NetworkIndex, NetworkAPStationEventArgs e)
64+
{
65+
Console.WriteLine($"NetworkAPStationChanged event Index:{NetworkIndex} Connected:{e.IsConnected} Station:{e.StationIndex} ");
66+
67+
// if connected then get information on the connecting station
68+
if (e.IsConnected)
69+
{
70+
WirelessAPConfiguration wapconf = WirelessAPConfiguration.GetAllWirelessAPConfigurations()[0];
71+
WirelessAPStation station = wapconf.GetConnectedStations(e.StationIndex);
72+
73+
string macString = BitConverter.ToString(station.MacAddres);
74+
Console.WriteLine($"Station mac {macString} Rssi:{station.Rssi} PhyMode:{station.PhyModes} ");
75+
76+
connectedCount++;
77+
78+
// Start web server when it connects otherwise the bind to network will fail as
79+
// no connected network. Start web server when first station connects
80+
if (connectedCount == 1)
81+
{
82+
// Wait for Staion to be fully connected before starting web server
83+
// other you will get a Network error
84+
Thread.Sleep(2000);
85+
server.Start();
86+
}
87+
}
88+
else
89+
{
90+
// Station disconnected. When no more station connected then stop webserver
91+
if (connectedCount > 0)
92+
{
93+
connectedCount--;
94+
if (connectedCount == 0)
95+
server.Stop();
96+
}
97+
}
98+
99+
}
100+
}
101+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Soft AP sample ")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("nanoFramework")]
13+
[assembly: AssemblyCopyright("Copyright ©2019 nanoFramework ")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]

samples/WiFiAP/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# WiFi Soft AP sample
2+
3+
Shows how to use various APIs related with WiFi Soft AP.
4+
5+
### Starts a Soft AP (Hot spot) and runs a simple web server to configure the Wireless connection parameters.
6+
7+
The sample uses GPIO pin 5 to switch back into the SoftAP configuration mode. When pulled to ground on boot will switch into Soft AP mode.
8+
You can connect to the web server via the url http://192.168.4.1/
9+
10+
Once you save the wireless configuration the next boot the device will automatically connect to that Access Point
11+
and the Soft AP will be disabled.
12+
13+
> **Note:** This sample is part of a large collection of nanoFramework feature samples.
14+
> If you are unfamiliar with Git and GitHub, you can download the entire collection as a
15+
> [ZIP file](https://github.com/nanoframework/Samples/archive/master.zip), but be
16+
> sure to unzip everything to access any shared dependencies.
17+
<!-- For more info on working with the ZIP file,
18+
> the samples collection, and GitHub, see [Get the UWP samples from GitHub](https://aka.ms/ovu2uq).
19+
> For more samples, see the [Samples portal](https://aka.ms/winsamples) on the Windows Dev Center. -->
20+
21+
## Hardware requirements
22+
23+
An hardware device with WiFi networking capabilities running a nanoFramework image.
24+
Currently only the Esp32.
25+
26+
## Related topics
27+
28+
### Reference
29+
30+
- [SYstem.Net.NetworkInformation](http://docs.nanoframework.net/api/System.Net.NetworkInformation.html)
31+
32+
## Build the sample
33+
34+
1. If you download the samples ZIP, be sure to unzip the entire archive, not just the folder with the sample you want to build.
35+
2. Start Microsoft Visual Studio 2017 and select **File** \> **Open** \> **Project/Solution**.
36+
3. Starting in the folder where you unzipped the samples, go to the subfolder for this specific sample. Double-click the Visual Studio Solution (.sln) file.
37+
4. Press Ctrl+Shift+B, or select **Build** \> **Build Solution**.
38+
39+
## Run the sample
40+
41+
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
42+
43+
### Deploying the sample
44+
45+
- Select Build > Deploy Solution.
46+
47+
### Deploying and running the sample
48+
49+
- To debug the sample and then run it, press F5 or select Debug > Start Debugging.

samples/WiFiAP/Resources.Designer.cs

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/WiFiAP/Resources.resx

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
121+
<data name="favicon" type="System.Resources.ResXFileRef, System.Windows.Forms">
122+
<value>Resources\Images\favicon.png;System.Byte[], mscorlib, Version=1.2.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731</value>
123+
</data>
124+
<data name="main" type="System.Resources.ResXFileRef, System.Windows.Forms">
125+
<value>resources\html\main.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
126+
</data>
127+
</root>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<h1>NanoFramework</h1>
5+
<form method='POST'>
6+
<fieldset><legend>Wireless configuration</legend>
7+
Ssid:<br><input type="text" name="ssid" value=""><br>
8+
Password:<br><input type="text" name="password" value=""><br>
9+
<br>
10+
<input type="submit" value="Save">
11+
</fieldset>
12+
<b>{message}</b>
13+
</form>
14+
</body>
15+
</html>
9.53 KB
Loading

0 commit comments

Comments
 (0)