提交数据到月球猫控制器 - 炫Q香蕉派

提交数据到月球猫控制器

球猫建立了一个“月球猫控制器”,地址是https://yqmiot.com/,mqtt端口是11883,wss地址是wss://yqmiot.com/_wss
python的订阅代码是

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
  client.subscribe("#")

def on_message(client, userdata, msg):
  print msg.topic.decode("utf-8")
  print msg.payload.decode("utf-8")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("yqmiot.com", 11883, 50)
client.loop_forever()  

球猫给的html的测试代码

<html>
<head>
  <title>test Ws mqtt.js</title>
</head>
<body>
  <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
  <script>
    var client = mqtt.connect("wss://yqmiot.com/_wss"); // you add a ws:// url here
    client.subscribe("#");
    client.on("message", function(topic, payload) {
      alert([topic, payload].join(": "));
    });
    client.publish("mqtt/demo", "hello world!");
  </script>
</body>
</html>

如何添加自己的设备呢,球猫又写了文档

## 连接MQTT服务器
注册 yqmiot/1/0/# 主题
注册 yqmiot/1/<设备ID>/# 主题

## 设备上线
(刷新网页)收到 yqmiot/1/0/<控制器ID>/online 消息,发送猪蹄:
yqmiot/1/<控制器ID>/<设备ID> {"model": "light", "val": 0} 
就可以上线了。

## 控制设备
收到 
yqmiot/1/<设备ID>/<控制器ID>/switch 
{"model":"light","val":true}
猪蹄根据val更新设备开关状态

设备上线代码

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
  client.subscribe("yqmiot/1/0/#")
  client.subscribe("yqmiot/1/miku/#")

def on_message(client, userdata, msg):
  print msg.topic.decode("utf-8")
  print msg.payload.decode("utf-8")
  client.publish("yqmiot/1/229a8baa4b1fbc94/miku", "{\"model\": \"light\", \"val\": 1}")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("yqmiot.com", 11883, 50)
client.loop_forever() 

设备状态提交代码,打开

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("yqmiot.com", 11883, 50)
client.publish("yqmiot/1/229a8baa4b1fbc94/miku/switch", "{\"model\":\"light\",\"val\":true}")

关闭

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("yqmiot.com", 11883, 50)
client.publish("yqmiot/1/229a8baa4b1fbc94/miku/switch", "{\"model\":\"light\",\"val\":false}")

基本可以玩转了,球猫厉害!

标签: none

添加新评论