1、阿里云创建物模型
- 选择产品,点击查看
- 选择“功能定义”,再点击“编辑草稿”
- 选择“默认模块”,再点击“添加自定义功能”
- 选择“属性”并填写完相应的参数
- 重复步骤3,添加“花园温度”、“花园光照”
- 添加完自定义功能后,点击左下角“发布上线”
- 添加完成
2、阿里云物模型在线调试
- 在左侧选择“设备模拟器”,
- 启动设备模拟器后,模拟ESP8266上传数据给阿里云
- 数据发送成功
- 在阿里云平台左侧中选择“设备”,可查看刚才模拟发送的数据
3、使用ESP8266上传数据到阿里云
在ESP8266基础应用之使用ESP8266模块接入阿里云物联网平台工程基础上测试
3.1、上传数据到阿里云
- 参考物模型在线调试数据发送的JSON格式
{"id":1616740619396,"params":{"LightLux":33,"temp":22,"humidity":11},"version":"1.0","method":"thing.event.property.post"}
- 在ESP8266工程中使用乐鑫官方JSON库建立JSON树
LOCAL int ICACHE_FLASH_ATTR
lth_value_get(struct jsontree_context *js_ctx)
{
const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
static uint32 tsl_id = 11223344;
static uint8 lightlux,temp,humidity;
//模拟温度、湿度、光照、id
if((lightlux++) > 40) lightlux = 0;
if((temp++) > 40) temp = 0;
if((humidity++) > 40) humidity = 0;
if((tsl_id++) > 11223366) tsl_id = 11223344;
if (os_strncmp(path, "id", 2) == 0) {
jsontree_write_int(js_ctx, tsl_id);
} else if (os_strncmp(path, "LightLux", 8) == 0) {
jsontree_write_int(js_ctx, lightlux);
} else if (os_strncmp(path, "temp", 4) == 0) {
jsontree_write_int(js_ctx, temp);
} else if (os_strncmp(path, "humidity", 8) == 0) {
jsontree_write_int(js_ctx, humidity);
} else if (os_strncmp(path, "version", 7) == 0) {
jsontree_write_string(js_ctx, "1.0");
} else if (os_strncmp(path, "method", 6) == 0) {
jsontree_write_string(js_ctx, "thing.event.property.post");
}
return 0;
}
LOCAL struct jsontree_callback lth_value_callback =
JSONTREE_CALLBACK(lth_value_get ,NULL);
JSONTREE_OBJECT(set_lth_tree,
JSONTREE_PAIR("LightLux", <h_value_callback),
JSONTREE_PAIR("temp", <h_value_callback),
JSONTREE_PAIR("humidity", <h_value_callback));
JSONTREE_OBJECT(lth_tree,
JSONTREE_PAIR("id", <h_value_callback),
JSONTREE_PAIR("params", &set_lth_tree),
JSONTREE_PAIR("version", <h_value_callback),
JSONTREE_PAIR("method", <h_value_callback));
void ICACHE_FLASH_ATTR
user_lth_jsontree_setup(char *pbuf)
{
struct jsontree_context js;
jsontree_setup(&js, (struct jsontree_value *)<h_tree, json_putchar);
os_memset(pbuf, 0, os_strlen(pbuf));
json_ws_send((struct jsontree_value *)<h_tree, "{", pbuf);
}
- 使用定时器定时发送数据
#define AliyunSubscribeTopic_property_post_reply "/sys/a1tUbQR2faQ/dev-esp8266/thing/event/property/post_reply"
#define AliyunPublishTopic_property_post "/sys/a1tUbQR2faQ/dev-esp8266/thing/event/property/post"
char lth_pbuf[150];
/******************************************************************************
* FunctionName :
* Description :
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
hw_timer_cb(void)
{
static uint8_t hw_timer_cnt;
if (wifi_getState == STATION_GOT_IP)
{
if((hw_timer_cnt++) >= 30) //每3秒发送一次
{
hw_timer_cnt = 0;
os_printf("---hw timer inter \n");
if(client != NULL)
{
user_lth_jsontree_setup(lth_pbuf);
MQTT_Publish(client, AliyunPublishTopic_property_post, lth_pbuf, os_strlen(lth_pbuf), 0, 0);//发布消息
}
else
{
os_printf("---client is null \n");
}
}
}
}
- 烧录程序后,观察阿里云平台数据变化
也可以点击“查看数据”,查看曲线形式
3.2、解析阿里云反馈的数据
- 在串口调试助手查看阿里云反馈的数据信息
- 数据格式如下,JSON格式
{"code":200,"data":{},"id":"11223357","message":"success","method":"thing.event.property.post","version":"1.0"}
- 在ESP8266工程中解析阿里云反馈的数据
暂时没有做出来……