Back to Guides
Legacy • ~18 min read

Sending Data to MudPi (Legacy)

Legacy guide using paho-mqtt 1.x API. For paho-mqtt 2.x (MudPi v0.11+), see the updated guide.

Outdated Guide

This guide uses the paho-mqtt 1.x API (mqtt.Client() without CallbackAPIVersion). MudPi v0.11+ requires paho-mqtt 2.x, which has a different client constructor. Please see the updated Sending Data guide.

Overview

MQTT is a lightweight messaging protocol ideal for IoT devices. MudPi can receive sensor data and commands via MQTT, allowing you to integrate remote sensors, ESP32 boards, and other devices into your garden system.

Installing the MQTT Broker

Install Mosquitto, a popular MQTT broker, on your Raspberry Pi:

Shell
sudo apt update
sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto
sudo systemctl start mosquitto

Note

Mosquitto runs on port 1883 by default. For production use, configure authentication and TLS encryption.

Command Line Tools

Mosquitto includes CLI tools for publishing and subscribing to MQTT topics.

Publishing Messages

Shell
# Publish a sensor reading
mosquitto_pub -h localhost -t "mudpi/sensor/outdoor_temp" -m '{"value": 72.5}'

# Publish with a specific QoS level
mosquitto_pub -h localhost -t "mudpi/sensor/moisture" -m '{"value": 45}' -q 1

Subscribing to Topics

Shell
# Subscribe to all MudPi sensor topics
mosquitto_sub -h localhost -t "mudpi/sensor/#"

# Subscribe to a specific topic
mosquitto_sub -h localhost -t "mudpi/sensor/outdoor_temp"

Python with paho-mqtt (1.x)

Use the paho-mqtt library to publish sensor data from Python scripts:

mqtt_publish.py
import paho.mqtt.client as mqtt
import json
import time

client = mqtt.Client()
client.connect('localhost', 1883, 60)

while True:
    data = {
        'value': 23.5,
        'unit': 'celsius',
        'timestamp': time.time()
    }
    client.publish('mudpi/sensor/remote_temp', json.dumps(data))
    print(f'Published: {data}')
    time.sleep(30)

Tip

Install paho-mqtt with pip install paho-mqtt. You can also use it to subscribe to MudPi events and build custom dashboards or alert systems.

ESP32 / Arduino

Use the PubSubClient library to send data from an ESP32 or Arduino with WiFi to your MudPi MQTT broker:

Arduino (C++)
Click to expand code…

WiFi Credentials

Never commit WiFi credentials or MQTT passwords to version control. Use environment variables or a separate configuration file that's excluded from your repository.