-
Notifications
You must be signed in to change notification settings - Fork 0
/
iothubClient.ino
154 lines (138 loc) · 4.34 KB
/
iothubClient.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
static WiFiClientSecure sslClient; // for ESP8266
const char *onSuccess = "\"Successfully invoke device method\"";
const char *notFound = "\"No method found\"";
/*
* The new version of AzureIoTHub library change the AzureIoTHubClient signature.
* As a temporary solution, we will test the definition of AzureIoTHubVersion, which is only defined
* in the new AzureIoTHub library version. Once we totally deprecate the last version, we can take
* the #ifdef out.
* Break changes in version 1.0.34: AzureIoTHub library removed AzureIoTClient class.
* So we remove the code below to avoid compile error.
*/
/*
* #ifdef AzureIoTHubVersion
* static AzureIoTHubClient iotHubClient;
* void initIoThubClient()
* {
* iotHubClient.begin(sslClient);
* }
* #else
* static AzureIoTHubClient iotHubClient(sslClient);
* void initIoThubClient()
* {
* iotHubClient.begin();
* }
* #endif
*/
static void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userContextCallback)
{
if (IOTHUB_CLIENT_CONFIRMATION_OK == result)
{
// Serial.println("Message sent to Azure IoT Hub.");
blinkLED();
}
else
{
Serial.println("Failed to send message to Azure IoT Hub.");
}
messagePending = false;
}
static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, char *buffer, bool temperatureAlert)
{
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char *)buffer, strlen(buffer));
if (messageHandle == NULL)
{
Serial.println("Unable to create a new IoTHubMessage.");
}
else
{
MAP_HANDLE properties = IoTHubMessage_Properties(messageHandle);
Map_Add(properties, "temperatureAlert", temperatureAlert ? "true" : "false");
// Serial.printf("Sending message: %s.\r\n", buffer);
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, NULL) != IOTHUB_CLIENT_OK)
{
Serial.println("Failed to hand over the message to IoTHubClient.");
}
else
{
messagePending = true;
// Serial.println("IoTHubClient accepted the message for delivery.");
}
IoTHubMessage_Destroy(messageHandle);
}
}
void start()
{
Serial.println("Start sending temperature and humidity data.");
messageSending = true;
}
void stop()
{
Serial.println("Stop sending temperature and humidity data.");
messageSending = false;
}
IOTHUBMESSAGE_DISPOSITION_RESULT receiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void *userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
const unsigned char *buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
Serial.println("Unable to IoTHubMessage_GetByteArray.");
result = IOTHUBMESSAGE_REJECTED;
}
else
{
/*buffer is not zero terminated*/
char *temp = (char *)malloc(size + 1);
if (temp == NULL)
{
return IOTHUBMESSAGE_ABANDONED;
}
strncpy(temp, (const char *)buffer, size);
temp[size] = '\0';
Serial.printf("Receive C2D message: %s.\r\n", temp);
free(temp);
blinkLED();
}
return IOTHUBMESSAGE_ACCEPTED;
}
int deviceMethodCallback(const char *methodName, const unsigned char *payload, size_t size, unsigned char **response, size_t *response_size, void *userContextCallback)
{
Serial.printf("Try to invoke method %s.\r\n", methodName);
const char *responseMessage = onSuccess;
int result = 200;
if (strcmp(methodName, "start") == 0)
{
start();
}
else if (strcmp(methodName, "stop") == 0)
{
stop();
}
else
{
Serial.printf("No method %s found.\r\n", methodName);
responseMessage = notFound;
result = 404;
}
*response_size = strlen(responseMessage);
*response = (unsigned char *)malloc(*response_size);
strncpy((char *)(*response), responseMessage, *response_size);
return result;
}
void twinCallback(
DEVICE_TWIN_UPDATE_STATE updateState,
const unsigned char *payLoad,
size_t size,
void *userContextCallback)
{
char *temp = (char *)malloc(size + 1);
for (int i = 0; i < size; i++)
{
temp[i] = (char)(payLoad[i]);
}
temp[size] = '\0';
parseTwinMessage(temp);
free(temp);
}