Skip to content

Commit 1dc7f70

Browse files
authored
add direct commands and properties (#377)
* add direct commands and properties Signed-off-by: Ryan Winter <ryanwinter@outlook.com> * add cert include Signed-off-by: Ryan Winter <ryanwinter@outlook.com> * fix redirect link Signed-off-by: Ryan Winter <ryanwinter@outlook.com> --------- Signed-off-by: Ryan Winter <ryanwinter@outlook.com>
1 parent 1d86714 commit 1dc7f70

File tree

2 files changed

+179
-9
lines changed

2 files changed

+179
-9
lines changed

Microchip/ATSAME54-XPRO/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ For guidance on connecting additional devices, see [Getting started with IoT dev
2727
* Ethernet cable
2828
* Optional (for temperature reporting):
2929
* [Weather Click](https://www.mikroe.com/weather-click) sensor. You can add this sensor to the device to monitor weather conditions. If you don't have this sensor, you can still complete this tutorial.
30-
* [mikroBUS Xplained Pro](https://www.microchip.com/Developmenttools/ProductDetails/ATMBUSADAPTER-XPRO) adapter. Use this adapter to attach the Weather Click sensor to the Microchip E54. If you don't have the sensor and this.
30+
* [mikroBUS Xplained Pro](https://www.microchip.com/development-tool/atmbusadapter-xpro) adapter. Use this adapter to attach the Weather Click sensor to the Microchip E54. If you don't have the sensor and this.
3131

3232
## Steps
3333

STMicroelectronics/B-U585I-IOT02A/app/nx_client.c

Lines changed: 178 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <stdio.h>
77

8+
#include "b_u585i_iot02a.h"
89
#include "b_u585i_iot02a_env_sensors.h"
910

1011
#include "nx_api.h"
@@ -15,18 +16,72 @@
1516
#include "azure_iot_nx_client.h"
1617

1718
#include "azure_config.h"
19+
#include "azure_device_x509_cert_config.h"
20+
#include "azure_pnp_info.h"
1821
#include "stm_networking.h"
1922

2023
#define IOT_MODEL_ID "dtmi:azurertos:devkit:gsg;2"
2124

22-
#define TELEMETRY_TEMPERATURE "temperature"
23-
#define TELEMETRY_PRESSURE "pressure"
24-
#define TELEMETRY_HUMIDITY "humidity"
25+
#define TELEMETRY_HUMIDITY "humidity"
26+
#define TELEMETRY_TEMPERATURE "temperature"
27+
#define TELEMETRY_PRESSURE "pressure"
28+
#define TELEMETRY_INTERVAL_PROPERTY "telemetryInterval"
29+
#define LED_STATE_PROPERTY "ledState"
30+
#define SET_LED_STATE_COMMAND "setLedState"
2531

2632
static AZURE_IOT_NX_CONTEXT azure_iot_nx_client;
2733

2834
static int32_t telemetry_interval = 10;
2935

36+
static UINT append_device_info_properties(NX_AZURE_IOT_JSON_WRITER* json_writer)
37+
{
38+
if (nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
39+
(UCHAR*)DEVICE_INFO_MANUFACTURER_PROPERTY_NAME,
40+
sizeof(DEVICE_INFO_MANUFACTURER_PROPERTY_NAME) - 1,
41+
(UCHAR*)DEVICE_INFO_MANUFACTURER_PROPERTY_VALUE,
42+
sizeof(DEVICE_INFO_MANUFACTURER_PROPERTY_VALUE) - 1) ||
43+
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
44+
(UCHAR*)DEVICE_INFO_MODEL_PROPERTY_NAME,
45+
sizeof(DEVICE_INFO_MODEL_PROPERTY_NAME) - 1,
46+
(UCHAR*)DEVICE_INFO_MODEL_PROPERTY_VALUE,
47+
sizeof(DEVICE_INFO_MODEL_PROPERTY_VALUE) - 1) ||
48+
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
49+
(UCHAR*)DEVICE_INFO_SW_VERSION_PROPERTY_NAME,
50+
sizeof(DEVICE_INFO_SW_VERSION_PROPERTY_NAME) - 1,
51+
(UCHAR*)DEVICE_INFO_SW_VERSION_PROPERTY_VALUE,
52+
sizeof(DEVICE_INFO_SW_VERSION_PROPERTY_VALUE) - 1) ||
53+
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
54+
(UCHAR*)DEVICE_INFO_OS_NAME_PROPERTY_NAME,
55+
sizeof(DEVICE_INFO_OS_NAME_PROPERTY_NAME) - 1,
56+
(UCHAR*)DEVICE_INFO_OS_NAME_PROPERTY_VALUE,
57+
sizeof(DEVICE_INFO_OS_NAME_PROPERTY_VALUE) - 1) ||
58+
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
59+
(UCHAR*)DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_NAME,
60+
sizeof(DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_NAME) - 1,
61+
(UCHAR*)DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_VALUE,
62+
sizeof(DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_VALUE) - 1) ||
63+
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
64+
(UCHAR*)DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_NAME,
65+
sizeof(DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_NAME) - 1,
66+
(UCHAR*)DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_VALUE,
67+
sizeof(DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_VALUE) - 1) ||
68+
nx_azure_iot_json_writer_append_property_with_double_value(json_writer,
69+
(UCHAR*)DEVICE_INFO_TOTAL_STORAGE_PROPERTY_NAME,
70+
sizeof(DEVICE_INFO_TOTAL_STORAGE_PROPERTY_NAME) - 1,
71+
DEVICE_INFO_TOTAL_STORAGE_PROPERTY_VALUE,
72+
2) ||
73+
nx_azure_iot_json_writer_append_property_with_double_value(json_writer,
74+
(UCHAR*)DEVICE_INFO_TOTAL_MEMORY_PROPERTY_NAME,
75+
sizeof(DEVICE_INFO_TOTAL_MEMORY_PROPERTY_NAME) - 1,
76+
DEVICE_INFO_TOTAL_MEMORY_PROPERTY_VALUE,
77+
2))
78+
{
79+
return NX_NOT_SUCCESSFUL;
80+
}
81+
82+
return NX_AZURE_IOT_SUCCESS;
83+
}
84+
3085
static UINT append_device_telemetry(NX_AZURE_IOT_JSON_WRITER* json_writer)
3186
{
3287
float temperature;
@@ -38,29 +93,140 @@ static UINT append_device_telemetry(NX_AZURE_IOT_JSON_WRITER* json_writer)
3893
printf("ERROR: BSP_ENV_SENSOR_GetValue\r\n");
3994
}
4095

