仍然使用,小程序控制,通过阿里云的物联网模型,选择ESP8266上外接的LED的颜色,再调节亮度
首先,根据逻辑,应该先从小程序传出一个物模型调用,选择颜色,再传出亮度的物模型调用,调节亮度。

阿里云上的物模型如下:
先是颜色,注意功能类型选择服务,标识符是代码通讯的标志,注意服务的标识符和参数的标识符可以一致也可以不一致,跟代码里的对应就可以,参数这里一个颜色也够了
esp8266外控LED灯颜色调节亮度-LMLPHP
esp8266外控LED灯颜色调节亮度-LMLPHP
然后亮度调节的物模型,一样类型是服务,注意标识符,参数可以只有一个
esp8266外控LED灯颜色调节亮度-LMLPHP
esp8266外控LED灯颜色调节亮度-LMLPHP
对应的小程序函数如下:

//调用服务使用单选框改变颜色:在阿里云的物理模型添加服务标识符colorset,参数里的标识符也colorset,类型text
radioChangeColor: function (e) {
  var DeviceName = this.options.deviceName
  var that = this
  console.log("radio"+e.detail.value)
  //防止重复点击
  that.setData({
    buttonDisabled: true
  })
  var str1 = "{\"colorset\":"
    str1 += "\""+e.detail.value+"\""
    //"\""+变量+"\""
    str1 += "}"
    //如果想传出两个变量,物模型标识符colorset,其中参数colorset字符串,light整型
    var str2 = "{\"colorset\":"
    str2 += "\""+e.detail.value+"\""
    str2 +=",\"light\":"
    str2 += ""+this.data.x.light+""
    str2 += "}"
    console.log('str2')   //str1此时为字符串{'color':hh}
    console.log(str2)
    console.log('str1')   //str1此时为字符串{'color':hh}
    console.log(str1)
    console.log("str1",typeof str1) //x为对象格式object
    //var Obj = that.data.y
    //console.log("json",that.data.y)
   // console.log(Obj)
  aliSdk.request({
      //Action: "SetDeviceProperty",
      Action: "InvokeThingService",
     //IotInstanceId:"iot-06z00j02g2qt4mg",//实例ID
      ProductKey: app.globalData.productKey,
      DeviceName: DeviceName,
      Identifier: "colorset",
     //虚拟设备收到消息received topic=/sys/a1S84XbXw8t/test02/thing/service/OpenDevice, payload={"method":"thing.service.OpenDevice","id":"207343321","params":{},"version":"1.0.0"}
      //Args: str1
      Args: str2
     // Items: '{"color":"red"}'//无参服务,所以传空
    }, {
      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
      })
    })
},

调节亮度的函数如下:

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-06z00j02g2qt4mg",//实例ID
      ProductKey: app.globalData.productKey,
      DeviceName: DeviceName,
      Identifier: "light",
      //Args: "{'light':51}" 
      //传送参数时,格式为{'Identifier':数值},传入格式必须有引号,也就是对象转换成字符串格式
      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
      })
    })
},

arduino这边,有一个报错点,就是第一次改变灯的颜色调用mqtt解析函数的时候,会获取颜色参数,放入key2中,第二次调节亮度时再次调用mqtt解析函数,key2又被冲掉了,所以key2不能在函数调用的时候才被创建初始化,应该在一开始定义,一开始定义String key2;arduino中的字符串应该是双引号定义
而后 String key2 = jsonBuffer[“params”][“colorset”]; 这里由于arduinoJson库会报错
因此转接了一次

 if (method == "thing.service.light") {
        Serial.println(key2);
      key1 = jsonBuffer["params"]["light"];
           Serial.println(key1);
      if(key2 == "r")
         {Serial.println("ok");
           analogWrite( LED_PIN1, key1);
          digitalWrite( LED_PIN2, LOW);
           digitalWrite( LED_PIN3, LOW);}
          // AliyunIoTSDK::begin(espClient,PRODUCT_KEY,DEVICE_NAME,DEVICE_SECRET,REGION_ID);
         //  AliyunIoTSDK::send("colour", "red");}
            //如果想实现流水灯的效果,在某个信号开启后,用delay即可,3000=3s
    
}

    if (method == "thing.service.colorset") {
        String  key3 = jsonBuffer["params"]["colorset"];
        key2=key3;
        Serial.println(key2);
            //digitalWrite(LED_BUILTIN, HIGH);
            digitalWrite( LED_PIN1, HIGH);
          digitalWrite( LED_PIN2, LOW);
           digitalWrite( LED_PIN3, LOW);
          // AliyunIoTSDK::begin(espClient,PRODUCT_KEY,DEVICE_NAME,DEVICE_SECRET,REGION_ID);
         //  AliyunIoTSDK::send("colour", "red");}
            //如果想实现流水灯的效果,在某个信号开启后,用delay即可,3000=3s
    
}
}
11-13 23:23