-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
124 lines (107 loc) · 4.68 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
using AzureIoT_BMP280;
using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.ReadResult;
using nanoFramework.Azure.Devices.Client;
using nanoFramework.Azure.Devices.Shared;
using nanoFramework.Networking;
using System;
using System.Device.Gpio;
using System.Device.I2c;
using System.Device.Wifi;
using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
const string IntervalSeconds = "intervalSeconds";
var gpio = new GpioController();
var led = gpio.OpenPin(4, PinMode.Output); // note: LED connected to GPIO pin 4
var i2cSettings = new I2cConnectionSettings(1, Bmp280.DefaultI2cAddress); // note: using I2C bus 1, GPIO pins: SDA 18, SCL 19
var i2cDevice = I2cDevice.Create(i2cSettings);
Bmp280 bmp280Sensor = null;
try {
bmp280Sensor = new Bmp280(i2cDevice)
{
TemperatureSampling = Sampling.LowPower,
PressureSampling = Sampling.UltraHighResolution
};
} catch (System.IO.IOException ex) {
Debug.WriteLine("BMP280 sensor unavailable.");
}
DeviceClient azureIoT = new(Config.IotHubAddress, Config.DeviceID, Config.SasKey, azureCert: new X509Certificate(Resources.GetBytes(Resources.BinaryResources.AzureRoot)));
Bmp280ReadResult readResult;
bool isTwinUpdated = false;
int intervalSeconds = 60;
while (true) {
DateTime startTime = DateTime.UtcNow;
DateTime continueTime = startTime.AddSeconds(intervalSeconds);
uint blinkPattern = 0xF0F0F0F0;
int patternShift = 0;
try {
led.Write(PinValue.High);
if (bmp280Sensor != null) {
// get temperature and pressure measurements
readResult = bmp280Sensor.Read();
if (readResult != null) {
Debug.WriteLine($"Measurement result obtained. T={readResult.Temperature.DegreesCelsius:f2}°C, P={readResult.Pressure.Hectopascals:f2}hPa");
} else {
Debug.WriteLine("Measurement result unavailable.");
continue;
}
} else {
// simulate sensor measurement
readResult = new Bmp280ReadResult(new UnitsNet.Temperature(20.0, UnitsNet.Units.TemperatureUnit.DegreeCelsius), new UnitsNet.Pressure(1020.0, UnitsNet.Units.PressureUnit.Hectopascal));
Debug.WriteLine("Measurement result simulated.");
}
if (WifiNetworkHelper.Status != NetworkHelperStatus.NetworkIsReady) {
if (WifiNetworkHelper.ConnectDhcp(Config.Ssid, Config.Password, WifiReconnectionKind.Automatic, requiresDateTime: true, token: new CancellationTokenSource(10_000).Token)) {
Debug.WriteLine("WiFi connection succeeded.");
} else {
Debug.WriteLine("WiFi connection failed.");
continue;
}
}
if (!azureIoT.IsConnected) {
if (azureIoT.Open()) {
Debug.WriteLine("Azure connection succeeded.");
} else {
Debug.WriteLine("Azure connection failed.");
continue;
}
}
// get the device twin
var twin = azureIoT.GetTwin(new CancellationTokenSource(5_000).Token);
if ((twin != null) && (twin.Properties.Desired.Contains(IntervalSeconds))) {
intervalSeconds = (int)twin.Properties.Desired[IntervalSeconds];
continueTime = startTime.AddSeconds(intervalSeconds);
}
if (!isTwinUpdated) {
// update reported properties
TwinCollection reported = new();
reported.Add(IntervalSeconds, intervalSeconds);
reported.Add("firmware", "nanoFramework");
reported.Add("firmwareVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
azureIoT.UpdateReportedProperties(reported, new CancellationTokenSource(5_000).Token);
isTwinUpdated = true;
}
// send data to Azure IoT Hub
string payload = $"{{\"temperature\":{readResult.Temperature.DegreesCelsius:f2},\"pressure\":{readResult.Pressure.Hectopascals:f2}}}";
if (azureIoT.SendMessage(payload, new CancellationTokenSource(5_000).Token)) {
Debug.WriteLine("Azure message sent.");
blinkPattern = 0xF0F0CCC0;
} else {
Debug.WriteLine("Azure message send failed.");
continue;
}
} catch (Exception ex) {
Debug.WriteLine("Unhandled exception:");
Debug.WriteLine(ex.Message);
blinkPattern = 0xAAAAAAAA;
}
do {
// wait for the next interval blinking the LED
led.Write(((blinkPattern >> patternShift) & 0x1) == 1 ? PinValue.High : PinValue.Low);
patternShift++;
patternShift %= 32;
Thread.Sleep(100);
} while (DateTime.UtcNow < continueTime);
}