forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
156 lines (132 loc) · 7.14 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
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
using System;
using System.Device.Pwm;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using nanoFramework.Hardware.Esp32;
using nanoFramework.Networking;
using nanoFramework.WebServer;
namespace ServoMotorTester
{
public class Program
{
// Adjust the SSID and the password
private const string Ssid = "yourSSID";
private const string Password = "yourPassword";
// Adjust the pin used for the servo motor
private static int _pinPwm = 7;
private static WebServer _server;
private static PwmChannel _pwm;
private static ServoInformation _servo = new ServoInformation();
public static void Main()
{
Debug.WriteLine("Hello from nanoFramework ServoMotor tester!");
Console.WriteLine($"Connected with wifi credentials. IP Address: {GetCurrentIPAddress()}");
var res = WifiNetworkHelper.ConnectDhcp(Ssid, Password, token: new CancellationTokenSource(60_000).Token);
if (!res)
{
Console.WriteLine("Can't connect to the WiFi, check your credentials.");
return;
}
// Setting up the web server
_server = new WebServer(80, HttpProtocol.Http);
_server.CommandReceived += ServerCommandReceived;
_server.Start();
// setting up the PWM
Configuration.SetPinFunction(_pinPwm, DeviceFunction.PWM1);
_pwm = PwmChannel.CreateFromPin(_pinPwm, frequency: (int)_servo.Frequency);
_pwm.Start();
Thread.Sleep(Timeout.Infinite);
}
private static void ServerCommandReceived(object obj, WebServerEventArgs e)
{
const string PageProcess = "req";
const string ParamMinPulse = "mi";
const string ParamMaxPulse = "ma";
const string ParamFrequency = "fe";
const string ParamPosition = "po";
var url = e.Context.Request.RawUrl;
if (url.IndexOf($"/{PageProcess}") == 0)
{
string resp = string.Empty;
var parameters = WebServer.DecodeParam(url);
foreach (UrlParameter param in parameters)
{
if (param.Name == ParamMinPulse)
{
if (uint.TryParse(param.Value, out var value))
{
_servo.MinPulse = value;
resp += $"{nameof(_servo.MinPulse)}: {_servo.MinPulse} ";
}
continue;
}
if (param.Name == ParamMaxPulse)
{
if (uint.TryParse(param.Value, out var value))
{
_servo.MaxPulse = value;
resp += $"{nameof(_servo.MaxPulse)}: {_servo.MaxPulse} ";
}
continue;
}
if (param.Name == ParamFrequency)
{
if (uint.TryParse(param.Value, out var value))
{
_servo.Frequency = value;
_pwm.Frequency = (int)_servo.Frequency;
resp += $"{nameof(_servo.Frequency)}: {_servo.Frequency} ";
}
continue;
}
if (param.Name == ParamPosition)
{
if (uint.TryParse(param.Value, out var value))
{
_servo.Position = value;
if (_servo.Position > 100)
{
_servo.Position = 100;
}
resp += $"{nameof(_servo.Position)}: {_servo.Position} ";
}
continue;
}
}
uint duration = (uint)(_servo.MinPulse + (_servo.MaxPulse - _servo.MinPulse) / 100.0f * _servo.Position);
_pwm.DutyCycle = (double)duration / 1_000_000 * _pwm.Frequency;
resp += $"{nameof(_pwm.DutyCycle)} {_pwm.DutyCycle} ";
WebServer.OutPutStream(e.Context.Response, resp);
return;
}
string strResp = string.Empty;
strResp += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
strResp += "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Servo Motor Discover</title>";
strResp += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
strResp += "<meta http-equiv=\"Cache-control\" content=\"no-cache\"/>";
//create the script part
strResp += "<script language=\"JavaScript\">var xhr = new XMLHttpRequest();function btnclicked(boxMSG, cmdSend) {";
strResp += "document.getElementById('status').innerHTML=\"waiting\";";
strResp += "xhr.open('GET', cmdSend + boxMSG.value);";
strResp += "xhr.send(null); xhr.onreadystatechange = function() {if (xhr.readyState == 4) {document.getElementById('status').innerHTML=xhr.responseText;}};}";
strResp += "</script>";
//body
strResp += "</head><body><table >";
strResp += $"<tr><td>Min pulse</td><td><input id=\"MinPulse\" type=\"text\" value=\"{_servo.MinPulse}\" /></td><td><input id=\"MinPulseBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById ('MinPulse'),'{PageProcess}?{ParamMinPulse}=')\" /></td></tr>";
strResp += $"<tr><td>Max pulse</td><td><input id=\"MaxPulse\" type=\"text\" value=\"{_servo.MaxPulse}\" /></td><td><input id=\"MaxPulseBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('MaxPulse'),'{PageProcess}?{ParamMaxPulse}=')\" /></td></tr>";
strResp += $"<tr><td>Frequency</td><td><input id=\"Frequency\" type=\"text\" value=\"{_servo.Frequency}\" /></td><td><input id=\"FrequencyBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('Frequency'),'{PageProcess}?{ParamFrequency}=')\" /></td></tr>";
strResp += $"<tr><td>Position %</td><td><input id=\"Position\" type=\"text\" value=\"{_servo.Position}\" /></td><td><input id=\"PositionBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('Position'),'{PageProcess}?{ParamPosition}=')\" /></td></tr>";
strResp += "</table><div id=\"status\"></div></body></html>";
WebServer.OutPutStream(e.Context.Response, strResp);
}
private static string GetCurrentIPAddress()
{
NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0];
// get first NI ( Wifi on ESP32 )
return ni.IPv4Address.ToString();
}
}
}