(四)—— 远程点亮led灯

前言

  • 使用mqtt通讯,远程控制RPC
  • round switch和Led indicator部件,外观能设置的东西很少,甚至开关的title文字颜色都不能改

MQTTFX模拟

服务器发RPC报文给客户端

  1. 默认设置新建一个round switch部件,注意:高级中Retrieve led status value using method(检查设备状态的方法)是Subscribe for attribute
  2. 默认设置新建一个Led indicator部件用于显示当前led状态
  3. 仪表板点击round switch会向v1/devices/me/rpc/request/$id主题发送报文{“method”:”setValue”,”params”:$true(false)}

客户端收到RPC报文后执行

  1. 解析报文id,向v1/devices/me/rpc/response/ParseError: KaTeX parse error: Expected ‘}’, got ‘EOF’ at end of input: …red”>{“value”: 0 or 1}确认连接
  2. 客户端记录led的状态,暂时只考虑开关两种状态
  3. 客户端向v1/devices/me/attributes主题发送属性报文
  4. 别忘记执行led开关操作,此处只需要digitalWrite(LED_BUILTIN, !ledStatus)(板载灯是反逻辑的)就可以了,也可以理解成更新led灯状态

服务器收到应答报文后执行

  1. 服务器根据v1/devices/me/rpc/response/$request_id主题响应报文更新round switch的状态
  2. 服务器根据v1/devices/me/attributes主题属性报文更新Led indicator的状态

至此远程点亮led就完成了,单片机上只要点灯成功,执行其他操作也就同理了

完整代码