forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
99 lines (86 loc) · 3.35 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
using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.ReadResult;
using nanoFramework.Azure.Devices.Client;
using nanoFramework.Azure.Devices.Shared;
using nanoFramework.Hardware.Esp32;
using nanoFramework.Networking;
using System;
using System.Device.I2c;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using AzureSDKSleepBMP280;
const string DeviceID = "nanoEdgeTwin";
const string IotHubAddress = "youriothub.azure-devices.net";
const string SasKey = "yoursaskey";
const string Ssid = "your wifi";
const string Password = "your wifi password";
int sleepTimeMinutes = 60000;
int minutesToGoToSleep = 2;
try
{
// Measuring the temperature and pressure
Bmp280ReadResult readResult = null;
// On an ESP32
// I2C# Data Clock
// I2C1 GPIO 18 GPIO 19
const int busId = 1;
I2cConnectionSettings i2cSettings = new(busId, Bmp280.DefaultI2cAddress);
I2cDevice i2cDevice = I2cDevice.Create(i2cSettings);
var i2CBmp280 = new Bmp280(i2cDevice);
// set higher sampling
i2CBmp280.TemperatureSampling = Sampling.LowPower;
i2CBmp280.PressureSampling = Sampling.UltraHighResolution;
readResult = i2CBmp280.Read();
ConnectToWifi();
// If you have uploaded the Azure Certificate on the device, just use:
//DeviceClient azureIoT = new(IotHubAddress, DeviceID, SasKey);
// Otherwise, use:
DeviceClient azureIoT = new(IotHubAddress, DeviceID, SasKey, azureCert: new X509Certificate(Resource.GetString(Resource.StringResources.AzureRootCerts)));
azureIoT.Open();
// Gets the twin
var twin = azureIoT.GetTwin(new CancellationTokenSource(5000).Token);
if ((twin != null) && (twin.Properties.Desired.Contains("TimeToSleep")))
{
minutesToGoToSleep = (int)twin.Properties.Desired["TimeToSleep"];
}
// Report the twins
TwinCollection reported = new();
reported.Add("TimeToSleep", minutesToGoToSleep);
reported.Add("Firmware", "nanoFramework");
azureIoT.UpdateReportedProperties(reported, new CancellationTokenSource(5000).Token);
if (readResult != null)
{
//Publish telemetry data if we have a measure
azureIoT.SendMessage($"{{\"Temperature\":{readResult.Temperature.DegreesCelsius},\"Pressure\":{readResult.Pressure.Hectopascals}}}", new CancellationTokenSource(2000).Token);
}
}
catch
{
// We won't do anything
// This global try catch is to make sure whatever happen, we will safely be able to go
// To sleep
}
// This prevent to debug, once in deep sleep, you won't be able to connect to the device
// So comment to, start with and check what's happening.
// And un comment the next line
////Thread.Sleep(Timeout.Infinite);
// Just go to sleep when we will arrive at this point
GoToSleep();
void GoToSleep()
{
Sleep.EnableWakeupByTimer(TimeSpan.FromMinutes(minutesToGoToSleep));
Sleep.StartDeepSleep();
}
void ConnectToWifi()
{
// As we are using TLS, we need a valid date & time
// We will wait maximum 1 minute to get connected and have a valid date
CancellationTokenSource cs = new(sleepTimeMinutes);
var success = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true, token: cs.Token);
if (!success)
{
// This prevent to debug, once in deep sleep, you won't be able to connect to the device
// So comment to, start with and check what's happening.
GoToSleep();
}
}