MQTT Learning notes-The Raspberry Pi Mqtt client uses Mosquitto and Paho-python

Source: Internet
Author: User
Tags install openssl

0 PrefaceThis article explains how to install Mosquitto on a Raspberry Pi. In this article, two simple examples of how to use the MQTT protocol for message subscriptions in the Raspberry Pi include the mosquitto_sub instruction to implement message subscriptions and Paho-python extension libraries to enable remote control of GPIO ports. This article uses two tools--mosquitto Paho-python, where Mosquitto is an open source message broker software that implements the MQTT v3.1 protocol, providing lightweight, publish/subscribe messaging push patterns that make device-to-device message communication easy to use In addition, Paho-python is a client that complies with the Mqtt v3.1 protocol, Paho-python can connect to an MQTT proxy server, publish messages, subscribe to messages, and get push messages. "Related blog posts""Mqtt Learning Note--MQTT Protocol experience Mosquitto installation and use""Raspberry Pi Study notes-index blog post""Raspberry Pi Learning note--gpio functional learning""Raspberry Pi study notes--rpi.gpio running Lights"
1 Installing the Mosquitto on the Raspberry Piinstalling Mosquitto on a Raspberry Pi is similar to other platforms, and may be slightly longer if the Raspberry Pi platform is compiled directly. Mosquitto Source Generation package is not big, so compile time tolerable.
1.1 Installation as of September 2014, the latest version is mosquitto-1.3.4. Create a new directory in the Raspberry Pi, such as software.
# Download Source code packagewget http://mosquitto.org/files/source/mosquitto-1.3.4.tar.gz# Unziptar zxfv mosquitto-1.3.4.tar.gz # Enter Directory
CD mosquitto-1.3.4
# compilation Make# installation
sudo make install

1.2 installation attention points
"1" compilation Not found Openssl/ssl.h
"Workaround"--Install OpenSSL sudo apt-get install Libssl-dev
"2" compilation process could not find ares.h"Workaround"--Modify With_srv:=yes in config.mk and change to With_srv:=yes"3" libmosquitto.so.1 not found during useerror while loading shared Libraries:libmosquitto.so.1:cannot open Shared object file:no such file or directory
"Workaround"--Modify the libmosquitto.so location
# Create a linksudo ln-s/usr/local/lib/libmosquitto.so.1/usr/lib/libmosquitto.so.1# Update the dynamic link librarysudo ldconfig"4" make:g++: Command not found"Workaround"Installing the g++ compilersudo apt-get install g++
2 Simple Examplesdesign a simple test case, run the MQTT proxy server on the PC, and the Raspberry Pi subscription subject is GPIO message, the PC publishes the same subject message, the content of the message is the JSON packet, the packet format is {"index": +, "value": 0}, Index represents the number of the Raspberry Pi Gpio, and value represents the open or closed state.
Figure 1 Examplein this case, the PC IP address is 192.168.1.110and the Raspberry Pi IP address is 192.168.1.106 2.1 Opening the MQTT service in the PCmosquitto-v
2.2 Subscribing to a message in the Raspberry Pimosquitto_sub-v-T gpio-h 192.168.1.110"-H" specifies the MQTT proxy server host, pointing to the PC IP address 192.168.1.110
2.3 Publishing messages in PCmosquitto_pub-t gpio-h 192.168.1.110-m "{\" pin\ ": 17,\" value\ ": 0}""-H" specifies the MQTT proxy server host, pointing to the PC IP address 192.168.1.110
2.4 Messages are pushed to the Raspberry PiFinally, output the following in the Raspberry Pi:Gpio {"index": +, "value": 0}
Output in the PC MQTT Server Console1410600001:mosquitto version 1.3.4 (build date 2014-09-13 15:55:06+0800) starting1410600001:using Default CONFIG.1410600001:opening IPv4 Listen socket on port 1883.1410600001:opening IPv6 Listen socket on port 1883.1410600062:new connection from 192.168.1.106 on port 1883.1410600062:new client connected from 192.168.1.106 as MOSQSUB/3063-RASPBERRYP (c1, k60).1410600062:sending CONNACK to Mosqsub/3063-raspberryp (0)1410600062:received SUBSCRIBE from Mosqsub/3063-raspberryp1410600062:gpio (QoS 0)1410600062:MOSQSUB/3063-RASPBERRYP 0 Gpio1410600062:sending SUBACK to Mosqsub/3063-raspberryp1410600122:received PINGREQ from Mosqsub/3063-raspberryp1410600122:sending Pingresp to Mosqsub/3063-raspberryp1410600152:new connection from 192.168.1.110 on port 1883.1410600152:new client connected from 192.168.1.110 as Mosqpub/9793-easyarm (c1, k60).1410600152:sending CONNACK to Mosqpub/9793-easyarm (0)1410600152:received PUBLISH from Mosqpub/9793-easyarm (D0, q0, R0, M0, ' Gpio ', ... (bytes))1410600152:sending PUBLISH to Mosqsub/3063-raspberryp (D0, q0, R0, M0, ' Gpio ', ... (bytes))1410600152:received DISCONNECT from Mosqpub/9793-easyarm1410600182:received PINGREQ from Mosqsub/3063-raspberryp1410600182:sending Pingresp to Mosqsub/3063-raspberryp
3 Remote control of GPIO using Mqttby using the Python-gpio extension library, the remote control of the GPIO port is implemented by means of message push. 3.1 Installing Paho-mqttinstall PAHO-MQTT using the PIP tool and enter the following instructions:sudo pip install paho-mqtt
3.2 Raspberry Pi subscription code --simple.py
#-*-Coding:utf-8-*-import paho.mqtt.client as Mqttimport Rpi.gpio as Gpioimport json# BCM GPIO number pins = [17,18,27,22, 23,24,25,4]def 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 After completion, subscribe to GPIO topic client.subscribe ("Gpio") # Message Push callback function def on_message (client, UserData, msg): Print (msg.topic+ "" +str (MSG.P ayload) # Get PIN in payload and value Gpio = Json.loads (str (msg.payload)) if gpio[' pin '] in pins:if gpio[' value '] = = 0:gpio.output (gpio[' pin '), GPIO. Low) Else:GPIO.output (gpio[' pin '), GPIO. High) if __name__ = = ' __main__ ': client = Mqtt. Client () Client.on_connect = On_connect Client.on_message = ON_message Gpio_setup () Try: # Please change the IP address of the MQTT Proxy server client.connect ("192.168.1.110", 1883, 60) according to the actual situation Client.loop_forever () except KeyboardInterrupt:client.disconnect () Gpio_destroy ()

3.3 Starting a service and publishing a message"Start the MQTT Proxy service in PC"mosquitto-v"Run scripts in the Raspberry Pi"sudo python simple.py"Publish messages in PC"# Open GPIO17
Mosquitto_pub-h 192.168.1.110-t gpio-m "{\" pin\ ": 17,\" value\ ": 1}"
# Close GPIO17
Mosquitto_pub-h 192.168.1.110-t gpio-m "{\" pin\ ": 17,\" value\ ": 0}""Run Results"Raspberry Pi GPIO17 is lit or extinguished according to the messages pushed by the PC.
4 SummaryThis article explains how to use the MQTT client in a Raspberry Pi to achieve remote control of Gpio via PAHO-MQTT. This example runs in the LAN and can not reflect the advantages of the MQTT protocol in the remote control. The latter will also spend more time practicing and analyzing the MQTT protocol.

MQTT Learning notes-The Raspberry Pi Mqtt client uses Mosquitto and Paho-python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.