ESP8266基础应用之阿里云物模型的应用

1、阿里云创建物模型

  1. 选择产品,点击查看在这里插入图片描述
  2. 选择“功能定义”,再点击“编辑草稿”在这里插入图片描述
  3. 选择“默认模块”,再点击“添加自定义功能”在这里插入图片描述
  4. 选择“属性”并填写完相应的参数在这里插入图片描述
  5. 重复步骤3,添加“花园温度”、“花园光照”在这里插入图片描述
  6. 添加完自定义功能后,点击左下角“发布上线”在这里插入图片描述
    在这里插入图片描述
  7. 添加完成在这里插入图片描述

2、阿里云物模型在线调试

  1. 在左侧选择“设备模拟器”,在这里插入图片描述
  2. 启动设备模拟器后,模拟ESP8266上传数据给阿里云在这里插入图片描述
  3. 数据发送成功在这里插入图片描述
  4. 在阿里云平台左侧中选择“设备”,可查看刚才模拟发送的数据在这里插入图片描述

3、使用ESP8266上传数据到阿里云

ESP8266基础应用之使用ESP8266模块接入阿里云物联网平台工程基础上测试

3.1、上传数据到阿里云

  1. 参考物模型在线调试数据发送的JSON格式
{"id":1616740619396,"params":{"LightLux":33,"temp":22,"humidity":11},"version":"1.0","method":"thing.event.property.post"}
  1. 在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", &lth_value_callback),
                JSONTREE_PAIR("temp", &lth_value_callback),
                JSONTREE_PAIR("humidity", &lth_value_callback));

JSONTREE_OBJECT(lth_tree,
                JSONTREE_PAIR("id", &lth_value_callback),
                JSONTREE_PAIR("params", &set_lth_tree),
                JSONTREE_PAIR("version", &lth_value_callback),
                JSONTREE_PAIR("method", &lth_value_callback));

void ICACHE_FLASH_ATTR
user_lth_jsontree_setup(char *pbuf)
{
   	struct jsontree_context js;

    jsontree_setup(&js, (struct jsontree_value *)&lth_tree, json_putchar);
	os_memset(pbuf, 0, os_strlen(pbuf));
	json_ws_send((struct jsontree_value *)&lth_tree, "{", pbuf);
}
  1. 使用定时器定时发送数据
#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");
			}
		}
	}
}
  1. 烧录程序后,观察阿里云平台数据变化
    在这里插入图片描述
    也可以点击“查看数据”,查看曲线形式
    在这里插入图片描述

3.2、解析阿里云反馈的数据

  1. 在串口调试助手查看阿里云反馈的数据信息
    在这里插入图片描述
  2. 数据格式如下,JSON格式
{"code":200,"data":{},"id":"11223357","message":"success","method":"thing.event.property.post","version":"1.0"} 
  1. 在ESP8266工程中解析阿里云反馈的数据
    暂时没有做出来……
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值