-
Notifications
You must be signed in to change notification settings - Fork 5
/
GetStarted.ino
210 lines (181 loc) · 6.71 KB
/
GetStarted.ino
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
// To get started please visit https://microsoft.github.io/azure-iot-developer-kit/docs/projects/connect-iot-hub?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode
#include "AZ3166WiFi.h"
#include "AzureIotHub.h"
#include "DevKitMQTTClient.h"
#include "config.h"
#include "utility.h"
#include "SystemTickCounter.h"
static bool hasWifi = false;
int messageCount = 1;
int sentMessageCount = 0;
static bool messageSending = true;
static uint64_t send_interval_ms;
static float temperature;
static float humidity;
// iot-edge-change: Add iot edge root ca cert
static const char edgeCert [] =
"-----BEGIN CERTIFICATE-----\r\n"
"MIIFjTCCA3WgAwIBAgIUUXwWB80z3N9LC2PCyR9kQabPlQ4wDQYJKoZIhvcNAQEL\r\n"
"BQAwKjEoMCYGA1UEAwwfQXp1cmVfSW9UX0h1Yl9DQV9DZXJ0X1Rlc3RfT25seTAe\r\n"
"Fw0xOTA2MjQyMDU0NDRaFw0xOTA3MjQyMDU0NDRaMCoxKDAmBgNVBAMMH0F6dX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DwYD\r\n"
"VR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAYYwDQYJKoZIhvcNAQELBQADggIB\r\n"
"AJYS3m47bhxI+AMynVtnPFpZQjR1EdzN6CDRd93d4JKGPInl41QvRld1gtlFfKM6\r\n"
"evdNVnvxYdYjjfh3hmqR6qTXhZEv9/RkdSVaBnkyLyyNdWM8EJjCq+gJNJxH8FUK\r\n"
"SgBFz8fG686ipFxhkczCzdPnmGGDe6JH98i0DqGWeJS1BZZ6XqQ4XJdPJnvD2YKq\r\n"
"LNYQ+vkEJubmP6m0622p5P/+gX7mlvAfT5vZqkVwcYnoXI6gxFG5zKYm0zwtQtVt\r\n"
"N2vn5qY94X0BFRGbUNxRQRuDeZFTW5o5pxy6sqCv7pwP2RPvn/QgOfQxGGVsM+hf\r\n"
"1m8pv8wycCqzD38UjBsKhwraAE1LbDoxLlGVIxV/yV6DRnwSFt1aGOSddT108A5y\r\n"
"AcbShd8eMt+XxlYqdM8bdlPbFwNoXE/kswfc3+ssRORPyxP95gsU6mOOwIOeXOVU\r\n"
"bKWbLszQTtcpvZyhbKeDBgeHietEpeGIM6zfSDHisvikw6FZpuVLl32FAuA5B8B6\r\n"
"3i78CfSQ5E7Yem90fQLDJ0zpFFbIIlK15sHX3F3oNxWnskiTjys7cfDH6JaJt/Ww\r\n"
"Nq/sIoTM0Cq5QXeVYWnj2R2TVcXykqWbq06ZCXtRb/mt8uTA/8/gk1WdVXpoOr5F\r\n"
"xrx00XeUesvvgu1vGDyYMDjZAQtMSvcLLootLCmSPsEK\r\n"
"-----END CERTIFICATE-----";
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Utilities
static void InitWifi()
{
Screen.print(2, "Connecting...");
if (WiFi.begin() == WL_CONNECTED)
{
IPAddress ip = WiFi.localIP();
Screen.print(1, ip.get_address());
hasWifi = true;
Screen.print(2, "Running... \r\n");
}
else
{
hasWifi = false;
Screen.print(1, "No Wi-Fi\r\n ");
}
}
static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result)
{
if (result == IOTHUB_CLIENT_CONFIRMATION_OK)
{
blinkSendConfirmation();
sentMessageCount++;
}
Screen.print(1, "> IoT Hub");
char line1[20];
sprintf(line1, "Count: %d/%d",sentMessageCount, messageCount);
Screen.print(2, line1);
char line2[20];
sprintf(line2, "T:%.2f H:%.2f", temperature, humidity);
Screen.print(3, line2);
messageCount++;
}
static void MessageCallback(const char* payLoad, int size)
{
blinkLED();
Screen.print(1, payLoad, true);
}
static void DeviceTwinCallback(DEVICE_TWIN_UPDATE_STATE updateState, const unsigned char *payLoad, int size)
{
char *temp = (char *)malloc(size + 1);
if (temp == NULL)
{
return;
}
memcpy(temp, payLoad, size);
temp[size] = '\0';
parseTwinMessage(updateState, temp);
free(temp);
}
static int DeviceMethodCallback(const char *methodName, const unsigned char *payload, int size, unsigned char **response, int *response_size)
{
LogInfo("Try to invoke method %s", methodName);
const char *responseMessage = "\"Successfully invoke device method\"";
int result = 200;
if (strcmp(methodName, "start") == 0)
{
LogInfo("Start sending temperature and humidity data");
messageSending = true;
}
else if (strcmp(methodName, "stop") == 0)
{
LogInfo("Stop sending temperature and humidity data");
messageSending = false;
}
else
{
LogInfo("No method %s found", methodName);
responseMessage = "\"No method found\"";
result = 404;
}
*response_size = strlen(responseMessage) + 1;
*response = (unsigned char *)strdup(responseMessage);
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arduino sketch
void setup()
{
Screen.init();
Screen.print(0, "IoT DevKit");
Screen.print(2, "Initializing...");
Screen.print(3, " > Serial");
Serial.begin(115200);
// Initialize the WiFi module
Screen.print(3, " > WiFi");
hasWifi = false;
InitWifi();
if (!hasWifi)
{
return;
}
LogTrace("HappyPathSetup", NULL);
Screen.print(3, " > Sensors");
SensorInit();
Screen.print(3, " > IoT Hub");
DevKitMQTTClient_SetOption(OPTION_MINI_SOLUTION_NAME, "DevKit-GetStarted");
//iot-edge-change: add the root ca cert to the MQTT 'trusted' certs
DevKitMQTTClient_SetOption("TrustedCerts", edgeCert);
DevKitMQTTClient_Init(true);
DevKitMQTTClient_SetSendConfirmationCallback(SendConfirmationCallback);
DevKitMQTTClient_SetMessageCallback(MessageCallback);
DevKitMQTTClient_SetDeviceTwinCallback(DeviceTwinCallback);
DevKitMQTTClient_SetDeviceMethodCallback(DeviceMethodCallback);
send_interval_ms = SystemTickCounterRead();
}
void loop()
{
if (hasWifi)
{
if (messageSending &&
(int)(SystemTickCounterRead() - send_interval_ms) >= getInterval())
{
// Send teperature data
char messagePayload[MESSAGE_MAX_LEN];
bool temperatureAlert = readMessage(messageCount, messagePayload, &temperature, &humidity);
LogInfo(messagePayload);
EVENT_INSTANCE* message = DevKitMQTTClient_Event_Generate(messagePayload, MESSAGE);
// iot-edge-change: comment out this property
// DevKitMQTTClient_Event_AddProp(message, "temperatureAlert", temperatureAlert ? "true" : "false");
DevKitMQTTClient_SendEventInstance(message);
send_interval_ms = SystemTickCounterRead();
}
else
{
DevKitMQTTClient_Check();
}
}
delay(1000);
}