-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-azure-iot-hub.c
106 lines (94 loc) · 2.69 KB
/
arduino-azure-iot-hub.c
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "AzureIoTHub.h"
#include "iot_configs.h"
#include "sample.h"
static const char* connectionString = IOT_CONFIG_CONNECTION_STRING;
static char propText[1024];
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
const char* buf;
size_t len;
// Message content
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buf, &len) != IOTHUB_MESSAGE_OK)
{
(void)printf("Unable to retrieve the message data\r\n");
}
else
{
char* temp = malloc(len + 1);
memcpy(temp, buf, len);
temp[len] = '\0';
(void)printf("Data: [%.*s]\r\n", (int)len, temp);
if(strcmp(temp, "forward") == 0)
{
forward();
}
else if(strcmp(temp, "backward") == 0)
{
backward();
}
else if(strcmp(temp, "left") == 0)
{
left();
}
else if(strcmp(temp, "right") == 0)
{
right();
}
delay(300);
stopcar();
free(temp);
}
return IOTHUBMESSAGE_ACCEPTED;
}
void execute(void)
{
if (platform_init() != 0)
{
(void)printf("Failed to initialize platform.\r\n");
}
else
{
if (serializer_init(NULL) != SERIALIZER_OK)
{
(void)printf("Failed on serializer_init\r\n");
}
else
{
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
srand((unsigned int)time(NULL));
if (iotHubClientHandle == NULL)
{
(void)printf("Failed on IoTHubClient_LL_Create\r\n");
}
else
{
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
// For mbed add the certificate information
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
{
(void)printf("failure to set option \"TrustedCerts\"\r\n");
}
#endif // SET_TRUSTED_CERT_IN_SAMPLES
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &propText) != IOTHUB_CLIENT_OK)
{
printf("unable to IoTHubClient_SetMessageCallback\r\n");
}
else
{
/* wait for commands */
while (1)
{
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(100);
}
}
}
IoTHubClient_LL_Destroy(iotHubClientHandle);
serializer_deinit();
}
platform_deinit();
}
}