Contributed by OldNewman, organized by Ai-Thinker
[AI-WB2-32S-Kit Review] 04 Reporting Temperature and Humidity to the Alibaba Cloud IoT Platform
Alibaba Cloud IoT Platform
Introduction
The Alibaba Cloud IoT Platform is a one-stop IoT solution launched by Alibaba Cloud, helping enterprises achieve intelligent device connection, management and data processing. Its core features include:
- Device access and management: supports multiple protocols and provides full device lifecycle management.
- Data collection and processing: collects, stores and processes device data in real time, with rule engine support.
- Security: provides identity authentication, data encryption and permission control.
- Cloud-edge collaboration: supports edge computing to meet low-latency requirements.
- Data analysis and visualization: unlocks the value of data, providing intuitive reports and dashboards.
- Open ecosystem: compatible with third-party services and seamlessly integrated with other Alibaba Cloud products.
Hands-on on the Cloud Platform
Creating a Product
Log in to the Alibaba Cloud IoT Platform https://iot.console.aliyun.com/product
Enter the device management interface and select Product --> Create Product

Creating a New Product
On the create product page, select or fill in the following information in order:
- Product name: WB2-32S-Kit-IOT
- Category: custom category (convenient for later modification of the thing model)
- Node type: direct-connect device
- Network method: Wi-Fi
- Data format: ICA standard data format (Alink JSON)
- Verification type: weak verification
- Authentication method: device secret
- Product description: add a description, e.g. Ai-Thinker wireless product

After clicking Confirm, the following page pops up. Don't rush to Add Device; instead click Feature Definition to set up the thing model.

Feature Definition (Thing Model)
The feature definition interface pops up; click Go to Edit Draft

Adding Features
- Add standard features
- Other types
- Enter "Current Temperature" and press Enter to start searching for matching types
- From the list, select "Current Temperature", the feature whose category is "Air Box", and it is added to Selected Features on the right
- Click OK

Following the steps above, add the "Current Temperature" and "Current Humidity" features in turn.

After adding the features, be sure to pay attention to the prompt at the bottom: Publish for them to officially take effect.


Adding a Device (for the device triple)
Go to the Device Development tab and select Add Device

Manually Add a Device Name


View the Device Triple
The current device certificate uses one-device-one-secret. In the SDK, ProductKey, DeviceName and DeviceSecret must be provided for successful registration.

The Topic Corresponding to the Thing Model

Scroll down and find Thing Model Communication Topic-->Property Report

Find the request Topic under Alink JSON
The message type corresponding to /sys/

Device-side Development for the IoT Platform
For chips or modules, you would normally need to adapt the IoT platform device-side SDK yourself. Below is the official C Link SDK, which can be downloaded and ported.

However, the Ai-WB2-32S-Kit SDK has already been ported; just modify it and it works. See the Ai-Thinker-WB2/applications/iot-solution/ali_iot project.
ali_iot/
Click to expand full code
ali\_cloud/
ali\_csdk/ -- 阿里物联网平台 SDK 核心
components/
core/
demos/
external/
portfiles/
ali\_mqtt/ -- MQTT 应用,用户在这里订阅和发布TOPIC
ali\_ntp/ -- NTP 线程
ali\_ota/ -- 阿里物联网平台 OTA 功能实现
main/ -- 用户程序入口Reporting to the Cloud Platform
Hardware
The wiring between the DHT11 temperature and humidity sensor and the AI-WB2-32S-Kit is as follows
| DHT11 | WB2-32S-Kit |
|---|---|
| 3V3 | 3V3 |
| GND | GND |
| NC | NC |
| I/O | IO12 |
Physical Photos


Flow
Main Flow
After power-on, the main() function does two things:
- tcpip_init() starts the LwIP protocol stack
- Create the proc_main_entry() thread,
In the proc_main_entry() thread, after WiFi scan/connection is complete and an IP is obtained, ali_linkkit_main() is called to start the Alibaba Cloud IoT Platform MQTT connection thread:
Click to expand full code
注册 event_cb_wifi_event() ,这是 WiFi 协议栈的回调函数
调用 hal_wifi_start_firmware_task() 启动 WiFi 协议栈
调用 aos_post_event(CODE_WIFI_ON_INIT_DONE) 通知 WiFi 协议栈硬件初始化完毕The key function ali_linkkit_main():
- Call aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile) to register Ai-Thinker's adaptation layer for the Alibaba Cloud platform, including memory allocation/release, Mutex, time, network and other APIs
- Call the ali_mqtt_init() function, an application-layer function that starts the MQTT connection to Alibaba Cloud and subscribes/publishes messages

