STM32L4 Board – Sample Code of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 22/02/2021 | STM32L4 SDK 3.9 | Download |
How to run a C sample code STM32L4 Discovery Kit Board?
Prerequisite tools:
- “en.fp-cld-azur.Zip” for installing Prerequisite setup from here (Download)
- STM32CubeIDE or SystemWorkbanch for modification in example file here (Download)
Installation:
- For making own new application, Unzip “en.fp-cld-azure.zip” file and you will get STM32CubeFunctionPack_AZURE1_V5.0.0 folder.
- Now you need to build the project file named .project on path STM32CubeFunctionPack_AZURE1_V5.0.0\Projects\B-L475E-IOT01\Applications\Azure\STM32CubeIDE. That will create some folder and project with STM32CubeIDE IDE software.
- Unzip the package which you download STM32L4 SDK from our IoTConnect documentation “iotconnect-C-sdk-STM32_4.0.zip” and copy Azure folder from this.
- Now replace the Azure folder at path STM32CubeFunctionPack_AZURE1_V5.0.0\Projects\B-L475E-IOT01\Applications\Azure by above cpied folder.
- We used cJSON library, You need to install at this path STM32CubeFunctionPack_AZURE1_V5.0.0\Middlewares\Third_Party\azure-iot-sdk-c\deps\parson. You can get it from here (Download)
- We have main.c(STM32CubeFunctionPack_AZURE1_V5.0.0\Projects\B-L475E-IOT01\Applications\Azure\Src\), you need to input IoTConnect SDK basic configuration information like UniqueId, SID and Connection String.
- In azure_config.h (STM32CubeFunctionPack_AZURE1_V5.0.0\Projects\B-L475E-IOT01\Applications\Azure\Inc\) need to setup WiFi network credentials Network_SSID and Network_PASS.
Usage:
Prerequisite input data
char *Sid = "Your_SDK_ID"; // Setting -> Key Vault -> SDK identities char *ConnectionString = "Device_Connection_String"; // IoTConnect Device Unique ID char *UniqueId = "Your_Device_ID"; // IoTConnect Device Unique ID
“Sid” : It is the SDK Identity ID. It gets from the IoTConnect UI portal “Settings->Key Vault” Then select particular language and version. As a result you will get the Identification number.
“ConnectionString” : It is the Device connection string. It gets from the IoTConnect UI portal “Devices -> Select Device Type -> Click on Device -> Select ‘Info’ tab -> Click on ‘Connection Info’ -> Select ‘Device Connection’ ”
– SdkOptions is for the SDK configuration and needs to parse in SDK object initialize call. You need to manage the below configuration as per your device authentication type.
char *SdkOptions = NULL; cJSON *sdk_object = cJSON_CreateObject(); SdkOptions = cJSON_Print(sdk_object);
Note : No need to define any configuration here for SdkOptions so leave it blank.
– To Initialize the SDK object and connect to the cloud
Init(Sid, ConnectionString, SdkOptions, DeviceCallback, TwinUpdateCallback);
– This is the standard data input format for Gateway and non Gateway device to send the data on IoTConnect cloud(D2C). We need to read and setup all sensor(Attribute) data in JSON format.
void ReadSensordata(void){ char tsStrBuf[24 + 1]; // time format : YYYY-MM-DDTHH:MM:SS.FFFZ eg. 2020-01-21T22:57:59.968Z FormatTime(tsStrBuf, sizeof(tsStrBuf)); cJSON *obj, *root_array, *data, *Gy; root_array = cJSON_CreateArray(); obj = cJSON_CreateObject(); cJSON_AddItemToArray(root_array,obj); cJSON_AddStringToObject(obj, "uniqueId",UniqueId); cJSON_AddStringToObject(obj, "time",tsStrBuf); cJSON_AddItemToObject(obj, "data", data=cJSON_CreateObject()); cJSON_AddNumberToObject(data, "Temperature",56); cJSON_AddStringToObject(data, "SString", "P11"); cJSON_AddItemToObject(data, "Gyroscope", Gy=cJSON_CreateObject()); cJSON_AddStringToObject(Gy, "y", "P22"); cJSON_AddNumberToObject(Gy, "x",44); cJSON_AddNumberToObject(Gy, "z",55); Sensors_Data = cJSON_Print(root_array); cJSON_Delete(root_array); return 0; } /** * For Non Gateway Device payload examples * String Sensors_Data = [{ * "uniqueId": "Device UniqueId", * "time" : "date", * "data": {} * }]; * * For Gateway and multiple child Device payload examples * String Sensors_Data = [{ * "uniqueId": "Gateway Device UniqueId", // It should be first element * "time": "date", * "data": {} * }, * { * "uniqueId":"Child DeviceId", // Child device * "time": "date", * "data": {} * }] */
“time” : Date format should be as defined //”2021-01-24T10:06:17.857Z”
“data” : JSON data type format // {“temperature”: 15.55, “gyroscope” : { ‘x’ : -1.2 }}
– To send the data from Device To Cloud(D2C)
SendSensorData(Sensors_Data);
– To receive the command from Cloud to Device(C2D) and to send the command acknowledgment from device to cloud.
void DeviceCallback(char *payload){ AZURE_PRINTF("\r\n *** DeviceCallback MSG >>Payload : %s\r\n", payload); if(data.cmdType == "0x01") // Device Command if(data.cmdType == "0x02") // Firmware Command if(data.cmdType == "0x16") // Device connection status }
– To send the command acknowledgment from device to cloud.
Ack_Json=cJSON_CreateObject(); cJSON_AddStringToObject(Ack_Json, "ackId", cmd_ackID); cJSON_AddStringToObject(Ack_Json, "msg", ""); cJSON_AddStringToObject(Ack_Json, "childId", cmd_UNI); cJSON_AddNumberToObject(Ack_Json, "st", Status); char *Ack_Json_Data = cJSON_Print(Ack_Json); int msgType = ""; // 5 ("0x01" device command), 11 ("0x02" Firmware OTA command) /** * "ackId(*)" : Command Acknowledgment GUID which will receive from command payload (data.ackId) * "st(*)" : Acknowledgment status sent to cloud (4 = Fail, 6 = Device command[0x01], 7 = Firmware OTA command[0x02]) * "msg" : It is used to send your custom message * "childId" : It is used for Gateway's child device OTA update only * 0x01 : null or "" for Device command * 0x02 : null or "" for Gateway device and mandatory for Gateway child device's OTA update. * How to get the "childId" .? * - You will get child uniqueId for child device OTA command from payload "data.urls[~].uniqueId" * "msgType" : Message type (5 = "0x01" device command, 11 = "0x02" Firmware OTA command) */ SendAck(Ack_Json_Data,msgType);
Note : (*) indicates the mandatory element of the object.
– To receive the twin from Cloud to Device(C2D).
void TwinUpdateCallback(char *payload){ // Here we will get device twin properties payload from cloud to device AZURE_PRINTF("\r\n *** Twin MSG >>Payload : %s\r\n", payload); }
– To get the all twin property Desired and Reported.
GetAllTwins();
Note : Twin update reported property feature is not available in this SDK version
– To disconnect the device from the cloud.
Dispose();