-
Notifications
You must be signed in to change notification settings - Fork 0
/
programv4_con_hilo.cs
318 lines (287 loc) · 13 KB
/
programv4_con_hilo.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.Hardware.Esp32;
using System.Device.I2c;
using Iot.Device.Ahtxx;
using Iot.Device.Ssd13xx.Samples;
using System.Device.Wifi;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Text;
using Iot.Device.Ssd13xx;
using System.Device.Gpio;
using Primer_proyecto_04_02;
using Iot.Device.Rtc;
namespace proyect
{
class Program
{
//static GpioPin Button;
//static GpioController gpioControl;
static int ConnCount = 0;
// Set the SSID & Password to your local Wifi network
const string MYSSID = "Familia_Rivera";
const string MYPASSWORD = "Juanito2001";
static bool WifiConnected = false;
static Aht10 aht10;
static Ssd1306 oled;
static Ds1307 rtc;
static int I2cBus = 1;
public static void i2conf(int bus, int sda, int sdb)
{
Configuration.SetPinFunction(sda, DeviceFunction.I2C1_DATA);
Configuration.SetPinFunction(sdb, DeviceFunction.I2C1_CLOCK);
I2cBus = bus;
}
public static void aht10_conf()
{
I2cConnectionSettings i2cSettings = new(I2cBus, Aht10.DefaultI2cAddress);
I2cDevice i2cDevice = I2cDevice.Create(i2cSettings);
Aht10 aht10_ = new Aht10(i2cDevice);
aht10 = aht10_;
}
public static void oled_conf()
{
I2cConnectionSettings i2cSettings_ = new I2cConnectionSettings(I2cBus, Ssd1306.DefaultI2cAddress);
I2cDevice i2cDevice_ = I2cDevice.Create(i2cSettings_);
Ssd1306 oled_ = new Ssd1306(i2cDevice_, Ssd13xx.DisplayResolution.OLED128x64);
oled = oled_;
oled.ClearScreen();
oled.Font = new BasicFont();
}
public static void rtc_conf()
{
I2cConnectionSettings settings = new I2cConnectionSettings(1, Ds1307.DefaultI2cAddress);
I2cDevice device = I2cDevice.Create(settings);
Ds1307 rtc_ = new Ds1307(device);
//rtc_.DateTime = new DateTime(2023, 5, 19, 4, 38, 30);
rtc = rtc_;
}
static void Main(string[] args)
{
//gpioControl = new GpioController();
//Button = gpioControl.OpenPin(0, PinMode.InputPullUp);
//Button.DebounceTimeout = new TimeSpan(0, 0, 0, 0, 100);
////Button.ValueChanged += Button_ValueChanged;
//gpioControl.RegisterCallbackForPinValueChangedEvent(0, PinEventTypes.Falling, Button_ValueChanged);
i2conf(1, 21, 22);
aht10_conf();
oled_conf();
rtc_conf();
new Thread(display_oled).Start();
try
{
// Get the first WiFI Adapter
WifiAdapter wifi = WifiAdapter.FindAllAdapters()[0];
// Set up the AvailableNetworksChanged event to pick up when scan has completed
wifi.AvailableNetworksChanged += Wifi_AvailableNetworksChanged;
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
// give it some time to perform the initial "connect"
// trying to scan while the device is still in the connect procedure will throw an exception
Thread.Sleep(10_000);
// Loop forever scanning every 30 seconds
while (!WifiConnected)
{
try
{
Console.WriteLine("starting Wi-Fi scan");
wifi.ScanAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Failure starting a scan operation: {ex}");
}
Thread.Sleep(30000);
}
}
catch (Exception ex)
{
Console.WriteLine("message:" + ex.Message);
Console.WriteLine("stack:" + ex.StackTrace);
}
HttpListener Listener = new HttpListener("http", 80);
Listener.Start();
Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint EndPort = new IPEndPoint(IPAddress.Any, 1234);
Server.Bind(EndPort);
Server.Listen(10);
HttpListenerContext Request = Listener.GetContext();
new Thread(() => ProcessRequest(Request)).Start();
while (true)
{
Socket Connection = Server.Accept();
ConnCount++;
new Thread(() => ProcessRequest(Connection, ConnCount)).Start();
if (Connection != null)
{
Console.WriteLine(DateTime.UtcNow.ToString() + " | New Socket Connection from: " + Connection.RemoteEndPoint.ToString() + ", Local: " + Connection.LocalEndPoint.ToString());
Connection.Send(UTF8Encoding.UTF8.GetBytes("Hola Mundo"));
Thread.Sleep(1000);
Connection.Close();
}
// -----------------------------------------------------------
// ---------------------------------------------------------
}
}
private static void Button_ValueChanged(object sender, PinValueChangedEventArgs e)
{
Console.WriteLine("Button Value Change Event: " + e.ChangeType.ToString());
}
private static void ProcessRequest(HttpListenerContext Context)
{
try
{
if (Context != null)
{
if (Context.Request != null)
{
Console.WriteLine(Context.Request.RawUrl);
Console.WriteLine(Context.Request.HttpMethod);
foreach (string item in Context.Request.Headers.AllKeys)
{
Console.WriteLine(item + " : " + Context.Request.Headers[item]);
}
if (Context.Request.HttpMethod == "GET")
{
switch (Context.Request.RawUrl)
{
case "/":
if (Context.Response != null)
{
//string Respuesta = "<HTML><BODY>Hola Mundo, Respuesta HTTP desde NanoFrameWork " + DateTime.UtcNow.ToString() + ".</BODY></HTML>";
string Respuesta = Resource1.GetString(Resource1.StringResources.PaginaHome);
if (Respuesta.Contains("1"))
{
int Index = Respuesta.IndexOf("1");
string s = Respuesta.Substring(0, Index);
s += DateTime.UtcNow.ToString();
s += Respuesta.Substring(Index + 3);
Respuesta = s;
}
byte[] Buffer = UTF8Encoding.UTF8.GetBytes(Respuesta);
Context.Response.ContentLength64 = Buffer.Length;
Context.Response.OutputStream.Write(Buffer, 0, Buffer.Length);
Context.Response.Close();
}
break;
case "/favicon.ico":
if (Context.Response != null)
{
byte[] Buffer = Resource1.GetBytes(Resource1.BinaryResources.favicon);
Context.Response.ContentLength64 = Buffer.Length;
Context.Response.OutputStream.Write(Buffer, 0, Buffer.Length);
Context.Response.Close();
}
break;
default:
if (Context.Response != null)
{
Context.Response.ContentLength64 = 0;
Context.Response.Close();
}
break;
}
}
}
Context.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void ProcessRequest(Socket Connection, int Count)
{
try
{
if (Connection != null)
{
Console.WriteLine(DateTime.UtcNow.ToString() + " | New Socket Connection from: " + Connection.RemoteEndPoint.ToString() + ", Local: " + Connection.LocalEndPoint.ToString());
Connection.Send(UTF8Encoding.UTF8.GetBytes("Hola Mundo\n"));
//Thread.Sleep(1000);
Connection.Close();
Console.WriteLine("Socket de Conexion " + Count + " Finalizado");
Thread.Sleep(10000);
Console.WriteLine("Hilo de Conexion " + Count + " Finalizado");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
Console.WriteLine("Network Connection Ready");
}
else
{
Console.WriteLine("Network Connection Lost");
}
}
private static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface intf in Interfaces)
{
Console.WriteLine("Interface: " + intf.NetworkInterfaceType + ", IP Address: " + intf.IPv4Address.ToString());
}
}
private static void Wifi_AvailableNetworksChanged(WifiAdapter sender, object e)
{
Console.WriteLine("Wifi_AvailableNetworksChanged - get report");
// Get Report of all scanned Wifi networks
WifiNetworkReport report = sender.NetworkReport;
// Enumerate though networks looking for our network
foreach (WifiAvailableNetwork net in report.AvailableNetworks)
{
// Show all networks found
Console.WriteLine($"Net SSID :{net.Ssid}, BSSID : {net.Bsid}, rssi : {net.NetworkRssiInDecibelMilliwatts.ToString()}, signal : {net.SignalBars.ToString()}");
// If its our Network then try to connect
if (net.Ssid == MYSSID)
{
// Disconnect in case we are already connected
sender.Disconnect();
// Connect to network
WifiConnectionResult result = sender.Connect(net, WifiReconnectionKind.Automatic, MYPASSWORD);
// Display status
if (result.ConnectionStatus == WifiConnectionStatus.Success)
{
Console.WriteLine("Connected to Wifi network");
WifiConnected = true;
break;
}
else
{
Console.WriteLine($"Error {result.ConnectionStatus.ToString()} connecting o Wifi network");
}
}
}
}
static void display_oled()
{
while (true)
{
string textTemp = $"temp: {aht10.GetTemperature().DegreesCelsius:F1}C";
string textHum = $"hum: {aht10.GetHumidity().Percent:F0}%";
DateTime dt = rtc.DateTime;
string time = dt.ToString("yyyy/MM/dd HH:mm:ss");
string fecha = time.Substring(0, 11);
string hora = time.Substring(11, 8);
oled.Write(0, 0, textTemp, 1, false);
oled.Write(0, 1, textHum, 1, false);
oled.Write(0, 2, "fecha-hora", 1, false);
oled.Write(0, 3, fecha, 1, false);
oled.Write(0, 4, hora, 1, false);
oled.Display();
Thread.Sleep(10);
}
}
}
}