forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
103 lines (86 loc) · 3.91 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Diagnostics;
using System.Threading;
using System.Net.NetworkInformation;
using nanoFramework.Runtime.Native;
using Windows.Devices.Gpio;
namespace WiFiAP
{
public class Program
{
// Start Simple WebServer
static WebServer server = new WebServer();
// Connected Station count
static int connectedCount = 0;
// GPIO pin used to put device into AP set-up mode
const int SETUP_PIN = 5;
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);
}
/// <summary>
/// Event handler for Stations connecting or Disconnecting
/// </summary>
/// <param name="NetworkIndex">The index of Network Interface raising event</param>
/// <param name="e">Event argument</param>
private static void NetworkChange_NetworkAPStationChanged(int NetworkIndex, NetworkAPStationEventArgs e)
{
Debug.WriteLine($"NetworkAPStationChanged event Index:{NetworkIndex} Connected:{e.IsConnected} Station:{e.StationIndex} ");
// if connected then get information on the connecting station
if (e.IsConnected)
{
WirelessAPConfiguration wapconf = WirelessAPConfiguration.GetAllWirelessAPConfigurations()[0];
WirelessAPStation station = wapconf.GetConnectedStations(e.StationIndex);
string macString = BitConverter.ToString(station.MacAddres);
Debug.WriteLine($"Station mac {macString} Rssi:{station.Rssi} PhyMode:{station.PhyModes} ");
connectedCount++;
// Start web server when it connects otherwise the bind to network will fail as
// no connected network. Start web server when first station connects
if (connectedCount == 1)
{
// Wait for Station to be fully connected before starting web server
// other you will get a Network error
Thread.Sleep(2000);
server.Start();
}
}
else
{
// Station disconnected. When no more station connected then stop web server
if (connectedCount > 0)
{
connectedCount--;
if (connectedCount == 0)
server.Stop();
}
}
}
}
}