41-
if (BSP_ENV_SENSOR_GetValue(1, ENV_PRESSURE, &pressure) != BSP_ERROR_NONE)
96+
if (BSP_ENV_SENSOR_GetValue(0, ENV_HUMIDITY, &humidity) != BSP_ERROR_NONE)
4297
{
4398
printf("ERROR: BSP_ENV_SENSOR_GetValue\r\n");
4499
}
45100

46-
if (BSP_ENV_SENSOR_GetValue(0, ENV_HUMIDITY, &humidity) != BSP_ERROR_NONE)
101+
if (BSP_ENV_SENSOR_GetValue(1, ENV_PRESSURE, &pressure) != BSP_ERROR_NONE)
47102
{
48103
printf("ERROR: BSP_ENV_SENSOR_GetValue\r\n");
49104
}
50105

51106
if (nx_azure_iot_json_writer_append_property_with_double_value(
52-
json_writer, (UCHAR*)TELEMETRY_TEMPERATURE, sizeof(TELEMETRY_TEMPERATURE) - 1, temperature, 2) ||
107+
json_writer, (UCHAR*)TELEMETRY_HUMIDITY, sizeof(TELEMETRY_HUMIDITY) - 1, humidity, 2) ||
53108
nx_azure_iot_json_writer_append_property_with_double_value(
54-
json_writer, (UCHAR*)TELEMETRY_PRESSURE, sizeof(TELEMETRY_PRESSURE) - 1, pressure, 2) ||
109+
json_writer, (UCHAR*)TELEMETRY_TEMPERATURE, sizeof(TELEMETRY_TEMPERATURE) - 1, temperature, 2) ||
55110
nx_azure_iot_json_writer_append_property_with_double_value(
56-
json_writer, (UCHAR*)TELEMETRY_HUMIDITY, sizeof(TELEMETRY_HUMIDITY) - 1, humidity, 2))
111+
json_writer, (UCHAR*)TELEMETRY_PRESSURE, sizeof(TELEMETRY_PRESSURE) - 1, pressure, 2))
57112
{
58113
return NX_NOT_SUCCESSFUL;
59114
}
60115

61116
return NX_AZURE_IOT_SUCCESS;
62117
}
63118

