阿里云物联网的通信方式

阿里云物联网通信的两种方式,一个是物模型(分为服务,事件,属性),一个是自定义topic(要另外设置数据流转)

1.使用产品内的功能定义,(其实也就是Topic中定义好的物模型主题),功能定义中有事件,属性,服务三种类型

进入产品的功能定义中,选择编辑草稿

添加自定义功能,

功能类型可以按照需求选择服务(调用比如开关灯,亮度调节等),属性(显示数据等),事件,其中标识符是代码识别此功能的重要标识,另外参数一定要与代码中调用的参数数据类型一致

一定要发布上线才有效

服务colorset和属性color调用的代码不同

首先小程序控制端,如果是通过按钮调用服务,使用aliSDK中的Action: "InvokeThingService",完整函数代码如下

//发送信息给阿里云,页面设置了一个亮度调节器(进度条),绑定以下函数,data里面x初始值x:{'light':12},

c 复制代码
changeDeviceLight: function (e) {
  var DeviceName = this.options.deviceName
  var that = this
  console.log(`携带值为`, e.detail.value)
  console.log("x",typeof that.data.x) //x为对象格式object
  that.data.x.light=e.detail.value    //将x里的数值替换成当前调节器的值
  var str = JSON.stringify(that.data.x);    //将对象转换成字符串,
 //var Obj = JSON.parse(str);    如果需要字符串转换成对象
  console.log("str",typeof str)        //此处可以校验是否已是字符串
  console.log("str",str)
  console.log("lightvalue",that.data.x)
  //防止重复点击
  that.setData({
    buttonDisabled: true
  })
 
  aliSdk.request({
      Action: "InvokeThingService",
     //IotInstanceId:"iot-",//实例ID
      ProductKey: app.globalData.productKey,
      DeviceName: DeviceName,
      Identifier: "light",
      //Args: "{'light':51}" 
      //传送参数时,格式为{'Identifier':数值},传入格式必须有引号,也就是对象转换成字符串格式,数值也必须符合当时阿里云内设置的数据类型,无参数的时候传空 Args:"{}"
      Args: str   //此处获取的亮度值会被传给阿里云
    }, {
      method: "POST"
    },
    (res) => {
      console.log("success")
      console.log(res) //查看返回response数据
      that.setData({
        openedDevice: !that.data.openedDevice
      })
    },
    (res) => {
      console.log("fail")
      wx.showToast({
        title: '网络连接失败',
        icon: 'none',
        duration: 1000,
        complete: () => {}
      })
    },
    (res) => {
      console.log("complete")
      that.setData({
        buttonDisabled: false
      })
    })
},

阿里云收到消息转给esp8266,arduino串口监视器显示{"method":"thing.service.light","id":"173255894","params":{"light":114},"version":"1.0.0"}

arduino端的代码将params里的light取出来

c 复制代码
String method =jsonBuffer["method"];
   int key1 = jsonBuffer["params"]["light"];
    Serial.println("method");
   Serial.println(method);
   Serial.println(key1);

如果是调用属性,则使用Action: "SetDeviceProperty"

c 复制代码
radioChangeColor: function (e) {
  var DeviceName = this.options.deviceName
  var that = this
  console.log(e.detail.value)
  //防止重复点击
  that.setData({
    buttonDisabled: true
  })
  var str1 = "{\"color\":"
    str1 += "\""+e.detail.value+"\""
    //"\""+变量+"\""可以在双引号里调用变量,而不被识别成字符串
    str1 += "}"
    console.log('str1')  
    console.log(str1)  //str1此时为字符串{"color":"yellow"}
    console.log("str1",typeof str1) 
   
  aliSdk.request({
      Action: "SetDeviceProperty",
     //IotInstanceId:"iot-",//实例ID
      ProductKey: app.globalData.productKey,
      DeviceName: DeviceName,
      Identifier: "color",
     Items: '{"color":"red"}'//属性调用也可使用Items:str1,将str1传给阿里云
    }, {
      method: "POST"
    },
    (res) => {
      console.log("success")
      console.log(res) //查看返回response数据
      that.setData({
        openedDevice: !that.data.openedDevice
      })
    },
    (res) => {
      console.log("fail")
      wx.showToast({
        title: '网络连接失败',
        icon: 'none',
        duration: 1000,
        complete: () => {}
      })
    },
    (res) => {
      console.log("complete")
      that.setData({
        buttonDisabled: false
      })
    })
},

阿里云收到消息后转给esp8266,串口监视器显示{"method":"thing.service.colorset","id":"921010594","params":{"colorset":"green"},"version":"1.0.0"}

arduino端的代码将params里的colorset取出来

c 复制代码
String method =jsonBuffer["method"];
   String key1 = jsonBuffer["params"]["colorset"];
    Serial.println("method");
   Serial.println(method);
   Serial.println(key1);
相关推荐
Clownseven2 小时前
[服务器备份教程] Rclone实战:自动备份数据到阿里云OSS/腾讯云COS等对象存储
服务器·阿里云·腾讯云
群联云防护小杜2 小时前
物联网僵尸网络防御:从设备认证到流量染色
运维·服务器·前端·网络·物联网·安全·ddos
YD12182 小时前
如何在 AWS 上构建支持 AVIF 的前端图片优化方案
前端·云计算·aws
Jackson@ML2 小时前
【AWS入门】Amazon SageMaker简介
云计算·aws·sagemaker
coding随想3 小时前
云计算简介:从“水电”到“数字引擎”的技术革命
云计算
Ultipa3 小时前
云计算与大数据进阶 | 26、解锁云架构核心:深度解析可扩展数据库的5大策略与挑战(上)
大数据·数据库·云计算
青牛科技-Allen3 小时前
TS01S:单通道差分灵敏度校准电容触摸传感器芯片
单片机·嵌入式硬件·物联网·pd快充·家电变频控制·工业电机驱动系统
通义灵码3 小时前
MySQL 开发的智能助手:通义灵码在 IntelliJ IDEA 中的应用
数据库·mysql·阿里云·intellij-idea·通义灵码
Johny_Zhao4 小时前
HSRP、GLBP、VRRP、NSRP 协议对比与配置指南
网络·人工智能·网络安全·信息安全·云计算·cisco·huawei·系统运维·华三
Linging_246 小时前
Spring AI Alibaba集成阿里云百炼大模型应用
人工智能·spring·阿里云