MQTT Flow

The interaction point between user code and the MQTT flow is: the user calls start_update_data(), which actually triggers the aiot_mqtt_send_thread() thread, which finally calls pub_msg() to upload thing model messages to the cloud platform.
Key Code
The main() function creates a thread that reads the DHT11 temperature and humidity data every 3 seconds, then calls start_update_data() to notify the MQTT thread.
Click to expand full code
static
void
proc_main_entry
(
void
* pvParameters)
{
aos_register_event_filter
(EV_WIFI, event_cb_wifi_event,
NULL
);
hal_wifi_start_firmware_task
();
aos_post_event
(EV_WIFI, CODE_WIFI_ON_INIT_DONE,
0
);
dht11_basic_init
();
while
(
1
) {
// Post data to AliCloud
aos_msleep
(
3000
);
if
(
0
==
dht11_basic_read
(&g_temperature, &g_humidity)) {
// Read successfully
blog_info
(
"%.2f C degree\t%u%%"
, g_temperature, g_humidity);
start_update_data
();
}
else
{
blog_error
(
"Read failed"
);
}
}
vTaskDelete
(
NULL
);
}The MQTT thing-model upload thread aiot_mqtt_send_thread() waits for the semaphore, then calls pub_msg() to upload.
Click to expand full code
/* 发送实时参数 */
void
aiot_mqtt_send_thread
(
void
* pvParameters)
{
// int32_t res = STATE_SUCCESS;
xSemaphore_send =
xSemaphoreCreateBinary
();
if
(xSemaphore_send ==
NULL
) {
printf
(
"create fail\r\n"
);
}
while
(g_mqtt_process_thread_running) {
if
(xSemaphore_send !=
NULL
) {
/* See if we can obtain the semaphore. If the semaphore is not
available wait 10 ticks to see if it becomes free. */
if
(
xSemaphoreTake
(xSemaphore_send, portMAX_DELAY) == pdTRUE) {
// 这里上传数据
pub_msg
(pvParameters);
}
}
}
vTaskDelete
(
NULL
);
}pub_msg() constructs a JSON string and reports the temperature and humidity in thing model message format, finally calling aiot_mqtt_pub() to upload the MQTT message.
Click to expand full code
/**
* @brief 上报物模型消息到云平台
*
* @param handle
*/
void
pub_msg
(
void
* handle)
{
static
uint32_t
id =
1
;
char
* version =
"1.0"
;
// format: sys/${productKey}/${deviceName}/thing/event/property/post
memset
(g_topic_property_post,
0
,
sizeof
(g_topic_property_post));
sprintf
(g_topic_property_post,
"/sys/%s/%s/thing/event/property/post"
, g_product_key, g_device_name);
printf
(
"g_topic_property_post=%s\r\n"
, g_topic_property_post);
//
TODO:
此处换行导致 g_pub_msg 占用字节特比多,552 个字节。如果调用 cJSON 或者重新排版,可以减少字节个数
sprintf
((
char
*)g_pub_msg,
"{ \
\"id\": \"%u\", \
\"version\" : \"%s\", \
\"method\" : \"thing.event.property.post\", \
\"params\" : { \
\"CurrentTemperature\": { \
\"value\": %.2f \
}, \
\"CurrentHumidity\" : { \
\"value\": %u \
} \
} \
}"
,
id++, version, g_temperature, g_humidity);
printf
(
"g_pub_msg [len=%u] =%s\r\n"
,
strlen
(g_pub_msg), g_pub_msg);
int32_t
res =
aiot_mqtt_pub
(handle, g_topic_property_post, (
uint8_t
*)g_pub_msg,
strlen
(g_pub_msg),
0
);
if
(res <
0
) {
printf
(
"aiot_mqtt_pub failed, res: -0x%04X\n"
, -res);
}
}IoT Platform Logs

Temperature Curve
The protruding values are where I pinched the DHT11, causing the temperature to rise.

Humidity Curve
The protruding parts of the curve are where I pinched the DHT11, increasing the humidity.

Review Summary
Fortunately, Ai-Thinker has already adapted the Ali C SDK; otherwise, adapting the Alibaba Cloud IoT Platform from scratch would take a lot of time.
Creating products and defining features is actually quite similar across IoT platforms.

