-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathProgram.cs
170 lines (135 loc) · 5.94 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Threading;
using System.Net;
/// <summary>
/// Improv provisioning sample
/// For details about Improv protocol and to try provisioning Wifi credentials from a web page
/// See: https://www.improv-wifi.com/
/// Also has instructions on how to embed into your web page.
/// </summary>
namespace ImprovWifi
{
public class Program
{
static Improv _imp;
public static void Main()
{
Console.WriteLine("Example of using IMPROV bluetooth LE for Wifi provisioning");
// Construct Improv class
_imp = new Improv();
// This optional event will be fired if asked to identify device
// You can flash an LED or some other method.
_imp.OnIdentify += Imp_OnIdentify;
// This optional event is called when the provisioning is completed and Wifi is connected but before
// improv has informed Improv client of result. This allows user to set the provision URL redirect with correct IP address
// See event handler
_imp.OnProvisioningComplete += Imp_OnProvisioningComplete;
// This optional event will be called to do the Wifi provisioning in user program.
// if not set then improv class will automatically try to connect to Wifi
// For this sample we will let iprov do it, uncomment next line to try user event. See event handler
// imp.OnProvisioned += Imp_OnProvisioned;
// Start IMPROV service to start advertising using provided device name.
_imp.Start("Improv sample");
// You may need a physical button to be pressed to authorise the provisioning (security)
// Wait for button press and call Authorise method
// For out test we will just Authorise
_imp.Authorise(true);
Console.WriteLine("Waiting for device to be provisioned");
// Now wait for Device to be Provisioned
// we could also just use the OnProvisioningComplete event
while (_imp.CurrentState != Improv.ImprovState.provisioned)
{
Thread.Sleep(500);
}
Console.WriteLine("Device has been provisioned");
// We are now provisioned and connected to Wifi, so stop bluetooth service to release resources.
_imp.Stop();
_imp = null;
;
// Start our very simple web page server to pick up the redirect we gave
Console.WriteLine("Starting simple web server");
SimpleWebListener();
Thread.Sleep(Timeout.Infinite);
}
/// <summary>
/// Event handler for OnProvisioningComplete event
/// </summary>
/// <param name="sender">Improv instance</param>
/// <param name="e">Not used</param>
private static void Imp_OnProvisioningComplete(object sender, EventArgs e)
{
SetProvisioningURL();
}
/// <summary>
/// Set URL with current IP address
/// The Improv client will redirect to this URL if set.
/// </summary>
private static void SetProvisioningURL()
{
// All good, wifi connected, set up URL for access
_imp.RedirectUrl = "http://" + _imp.GetCurrentIPAddress() + "/start.htm";
}
private static void Imp_OnProvisioned(object sender, ProvisionedEventArgs e)
{
string ssid = e.Ssid;
string password = e.Password;
Console.WriteLine("Provisioning device");
Console.WriteLine("Connecting to Wifi...");
// Try to connect to Wifi AP
// use improv internal method
if (_imp.ConnectWiFi(ssid, password))
{
Console.WriteLine("Connected to Wifi");
SetProvisioningURL();
}
else
{
Console.WriteLine("Failed to Connect to Wifi!");
// if not successful set error and return
_imp.ErrorState = Improv.ImprovError.unableConnect;
}
}
private static void Imp_OnIdentify(object sender, EventArgs e)
{
// Flash LED to Identify device or do nothing
Console.WriteLine("Flashing LED...");
}
private static void SimpleWebListener()
{
// set-up our HTTP response
string responseString =
"<HTML><BODY>" +
"<h2>Hello from nanoFramework</h2>" +
"<p>We are a newly provisioned device using <b>Improv</b> over Bluetooth.</p>" +
"<p>See <a href='https://www.improv-wifi.com'>Improv web site</a> for details" +
"</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Create a listener.
HttpListener listener = new("http", 80);
listener.Start();
while (true)
{
try
{
// Now wait on context for a connection
HttpListenerContext context = listener.GetContext();
Console.WriteLine("Web request received");
// Get the response stream
HttpListenerResponse response = context.Response;
// Write reply
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
// output stream must be closed
context.Response.Close();
Console.WriteLine("Web response sent");
// context must be closed
context.Close();
}
catch (Exception ex)
{
Console.WriteLine("* Error getting context: " + ex.Message + "\r\nSack = " + ex.StackTrace);
}
}
}
}
}