Back to Guides
Intermediate • ~12 min read

Using Redis with MudPi

Learn to interact with MudPi's Redis event system using CLI tools and Python.

Overview

MudPi uses Redis as its real-time data store and event bus. Understanding how to interact with Redis directly gives you powerful tools for debugging, monitoring, and extending your MudPi system.

Redis CLI

The redis-cli tool lets you interact with MudPi's data store from the command line.

Reading State

Shell
# Get the current value of a sensor
redis-cli GET "mudpi:sensor:soil_sensor_1"

# Set a value manually
redis-cli SET "mudpi:test:my_key" "hello"

Publishing Events

Shell
# Publish a message to a channel
redis-cli PUBLISH "mudpi:event:trigger" '{"event": "Triggered", "source": "manual"}'

Subscribing to Events

Shell
# Subscribe to all MudPi events
redis-cli SUBSCRIBE "mudpi:event:sensor"

Tip

Use PSUBSCRIBE "mudpi:*" with pattern matching to monitor all MudPi channels at once. This is invaluable for debugging event flow.

Python Redis Library

For more complex interactions, use the Python redis library to connect programmatically.

Publishing from Python

publish.py
import redis
import json

r = redis.Redis(host='localhost', port=6379, db=0)

data = {
    "event": "CustomEvent",
    "source": "my_script",
    "value": 42
}

r.publish('mudpi:event:custom', json.dumps(data))
print('Event published!')

Subscribing from Python

subscribe.py
import redis

r = redis.Redis(host='localhost', port=6379, db=0)
pubsub = r.pubsub()

pubsub.subscribe('mudpi:event:sensor')

print('Listening for sensor events...')
for message in pubsub.listen():
    if message['type'] == 'message':
        print(f"Received: {message['data']}")

Note

The Python redis library can be installed with pip install redis. It's already included as a MudPi dependency.