Unlike cloud-based timers, local timers allow a device to automatically run scheduled tasks even when it is offline. This topic uses a smart outlet as an example to demonstrate how to develop the local timer feature. You can use this example as a reference to implement the local timer feature for any device.
Configure console parameters
- Log on to the IoT Platform console.
Create a project. For more information, see Create a project.
Create a product and define its features. For more information, see Create a product and define its features.
On the page for the product, enable the local timer feature. In the Feature Parameters section, set the maximum number of local timers. This value depends on the device's storage and performance. The default value is 13.
NoteAfter you enable Local Timer, the Device Timer (DeviceTimer) property is automatically added to the feature definitions for the product.
Click Save to complete the configuration.
Develop the local timer feature on the device
Develop the timer feature.
After you define the DeviceTimer property in the console, the device receives property set messages from the cloud. These messages contain the details of the scheduled tasks. The development steps are described below.
NoteThe macro for DeviceTimer is enabled by default in newer versions of the device software development kit (SDK). For older SDK versions, you must enable the macro manually.
Obtain the V1.3.0 IoT Platform SDK. For more information, see Get the SDK.
The sample code for the smart outlet is located in Products/example/smart_outlet/smart_outlet_main.c.
The configuration code for the timer feature macro is located in Products/example/smart_outlet/smart_outlet.mk.
Develop the timer feature using the device SDK.
Confirm that the following macro is enabled.
GLOBAL_CFLAGS += -DENABLE_LOCALTIMER // Macro for local timer in older versions, disabled by defaultChange the default variable parameters in build.sh, as shown in the following code. For more information, see README.md.
Product type: default_type="example" Application name: default_app="smart_outlet" Module model: default_board="uno-91h" // Configure based on the actual model Cloud connection region: default_region=MAINLAND // Set to SINGAPORE for connections outside mainland China Cloud connection environment: default_env=ONLINE Debug log: default_debug=1 // 0: release, 1: debug Other parameters: default_args="" // Configure other compilation parametersRun
./build.shto compile the code and generate the firmware.After the code is successfully compiled, the firmware is ready to be flashed.
Flash the firmware.
The flashing method varies based on the module. For detailed instructions, contact the module manufacturer.
Debug the device.
When the device receives the properties for a scheduled task, you can view the log in user_property_set_event_handler.
static int user_property_set_event_handler(const int devid, const char *request, const int request_len) { int res = 0; user_example_ctx_t *user_example_ctx = user_example_get_ctx(); cJSON *root = NULL, *item = NULL; LOG_TRACE("Property Set Received, Devid: %d, Request: %s", devid, request); if ((root = cJSON_Parse(request)) == NULL) { LOG_TRACE("property set payload is not JSON format"); return -1; } if ((item = cJSON_GetObjectItem(root, "PowerSwitch")) != NULL && cJSON_IsNumber(item)) { if (item->valueint == 1) { product_set_switch(ON); } else { product_set_switch(OFF); } } else { #ifdef AOS_TIMER_SERVICE timer_service_property_set(request); #endif } cJSON_Delete(root); res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY, (unsigned char *)request, request_len); LOG_TRACE("Post Property Message ID: %d", res); return 0; }The following is an example of the data received by the device.
{ "LocalTimer": [ { "PowerSwitch": 1, "Timer": "5 4 1,2,3", "TimezoneOffset": 43200, "Enable": 1, "Targets": "PowerSwitch", "IsValid": 1 }, { "PowerSwitch": 0, "Timer": "", "TimezoneOffset": 43200, "Enable": 0, "Targets": "", "IsValid": 0 }, { "PowerSwitch": 0, "Timer": "", "TimezoneOffset": 43200, "Enable": 0, "Targets": "", "IsValid": 0 }, { "PowerSwitch": 0, "Timer": "", "TimezoneOffset": 43200, "Enable": 0, "Targets": "", "IsValid": 0 }, { "PowerSwitch": 0, "Timer": "", "TimezoneOffset": 43200, "Enable": 0, "Targets": "", "IsValid": 0 } ] }The preceding code shows a JSON array. The LocalTimer array contains five timer records. This number corresponds to the value that you set in Feature Parameters on the page. Each JSON object in the array represents a scheduled task. The following table describes the parameters.
Identifier
Parameter Name
Data Type
Required
Description
PowerSwitch
Power Switch
Boolean
Yes
A standard feature on the product's Function Definition page.
Timer
Scheduled Time
String
Yes
Indicates the scheduled time in cron format. For more information, see the general instructions in Develop the local timer feature for the app.
Enable
Enable
Boolean
Yes
Specifies whether to enable this scheduled task.
IsValid
Executable
Boolean
Yes
Specifies whether this scheduled task is valid. The device sends all data to the app. The app uses this field to determine whether to display the scheduled task to the user.
Targets
Scheduled Action
String
No
Indicates the specific action for the scheduled task. Add the Targets property to set multiple actions. The maximum value is 2048.
TimezoneOffset
Timezone Offset
Integer
No
Indicates the difference between the local event time and UTC.
Unit: seconds.
Value range: -43200 to 50400.
Step size: 3600.
Develop the timer feature for the app
IoT Platform supports native development, which is a static method, for the timer feature in your app. For more information, see Develop the local timer feature for the app.