0 Preface
August 2014 Yeelink launched the MQTT protocol-based switch type device Control API, compared to the HTTP restful rotation approach, by subscribing to related topic messages, you can remotely control the real-time application of class applications better. This article uses two ways to realize the remote control of the switch type device, one is based on the Nodejs mqtt.js Extension Library, the other is the Python-based PAHO-MQTT extension library. "Related blog--mqtt" "Mqtt Learning note--MQTT Protocol experience Mosquitto installation and use" MQTT Learning notes-Raspberry Pi client uses Mosquitto and Paho-python "" Related posts--http restf UL "Yeelink HTTP request Format Analysis" "Yeelink Platform query switch volume-socket programming Windows platform" "Yeelink Platform push sensor results-socket programming Windows platform" "Yeelink Platform use-- Remote control RT Thread + lwip+ STM32 "
1 Installing and using Mqtt.jsSince no Nodejs is installed on the Raspberry Pi and no gpio extension library is installed, this example does not show how to control the Gpio with Yeelink push messages. So this example can only be run on the PC temporarily, but this example is familiar with the basic use of Yeelink mqtt.
1.1 Installing Mqtt.jsnpm Install Mqtt or cnpm install Mqtt
1.2 Implementation Code--yeelink_mqtt.js
var mqtt = require (' Mqtt '); var username = ' U-apikey '; var password = ' ffa3826972d6cc7ba5b17e104ec5xxxx '; var hostname = ' mqtt.yeelink.net '; var topic = '/v1.1/device/1949/sensor/2511/datapoints '; var url = ' mqtt://' + username + ': ' + password + ' @ ' + hostname; Console.log (URL); var client = Mqtt.connect (URL); Subscribe to the topic client.subscribe (topic); Client.on (' message ', function (topic, message) { console.log (topic); Console.log (message); });
1.3 Run"Execute Code" node Yeelink_mqtt.js "Modify status" login Yeelink platform modifies the state of the device via a Web page, or it can modify the device status by other means. Once the device status is changed, the console outputs the message being pushed, and the process is almost free of latency. "Console Output"mqtt://u-apikey : [email protected]
/V1.1/ device/1949/sensor/2511/datapoints
< Span style= "Font-family:tahoma; font-size:18px; Line-height:27px ">{" sensor_id ": 2511," timestamp ": 1410835745," value ": 1}
/v1.1/device/1949/sensor/ 2511/datapoints
{" sensor_id ": 2511," timestamp ": 1410835754," value ": 0}
1.4 Brief DescriptionThe "1" MQTT protocol can specify a user name and password, in the Yeelink protocol, the user name becomes U-apikey, the password is the specific value of U-apikey. The "2" subscription topic is the device URI, and the API version number needs to be modified to v1.1 "3" host name Mqtt.yeelink.net instead of api.yeelink.net in the Mqtt.js sample code: Client = Mqtt.connect (' Mqtt://user:[email protected] '); In the sample code for Yeelink: client = Mqtt.connect ("mqtt://u-apikey:<your_key> @mqtt. yeelink.net"); The above two example code can explain the corresponding relationship of each parameter.
2 Installing and using PAHO-MQTTThis example demonstrates installing PAHO-MQTT on a Raspberry Pi, controlling the Gpio by pushing the content of the message
2.1 Installing PAHO-MQTTpip Install Paho-mqtt
2.2 Implementation Code
#-*-Coding:utf-8-*-import paho.mqtt.client as Mqttimport Rpi.gpio as gpioimport jsonhostname = ' mqtt.yeelink.net ' use Rname = ' u-apikey ' password = ' ffa3826972d6cc7ba5b17e104ec5xxxx ' topic = '/v1.1/device/1949/sensor/2511/datapoints ' # BCM Gpio Number pins = [17,18,27,22,23,24,25,4]pin = 18def Gpio_setup (): # uses BCM number Gpio.setmode (GPIO). BCM) # Sets all GPIO to the output state, and outputs the low-level for-pin in Pins:GPIO.setup (PIN, GPIO. Out) Gpio.output (PIN, GPIO. Low) def Gpio_destroy (): For pin in Pins:GPIO.output (PIN, Gpio. Low) Gpio.setup (PIN, GPIO. In) # Connection succeeded callback function def on_connect (client, UserData, flags, RC): Print ("Connected with result code" + str (RC)) # even Subscribe to Topic Client.subscribe (topic) # Message Push callback function def on_message (client, UserData, msg): Print (msg.topic+ "" +str (Msg.payloa d) # Get the pin in the payload and value Gpio = Json.loads (str (msg.payload)) if gpio[' value '] = = 0:gpio.output (PIN, GPIO.L OW) Else:GPIO.output (PIN, GPIO. High) if __name__ = = ' __Main__ ': client = Mqtt. Client () Client.on_connect = On_connect Client.on_message = on_message gpio_setup () Try: # Configure user name and password Client.username_pw_set (username, password) client.connect (hostname, 1883,) Client.loop_forever () Except KeyboardInterrupt:client.disconnect () Gpio_destroy ()
2.3 Run"Execute code" python yeelink_mqtt.js "Modify status" login Yeelink platform modifies the device status via the Web page, or other methods to modify the device status. Once the device status is changed, the console outputs the message being pushed, and the process is almost free of latency. "Console Output"Connected with result code 0/v1.1/device/1949/sensor/2511/datapoints {"sensor_id": 2511, "timestamp": 141083559 8, "value": 1}/v1.1/device/1949/sensor/2511/datapoints {"sensor_id": 2511, "timestamp": 141083560 2, "value": 0}
2.4 Brief description"1" note the user name and password settings in the code, set the user name and password through Client.username_pw_set (username, password), but the username and password are now changed toU-apikey andthe specific value of the U-apikey. the "2" On_connect is the connection success callback function, On_message is the callback function that receives the push message. "3" After the connection is successful, subscribe to the topic, if you receive the server push message, through the message value control Gpio.
3 Summary"1" in Yeelink applicationsU-apikey changed to user name and passwordthe "2" device URI becomes a theme"3" After subscribing to the topic, the device will receive a push message from the Yeelink platform if the switch device status is changed by other means (HTTP RESTful), we can assume that the message push is real-time. "4" compared to the device control, the MQTT protocol is more concise than the HTTP protocol, real-time better.
4 References"1" Yeelink MQTT API
Mqtt Learning Note--yeelink MQTT service uses mqtt.js and PAHO-MQTT