@@ -18,84 +18,39 @@ namespace WifiAP
18
18
public class Program
19
19
{
20
20
// Start Simple WebServer
21
- static WebServer server = new WebServer ( ) ;
21
+ private static WebServer _server = new WebServer ( ) ;
22
+ private static bool _wifiApMode = false ;
22
23
23
24
// Connected Station count
24
- static int connectedCount = 0 ;
25
+ private static int _connectedCount = 0 ;
25
26
26
27
// GPIO pin used to put device into AP set-up mode
27
- const int SETUP_PIN = 5 ;
28
+ private const int SetupPin = 5 ;
28
29
29
30
public static void Main ( )
30
31
{
31
32
Debug . WriteLine ( "Welcome to WiFI Soft AP world!" ) ;
32
33
33
34
var gpioController = new GpioController ( ) ;
34
- GpioPin setupButton = gpioController . OpenPin ( SETUP_PIN , PinMode . InputPullUp ) ;
35
+ GpioPin setupButton = gpioController . OpenPin ( SetupPin , PinMode . InputPullUp ) ;
35
36
36
37
// If Wireless station is not enabled then start Soft AP to allow Wireless configuration
37
38
// or Button pressed
38
- if ( ! Wireless80211 . IsEnabled ( ) || ( setupButton . Read ( ) == PinValue . High ) )
39
+ if ( setupButton . Read ( ) == PinValue . High )
39
40
{
40
-
41
- Wireless80211 . Disable ( ) ;
42
- if ( WirelessAP . Setup ( ) == false )
43
- {
44
- // Reboot device to Activate Access Point on restart
45
- Debug . WriteLine ( $ "Setup Soft AP, Rebooting device") ;
46
- Power . RebootDevice ( ) ;
47
- }
48
-
49
- var dhcpserver = new DhcpServer
50
- {
51
- CaptivePortalUrl = $ "http://{ WirelessAP . SoftApIP } "
52
- } ;
53
- var dhcpInitResult = dhcpserver . Start ( IPAddress . Parse ( WirelessAP . SoftApIP ) , new IPAddress ( new byte [ ] { 255 , 255 , 255 , 0 } ) ) ;
54
- if ( ! dhcpInitResult )
55
- {
56
- Debug . WriteLine ( $ "Error initializing DHCP server.") ;
57
- }
58
-
59
- Debug . WriteLine ( $ "Running Soft AP, waiting for client to connect") ;
60
- Debug . WriteLine ( $ "Soft AP IP address :{ WirelessAP . GetIP ( ) } ") ;
61
-
62
- // Link up Network event to show Stations connecting/disconnecting to Access point.
63
- //NetworkChange.NetworkAPStationChanged += NetworkChange_NetworkAPStationChanged;
64
- // Now that the normal Wifi is deactivated, that we have setup a static IP
65
- // We can start the Web server
66
- server . Start ( ) ;
41
+ WirelessAP . SetWifiAp ( ) ;
42
+ _wifiApMode = true ;
67
43
}
68
44
else
69
45
{
70
- Debug . WriteLine ( $ "Running in normal mode, connecting to Access point") ;
71
- var conf = Wireless80211 . GetConfiguration ( ) ;
72
-
73
- bool success ;
74
-
75
- // For devices like STM32, the password can't be read
76
- if ( string . IsNullOrEmpty ( conf . Password ) )
77
- {
78
- // In this case, we will let the automatic connection happen
79
- success = WifiNetworkHelper . Reconnect ( requiresDateTime : true , token : new CancellationTokenSource ( 60000 ) . Token ) ;
80
- }
81
- else
82
- {
83
- // If we have access to the password, we will force the reconnection
84
- // This is mainly for ESP32 which will connect normaly like that.
85
- success = WifiNetworkHelper . ConnectDhcp ( conf . Ssid , conf . Password , requiresDateTime : true , token : new CancellationTokenSource ( 60000 ) . Token ) ;
86
- }
87
-
88
- if ( success )
89
- {
90
- Debug . WriteLine ( $ "Connection is { success } ") ;
91
- Debug . WriteLine ( $ "We have a valid date: { DateTime . UtcNow } ") ;
92
- }
93
- else
94
- {
95
- Debug . WriteLine ( $ "Something wrong happened, can't connect at all") ;
96
- }
46
+ _wifiApMode = Wireless80211 . ConnectOrSetAp ( ) ;
97
47
}
98
48
49
+ Console . WriteLine ( $ "Connected with wifi credentials. IP Address: { ( _wifiApMode ? WirelessAP . GetIP ( ) : Wireless80211 . GetCurrentIPAddress ( ) ) } ") ;
50
+ if ( _wifiApMode )
51
+ {
52
+ _server . Start ( ) ;
53
+ }
99
54
100
55
// Just wait for now
101
56
// Here you would have the reset of your program using the client WiFI link
@@ -120,26 +75,26 @@ private static void NetworkChange_NetworkAPStationChanged(int NetworkIndex, Netw
120
75
string macString = BitConverter . ToString ( station . MacAddress ) ;
121
76
Debug . WriteLine ( $ "Station mac { macString } Rssi:{ station . Rssi } PhyMode:{ station . PhyModes } ") ;
122
77
123
- connectedCount ++ ;
78
+ _connectedCount ++ ;
124
79
125
80
// Start web server when it connects otherwise the bind to network will fail as
126
81
// no connected network. Start web server when first station connects
127
- if ( connectedCount == 1 )
82
+ if ( _connectedCount == 1 )
128
83
{
129
84
// Wait for Station to be fully connected before starting web server
130
85
// other you will get a Network error
131
86
Thread . Sleep ( 2000 ) ;
132
- server . Start ( ) ;
87
+ _server . Start ( ) ;
133
88
}
134
89
}
135
90
else
136
91
{
137
92
// Station disconnected. When no more station connected then stop web server
138
- if ( connectedCount > 0 )
93
+ if ( _connectedCount > 0 )
139
94
{
140
- connectedCount -- ;
141
- if ( connectedCount == 0 )
142
- server . Stop ( ) ;
95
+ _connectedCount -- ;
96
+ if ( _connectedCount == 0 )
97
+ _server . Stop ( ) ;
143
98
}
144
99
}
145
100
0 commit comments