-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathProgram.cs
70 lines (57 loc) · 1.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
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.Networking;
#if HAS_WIFI
#endif
namespace nanoFramework.WebServer.GpioRest
{
public class Program
{
#if HAS_WIFI
private static string MySsid = "ssid";
private static string MyPassword = "password";
#endif
private static bool _isConnected = false;
public static void Main()
{
Debug.WriteLine("Hello from a webserver!");
try
{
int connectRetry = 0;
Debug.WriteLine("Waiting for network up and IP address...");
bool success;
CancellationTokenSource cs = new(60000);
#if HAS_WIFI
success = WifiNetworkHelper.ConnectDhcp(MySsid, MyPassword, requiresDateTime: true, token: cs.Token);
#else
success = NetworkHelper.SetupAndConnectNetwork(cs.Token, true);
#endif
if (!success)
{
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {WifiNetworkHelper.Status}.");
if (WifiNetworkHelper.HelperException != null)
{
Debug.WriteLine($"Exception: {WifiNetworkHelper.HelperException}");
}
return;
}
// Instantiate a new web server on port 80.
using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(ControllerGpio) }))
{
// Start the server.
server.Start();
Thread.Sleep(Timeout.Infinite);
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
}
}
}