MQTT
🌟 Introduction
In this section, we'll explore the MQTT Blocks, which allow seamless integration with MQTT protocols for IoT (Internet of Things) applications.
🧠 What Is MQTT?
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for resource-constrained devices. It allows devices to publish, subscribe, and exchange data efficiently over a network.
- BuddyOS Stem system uses MQTT to communicate with ESP device. So you can show sensor data of your device to mobile app or control device remotely using MQTT.
- Go to home page of BYA website, check your device detail information to get
MQTT username
andMQTT password
. - Broker:
stem.mqtt.buddyos.ai
- Port:
1883
- Topic to listen data: stem/device/{MQTT username}/event, example:
stem/device/ESP32-cc8da2ecd26c/event
. You can only subscribe to the topic that include your MQTT username. - Topic to send data to device: stem/device/{MQTT username}/directive, example:
stem/device/ESP32-cc8da2ecd26c/directive
. You should send data as a JSON String with format:
{
"action": "your action",
"data": {}
}
🔗 Key MQTT Blocks
1. function handle_mqtt_message
- Purpose: Define actions to take when a message is received on a subscribed topic.
🧭 Tip
This block lets you define how your program reacts to received messages, enabling event-driven workflows.
2. MQTT message action
- Purpose: Get the message action inside the handler function.
📝 Note
Use this block to let your program read the message action received
3. send MQTT event
- Purpose: send data to MQTT topic
stem/device/{serial_number}/event
.
📝 Note
Use this block to send data from device to MQTT event topic. Then use another device or platform to listen to event topic. You can create a dashboard for monitor device data realtime.
🛠️ Example Workflow
Scenario: Sending analog data to MQTT
- Start getting data from sensor when receiving message action
start
. Then sending sensor data to MQTT event topic.
🌍 Applications
🏠 Smart Home
- Publish temperature, humidity, and light sensor data.
- Subscribe to control topics for automation.
🚗 Vehicle Monitoring
- Track real-time sensor data like speed and fuel levels.
🌐 IoT Projects
- Create a network of devices that exchange data efficiently.
🎉 Conclusion
The MQTT Blocks in Blockly empower users to build robust IoT systems with ease. By combining blocks for publishing, subscribing, and handling messages, you can create intelligent, event-driven applications.
🚀 Happy IoT Programming!