![]() Subscribe to raspberry/status in the MQTT X client. Next, testing whether the will message, is set successfully. View the messages published by the Raspberry Pi in the MQTT X client. Subscribe to the topic raspberry/topic in the MQTT X client. View the terminal information of Raspberry Pi, and you will see the messages published by MQTT X. Use MQTT X client to connect to the MQTT broker and send messages to the topic raspberry/topic. Open the terminal, run Python code, monitor messages. Run the Python code and actively send messages. We use MQTT 5.0 client tool - MQTT X to do the following tests. The parameter QoS is another MQTT feature, if you want to know more content of QoS, you can view introduction to MQTT QoS (the quality of service). In the above code, we use it to send messages to the topic raspberry/topic. # the four parameters are topic, sending content, QoS and whether retaining the message respectivelyĬlient.publish('raspberry/topic', payload=i, qos=0, retain=False)Ĭalling function publish() can send messages to a topic. # send a message to the raspberry/topic every 1 second, 5 times in a row Open any editor and input the following code, and save it as file subscriber.py. If you want to use the MQTT service completely, we still need to know how to publish and subscribe. Therefore, only accessing the MQTT broker is meaningless. For example, one device sends messages to the topic T, only the devices that subscribed to the topic T can receive the message. In the concept of the MQTT protocol, the message is delivered through the topic. If it is other issues, you can check the network situation, or check whether `paho-mqtt` has been installed. 0: connection succeededġ: connection failed - incorrect protocol versionĢ: connection failed - invalid client identifierģ: connection failed - the broker is not availableĤ: connection failed - wrong username or password If it returns other numbers, we need to judge it according to the response code below. If it is 0, then print Connected success to represent successfully connected. We judge the response code in the function on_connect. Save the above code as file test_connect.py, and run: python3 test_connect.py # Generally, we only need to pay attention to whether the response code is 0.ĭef on_connect(client, userdata, flags, rc):Ĭnnect("", 1883, 60) # flags save the dictionary of broker response flag. If it is needed, you can set it through user_data_set function. # userdata is users' information, usually empty. # client is the client instance connected this time It will be triggered when trying to connect to the MQTT broker The example code of connecting # test_connect.py If it is needed, you can use docker to quickly install the EMQX broker locally. The accessing information of the broker is as follows: This service is based on MQTT IoT cloud platform to create. This article will use the free public MQTT broker provided by EMQX. The use of MQTT Connect to the MQTT broker Use pip3 to install pip3 install paho-mqtt Use the source code to install git clone You can choose one of the following two methods to install. We need to install the library paho-mqtt for easy to connect the MQTT broker. Otherwise, please use the apt command to install (or follow the Python3 installation guideline ). If it displays Python 3.x.x (x means number), Python 3 was installed. Usually, Raspberry Pi has built-in Python3, if you not sure whether the Python3 was installed, you can use the following command to confirm. In this project, we will use Python to write a simple MQTT client on Raspberry Pi and implement connect, subscribe, unsubscribe, messaging, and other functions between this client and MQTT broker. Therefore, MQTT protocol is widely used in IoT, mobile internet, IoV, electricity power, and other industries. It is suitable for devices with limited hardware resources and the network environment with limited bandwidth. MQTT is a kind of lightweight IoT messaging protocol based on the publish/subscribe model, which can provide real-time and reliable messaging service for IoT devices, only using very little code and bandwidth. This board has the basic functions of PC and Raspberry Pi integrates Wi-Fi, Bluetooth, and a large number of GPIO, and is widely used in teaching, family entertainment, IoT, etc. This board provides USB interfaces and Ethernet interfaces can connect the keyboard, mouse, and networking cable. Raspberry Pi is a small single-board computer based on ARM and developed by the Raspberry Pi Foundation in the United Kingdom.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |