forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
148 lines (126 loc) · 5.77 KB
/
Copy pathProgram.cs
File metadata and controls
148 lines (126 loc) · 5.77 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Device.Gpio;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using Iot.Device.DhcpServer;
using nanoFramework.Networking;
using nanoFramework.Runtime.Native;
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!");
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();
}
var dhcpserver = new DhcpServer
{
CaptivePortalUrl = $"http://{WirelessAP.SoftApIP}"
};
var dhcpInitResult = dhcpserver.Start(IPAddress.Parse(WirelessAP.SoftApIP), new IPAddress(new byte[] { 255, 255, 255, 0 }));
if (!dhcpInitResult)
{
Debug.WriteLine($"Error initializing DHCP server.");
}
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);
}
/// <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.MacAddress);
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();
}
}
}
}
}