119+
static void set_led_state(bool level)
120+
{
121+
if (level)
122+
{
123+
printf("\tLED is turned ON\r\n");
124+
BSP_LED_On(LED_RED);
125+
}
126+
else
127+
{
128+
printf("\tLED is turned OFF\r\n");
129+
BSP_LED_Off(LED_RED);
130+
}
131+
}
132+
133+
static void command_received_cb(AZURE_IOT_NX_CONTEXT* nx_context_ptr,
134+
const UCHAR* component,
135+
USHORT component_length,
136+
const UCHAR* method,
137+
USHORT method_length,
138+
UCHAR* payload,
139+
USHORT payload_length,
140+
VOID* context_ptr,
141+
USHORT context_length)
142+
{
143+
UINT status;
144+
145+
if (strncmp((CHAR*)method, SET_LED_STATE_COMMAND, method_length) == 0)
146+
{
147+
bool arg = (strncmp((CHAR*)payload, "true", payload_length) == 0);
148+
set_led_state(arg);
149+
150+
if ((status = nx_azure_iot_hub_client_command_message_response(
151+
&nx_context_ptr->iothub_client, 200, context_ptr, context_length, NULL, 0, NX_WAIT_FOREVER)))
152+
{
153+
printf("Direct method response failed! (0x%08x)\r\n", status);
154+
return;
155+
}
156+
157+
azure_iot_nx_client_publish_bool_property(&azure_iot_nx_client, NULL, LED_STATE_PROPERTY, arg);
158+
}
159+
else
160+
{
161+
printf("Direct method is not for this device\r\n");
162+
163+
if ((status = nx_azure_iot_hub_client_command_message_response(
164+
&nx_context_ptr->iothub_client, 501, context_ptr, context_length, NULL, 0, NX_WAIT_FOREVER)))
165+
{
166+
printf("Direct method response failed! (0x%08x)\r\n", status);
167+
return;
168+
}
169+
}
170+
}
171+
172+
static void writable_property_received_cb(AZURE_IOT_NX_CONTEXT* nx_context,
173+
const UCHAR* component_name,
174+
UINT component_name_len,
175+
UCHAR* property_name,
176+
UINT property_name_len,
177+
NX_AZURE_IOT_JSON_READER* json_reader_ptr,
178+
UINT version)
179+
{
180+
UINT status;
181+
182+
if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
183+
{
184+
status = nx_azure_iot_json_reader_token_int32_get(json_reader_ptr, &telemetry_interval);
185+
if (status == NX_AZURE_IOT_SUCCESS)
186+
{
187+
printf("Updating %s to %ld\r\n", TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
188+
189+
// Confirm reception back to hub
190+
azure_nx_client_respond_int_writable_property(
191+
nx_context, NULL, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200, version);
192+
193+
azure_nx_client_periodic_interval_set(nx_context, telemetry_interval);
194+
}
195+
}
196+
}
197+
198+
static void property_received_cb(AZURE_IOT_NX_CONTEXT* nx_context,
199+
const UCHAR* component_name,
200+
UINT component_name_len,
201+
UCHAR* property_name,
202+
UINT property_name_len,
203+
NX_AZURE_IOT_JSON_READER* json_reader_ptr,
204+
UINT version)
205+
{
206+
UINT status;
207+
208+
if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
209+
{
210+
status = nx_azure_iot_json_reader_token_int32_get(json_reader_ptr, &telemetry_interval);
211+
if (status == NX_AZURE_IOT_SUCCESS)
212+
{
213+
printf("Updating %s to %ld\r\n", TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
214+
azure_nx_client_periodic_interval_set(nx_context, telemetry_interval);
215+
}
216+
}
217+
}
218+
219+
static void properties_complete_cb(AZURE_IOT_NX_CONTEXT* nx_context)
220+
{
221+
// Device twin processing is done, send out property updates
222+
azure_iot_nx_client_publish_properties(nx_context, DEVICE_INFO_COMPONENT_NAME, append_device_info_properties);
223+
azure_iot_nx_client_publish_bool_property(nx_context, NULL, LED_STATE_PROPERTY, false);
224+
azure_iot_nx_client_publish_int_writable_property(
225+
nx_context, NULL, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
226+
227+
printf("\r\nStarting Main loop\r\n");
228+
}
229+
64230
static void telemetry_cb(AZURE_IOT_NX_CONTEXT* nx_context)
65231
{
66232
azure_iot_nx_client_publish_telemetry(&azure_iot_nx_client, NULL, append_device_telemetry);
@@ -84,6 +250,10 @@ UINT azure_iot_nx_client_entry(
84250
}
85251

86252
// Register the callbacks
253+
azure_iot_nx_client_register_command_callback(&azure_iot_nx_client, command_received_cb);
254+
azure_iot_nx_client_register_writable_property_callback(&azure_iot_nx_client, writable_property_received_cb);
255+
azure_iot_nx_client_register_property_callback(&azure_iot_nx_client, property_received_cb);
256+
azure_iot_nx_client_register_properties_complete_callback(&azure_iot_nx_client, properties_complete_cb);
87257
azure_iot_nx_client_register_timer_callback(&azure_iot_nx_client, telemetry_cb, telemetry_interval);
88258

89259
// Setup authentication

0 commit comments

Comments
 (0)