5
5
6
6
#include <stdio.h>
7
7
8
+ #include "b_u585i_iot02a.h"
8
9
#include "b_u585i_iot02a_env_sensors.h"
9
10
10
11
#include "nx_api.h"
15
16
#include "azure_iot_nx_client.h"
16
17
17
18
#include "azure_config.h"
19
+ #include "azure_device_x509_cert_config.h"
20
+ #include "azure_pnp_info.h"
18
21
#include "stm_networking.h"
19
22
20
23
#define IOT_MODEL_ID "dtmi:azurertos:devkit:gsg;2"
21
24
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"
25
31
26
32
static AZURE_IOT_NX_CONTEXT azure_iot_nx_client ;
27
33
28
34
static int32_t telemetry_interval = 10 ;
29
35
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
+
30
85
static UINT append_device_telemetry (NX_AZURE_IOT_JSON_WRITER * json_writer )
31
86
{
32
87
float temperature ;
@@ -38,29 +93,140 @@ static UINT append_device_telemetry(NX_AZURE_IOT_JSON_WRITER* json_writer)
38
93
printf ("ERROR: BSP_ENV_SENSOR_GetValue\r\n" );
39
94
}
40
95
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 )
42
97
{
43
98
printf ("ERROR: BSP_ENV_SENSOR_GetValue\r\n" );
44
99
}
45
100
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 )
47
102
{
48
103
printf ("ERROR: BSP_ENV_SENSOR_GetValue\r\n" );
49
104
}
50
105
51
106
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 ) ||
53
108
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 ) ||
55
110
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 ))
57
112
{
58
113
return NX_NOT_SUCCESSFUL ;
59
114
}
60
115
61
116
return NX_AZURE_IOT_SUCCESS ;
62
117
}
63
118
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
+
64
230
static void telemetry_cb (AZURE_IOT_NX_CONTEXT * nx_context )
65
231
{
66
232
azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , NULL , append_device_telemetry );
@@ -84,6 +250,10 @@ UINT azure_iot_nx_client_entry(
84
250
}
85
251
86
252
// 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 );
87
257
azure_iot_nx_client_register_timer_callback (& azure_iot_nx_client , telemetry_cb , telemetry_interval );
88
258
89
259
// Setup authentication
0 commit comments