5. Cloud Calling System with @MQTT¶
Message Queuing Telemetry Transport (MQTT) is a simple messaging protocol. It is also the most common messaging protocol for the Internet of Things (IoT).
MQTT protocols define the way IoT devices transfer data. They are event-driven and interconnected using the Pub/Sub model. The sender (Publisher) and the receiver (Subscriber) communicate via Topics. A device publishes a message on a specific topic, and all devices subscribed to that topic receive the message.
In this section, a service bell system will be made using Pico W, HiveMQ (a free public MQTT broker service), and four buttons. The four buttons mean four tables in the restaurant, and you will be able to see which table’s guests need service on HiveMQ when the customer presses the button.
1. Build the Circuit
Make sure your Li-po Charger Module is connected as shown in the diagram. Otherwise, a short circuit will likely damage your battery and circuitry.
2. Visit HiveMQ
HiveMQ is an MQTT broker and client-based messaging platform that enables fast, efficient and reliable data transfer to IoT devices.。
Open HiveMQ Web Client in your browser.
Connects the client to the default public proxy.
Click on Add New Topic Subscription.
Fill in the topics you want to follow and click Subscribe. The topics set here should be more personal to avoid getting messages from other users, and pay attention to case sensitive.
3. Install the MQTT Module
Before we can start the project, we need to install the MQTT module for Pico W.
Connect to the network by running
do_connect()in the Shell, which we wrote earlier.
Type the following commands into the Shell and press
Enterto run them.
If you don’t have
secrets.pyscripts in your Pico W, please refer to 1. Access to the Network to create them.
from do_connect import * do_connect()
After a successful network connection, import the
upipmodule in the shell and use
upipto install the
umqtt.simplemodule, which is a simplified MQTT client for MicroPython.
import upip upip.install('umqtt.simple')
You will see that the
umqttmodule is installed under the
/lib/path of Pico W after completion.
4. Run the Script
5_mqtt_publish.pyfile under the path of
Click the Run current script button or press F5 to run it.
Go back to HiveMQ Web Client again and when you press one of the buttons on the breadboard, you will be able to see the Messages prompt on HiveMQ.
If you want this script to be able to boot up, you can save it to the Raspberry Pi Pico W as
How it works?
The Raspberry Pi Pico W needs to be connected to the Internet, as described in 1. Access to the Network. For this project, just use it.
from do_connect import * do_connect()
Initialize 4 button pins.
sensor1 = Pin(16, Pin.IN) sensor2 = Pin(17, Pin.IN) sensor3 = Pin(18, Pin.IN) sensor4 = Pin(19, Pin.IN)
Create two variables to store the
client ID of the MQTT broker we will use to connect to it.
Since we are using a public broker, our
client ID will not be used, even if one is required.
mqtt_server = 'broker.hivemq.com' client_id = 'Jimmy'
Connect to the MQTT agent and hold for one hour. If it fails, reset the Pico W.
try: client = MQTTClient(client_id, mqtt_server, keepalive=3600) client.connect() print('Connected to %s MQTT Broker'%(mqtt_server)) except OSError as e: print('Failed to connect to the MQTT Broker. Reconnecting...') time.sleep(5) machine.reset()
Create a variable
topic, which is the topic that the subscriber needs to follow. It should be the same as the topic filled in step 4 of 2. Visit HiveMQ above.
b here converts string to byte, because MQTT is a binary based protocol were the control elements are binary bytes and not text strings.
topic = b'SunFounder MQTT Test'
Set interrupts for each button. When a button is pressed, a message is posted under
def press1(pin): message = b'button 1 is pressed' client.publish(topic, message) print(message) sensor1.irq(trigger=machine.Pin.IRQ_RISING, handler=press1)