Skip to content
This repository was archived by the owner on Jul 22, 2022. It is now read-only.

Commit df88a5a

Browse files
committed
Update AzureIoTHub for Arduino using the latest azure-iot-sdks version
1 parent 46c321f commit df88a5a

File tree

159 files changed

+13396
-4834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+13396
-4834
lines changed

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
14
language: generic
25
env:
36
global:
4-
- IDE_VERSION=1.6.9
7+
- IDE_VERSION=1.6.8
58
matrix:
69
- BOARD="arduino:samd:arduino_zero_edbg"
710
- BOARD="arduino:samd:mkr1000"
@@ -23,6 +26,7 @@ before_install:
2326
fi
2427
- if [[ "$BOARD" =~ "esp8266:esp8266:" ]]; then
2528
arduino --pref "boardsmanager.additional.urls=http://arduino.esp8266.com/stable/package_esp8266com_index.json" --install-boards esp8266:esp8266;
29+
arduino --pref "boardsmanager.additional.urls=" --save-prefs;
2630
arduino --install-library NTPClient;
2731
arduino --install-library NTPClient;
2832
fi

LICENSE

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c) 2015 Arduino
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
1+
Microsoft Azure IoT SDKs
2+
Copyright (c) Microsoft Corporation
3+
All rights reserved.
4+
MIT License
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the ""Software""), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
1010
furnished to do so, subject to the following conditions:
1111

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
2214

15+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/arduino-libraries/AzureIoTHub.svg?branch=master)](https://travis-ci.org/arduino-libraries/AzureIoTHub)
44

5-
This library is a port of the [Microsoft Azure IoT device SDK for C](https://github.com/Azure/azure-iot-sdks/blob/master/c/readme.md) to Arduino. It allows you to use several Arduino compatible boards with Azure IoT Hub.
5+
This library is a port of the [Microsoft Azure IoT device SDK for C](https://github.com/Azure/azure-iot-sdks/blob/master/c/readme.md) to Arduino. It allows you to use several Arduino compatible boards with Azure IoT Hub. Please submit any contribution directly to [azure-iot-sdks](https://github.com/Azure/azure-iot-sdks).
66

77
Currently supported hardware:
88
- Atmel SAMD Based boards
@@ -13,8 +13,6 @@ Currently supported hardware:
1313
- SparkFun [Thing](https://www.sparkfun.com/products/13711)
1414
- Adafruit [Feather Huzzah](https://www.adafruit.com/products/2821)
1515

16-
[![Microsoft Azure Certified][Microsoft-Azure-Certified-Badge]][azure-certifiedforiot]
17-
1816
## Prerequisites
1917

2018
You should have the following ready before beginning with any board:
@@ -48,6 +46,8 @@ You should have the following ready before beginning with any board:
4846
4. Update Wifi SSID/Password in simplesample_http.ino
4947
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
5048
5. Update IoT Hub Connection string in simplesample_http.c
49+
6. Access the [SparkFun Get Started](https://azure.microsoft.com/en-us/documentation/samples/iot-hub-c-thingdev-getstartedkit/) tutorial to learn more about Microsoft Sparkfun Dev Kit.
50+
7. Access the [Huzzah Get Started](https://azure.microsoft.com/en-us/documentation/samples/iot-hub-c-huzzah-getstartedkit/) tutorial to learn more about Microsoft Huzzah Dev Kit.
5151

5252

5353
## Adafruit Feather M0
@@ -64,6 +64,7 @@ You should have the following ready before beginning with any board:
6464
7. Update Wifi SSID/Password in simplesample_http.ino
6565
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
6666
8. Update IoT Hub Connection string in simplesample_http.c
67+
9. Access the [Feather M0 WiFi Get Started](https://azure.microsoft.com/en-us/documentation/samples/iot-hub-c-m0wifi-getstartedkit/) tutorial to learn more about Microsoft Feather M0 WiFi Dev Kit.
6768

6869
## License
6970

examples/simplesample_http/simplesample_http.c

+98-68
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,23 @@ That does not mean that HTTP only works with the _LL APIs.
1111
Simply changing the using the convenience layer (functions not having _LL)
1212
and removing calls to _DoWork will yield the same results. */
1313

14+
#ifdef ARDUINO
1415
#include "AzureIoTHub.h"
15-
#include "iot_logging.h"
16+
#else
17+
#include "azure_c_shared_utility/threadapi.h"
18+
#include "azure_c_shared_utility/platform.h"
19+
#include "serializer.h"
20+
#include "iothub_client_ll.h"
21+
#include "iothubtransporthttp.h"
22+
#endif
1623

17-
static const char* connectionString = "HostName=[host].azure-devices.net;DeviceId=[device];SharedAccessKey=[key]";
24+
#ifdef MBED_BUILD_TIMESTAMP
25+
#include "certs.h"
26+
#endif // MBED_BUILD_TIMESTAMP
27+
28+
/*String containing Hostname, Device Id & Device Key in the format: */
29+
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
30+
static const char* connectionString = "[device connection string]";
1831

1932
// Define the Model
2033
BEGIN_NAMESPACE(WeatherStation);
@@ -29,36 +42,35 @@ WITH_ACTION(SetAirResistance, int, Position)
2942

3043
END_NAMESPACE(WeatherStation);
3144

32-
DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES)
3345

3446
EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
3547
{
3648
(void)device;
37-
LogInfo("Turning fan on.\r\n");
49+
(void)printf("Turning fan on.\r\n");
3850
return EXECUTE_COMMAND_SUCCESS;
3951
}
4052

4153
EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
4254
{
4355
(void)device;
44-
LogInfo("Turning fan off.\r\n");
56+
(void)printf("Turning fan off.\r\n");
4557
return EXECUTE_COMMAND_SUCCESS;
4658
}
4759

4860
EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
4961
{
5062
(void)device;
51-
LogInfo("Setting Air Resistance Position to %d.\r\n", Position);
63+
(void)printf("Setting Air Resistance Position to %d.\r\n", Position);
5264
return EXECUTE_COMMAND_SUCCESS;
5365
}
5466

5567
void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
5668
{
57-
int messageTrackingId = (intptr_t)userContextCallback;
69+
unsigned int messageTrackingId = (unsigned int)(uintptr_t)userContextCallback;
5870

59-
LogInfo("Message Id: %d Received.\r\n", messageTrackingId);
71+
(void)printf("Message Id: %u Received.\r\n", messageTrackingId);
6072

61-
LogInfo("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
73+
(void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
6274
}
6375

6476
static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
@@ -67,17 +79,17 @@ static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsign
6779
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
6880
if (messageHandle == NULL)
6981
{
70-
LogInfo("unable to create a new IoTHubMessage\r\n");
82+
printf("unable to create a new IoTHubMessage\r\n");
7183
}
7284
else
7385
{
7486
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
7587
{
76-
LogInfo("failed to hand over the message to IoTHubClient");
88+
printf("failed to hand over the message to IoTHubClient");
7789
}
7890
else
7991
{
80-
LogInfo("IoTHubClient accepted the message for delivery\r\n");
92+
printf("IoTHubClient accepted the message for delivery\r\n");
8193
}
8294
IoTHubMessage_Destroy(messageHandle);
8395
}
@@ -93,7 +105,7 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
93105
size_t size;
94106
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
95107
{
96-
LogInfo("unable to IoTHubMessage_GetByteArray\r\n");
108+
printf("unable to IoTHubMessage_GetByteArray\r\n");
97109
result = EXECUTE_COMMAND_ERROR;
98110
}
99111
else
@@ -102,14 +114,16 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
102114
char* temp = malloc(size + 1);
103115
if (temp == NULL)
104116
{
105-
LogInfo("failed to malloc\r\n");
117+
printf("failed to malloc\r\n");
106118
result = EXECUTE_COMMAND_ERROR;
107119
}
108120
else
109121
{
122+
EXECUTE_COMMAND_RESULT executeCommandResult;
123+
110124
memcpy(temp, buffer, size);
111125
temp[size] = '\0';
112-
EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
126+
executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
113127
result =
114128
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
115129
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
@@ -122,94 +136,110 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
122136

123137
void simplesample_http_run(void)
124138
{
125-
if (serializer_init(NULL) != SERIALIZER_OK)
139+
if (platform_init() != 0)
126140
{
127-
LogInfo("Failed on serializer_init\r\n");
141+
printf("Failed to initialize the platform.\r\n");
128142
}
129143
else
130144
{
131-
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
132-
srand((unsigned int)time(NULL));
133-
int avgWindSpeed = 10;
134-
135-
if (iotHubClientHandle == NULL)
145+
if (serializer_init(NULL) != SERIALIZER_OK)
136146
{
137-
LogInfo("Failed on IoTHubClient_LL_Create\r\n");
147+
(void)printf("Failed on serializer_init\r\n");
138148
}
139149
else
140150
{
141-
unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/
142-
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
143-
{
144-
LogInfo("failure to set option \"MinimumPollingTime\"\r\n");
145-
}
151+
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
152+
int avgWindSpeed = 10;
146153

147-
#ifdef MBED_BUILD_TIMESTAMP
148-
// For mbed add the certificate information
149-
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
150-
{
151-
LogInfo("failure to set option \"TrustedCerts\"\r\n");
152-
}
153-
#endif // MBED_BUILD_TIMESTAMP
154+
srand((unsigned int)time(NULL));
154155

155-
ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
156-
if (myWeather == NULL)
156+
if (iotHubClientHandle == NULL)
157157
{
158-
LogInfo("Failed on CREATE_MODEL_INSTANCE\r\n");
158+
(void)printf("Failed on IoTHubClient_LL_Create\r\n");
159159
}
160160
else
161161
{
162-
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
162+
// Because it can poll "after 9 seconds" polls will happen
163+
// effectively at ~10 seconds.
164+
// Note that for scalabilty, the default value of minimumPollingTime
165+
// is 25 minutes. For more information, see:
166+
// https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
167+
unsigned int minimumPollingTime = 9;
168+
ContosoAnemometer* myWeather;
169+
170+
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
163171
{
164-
LogInfo("unable to IoTHubClient_SetMessageCallback\r\n");
172+
printf("failure to set option \"MinimumPollingTime\"\r\n");
173+
}
174+
175+
#ifdef MBED_BUILD_TIMESTAMP
176+
// For mbed add the certificate information
177+
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
178+
{
179+
(void)printf("failure to set option \"TrustedCerts\"\r\n");
180+
}
181+
#endif // MBED_BUILD_TIMESTAMP
182+
183+
myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
184+
if (myWeather == NULL)
185+
{
186+
(void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
165187
}
166188
else
167189
{
168-
myWeather->DeviceId = "myFirstDevice";
169-
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
190+
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
170191
{
171-
unsigned char* destination;
172-
size_t destinationSize;
173-
if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK)
174-
{
175-
LogInfo("Failed to serialize\r\n");
176-
}
177-
else
192+
printf("unable to IoTHubClient_SetMessageCallback\r\n");
193+
}
194+
else
195+
{
196+
myWeather->DeviceId = "myFirstDevice";
197+
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
178198
{
179-
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
180-
if (messageHandle == NULL)
199+
unsigned char* destination;
200+
size_t destinationSize;
201+
if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK)
181202
{
182-
LogInfo("unable to create a new IoTHubMessage\r\n");
203+
(void)printf("Failed to serialize\r\n");
183204
}
184205
else
185206
{
186-
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
207+
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
208+
if (messageHandle == NULL)
187209
{
188-
LogInfo("failed to hand over the message to IoTHubClient");
210+
printf("unable to create a new IoTHubMessage\r\n");
189211
}
190212
else
191213
{
192-
LogInfo("IoTHubClient accepted the message for delivery\r\n");
214+
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
215+
{
216+
printf("failed to hand over the message to IoTHubClient");
217+
}
218+
else
219+
{
220+
printf("IoTHubClient accepted the message for delivery\r\n");
221+
}
222+
223+
IoTHubMessage_Destroy(messageHandle);
193224
}
194-
195-
IoTHubMessage_Destroy(messageHandle);
225+
free(destination);
196226
}
197-
free(destination);
198227
}
199-
}
200228

201-
/* wait for commands */
202-
while (1)
203-
{
204-
IoTHubClient_LL_DoWork(iotHubClientHandle);
205-
ThreadAPI_Sleep(100);
229+
/* wait for commands */
230+
while (1)
231+
{
232+
IoTHubClient_LL_DoWork(iotHubClientHandle);
233+
ThreadAPI_Sleep(100);
234+
}
206235
}
207-
}
208236

209-
DESTROY_MODEL_INSTANCE(myWeather);
237+
DESTROY_MODEL_INSTANCE(myWeather);
238+
}
239+
IoTHubClient_LL_Destroy(iotHubClientHandle);
210240
}
211-
IoTHubClient_LL_Destroy(iotHubClientHandle);
241+
serializer_deinit();
212242
}
213-
serializer_deinit();
243+
platform_deinit();
214244
}
215245
}

0 commit comments

Comments
 (0)