Skip to main content

MQTT mixin for spade, enabling agents to pub/sub to MQTT topics.

Project description

spade_mqtt

The goal of this project is to implement a way to enable spade agents to interact (connect, subscribe, publish, ...) on MQTT.

Installation

pip install spade_mqtt

Getting started

Agent should use it by adding the MqttMixin in the class declaration. This mixin add the mqtt attribute in the agent class. This object wraps mqtt calls and manage asyncio.

class MyAgent(MqttMixin, spade.agent.Agent):

    async def setup(self):
        try:
            await self.mqtt.connect("localhost", 30)
        except TimeoutError:
            print("Warning: could not connect to MQTT brocker localhost")
        #...

Such agent can then have behaviour that can subscribe on topics, and await messages on them.

class MQTTSubBehaviour(spade.behaviour.CyclicBehaviour):
    """Behaviour that subscribes to a topic and store the messages received."""

    def __init__(self, topic):
        spade.behaviour.CyclicBehaviour.__init__(self)
        self.topic = topic

    async def run(self):
        print(f"awaiting message on topic {self.topic}")
        msg = await self.agent.mqtt.receive(self.topic)
        if msg:
            print(f"got message with payload {msg.payload}")
            # process the message...

    async def on_start(self):
        await self.agent.mqtt.subscribe(self.topic)

    async def on_end(self):
        await self.agent.mqtt.unsubscribe(self.topic)

MQTT publish is done likewise.

class MQTTPubBehaviour(spade.behaviour.OneShotBehaviour):
    """Publish hello world to example/topic"""

    async def run(self):
        await self.agent.mqtt.publish(
            "example/topic", "hello world", retain=True
        )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spade_mqtt-0.6.0.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spade_mqtt-0.6.0-py2.py3-none-any.whl (18.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file spade_mqtt-0.6.0.tar.gz.

File metadata

  • Download URL: spade_mqtt-0.6.0.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spade_mqtt-0.6.0.tar.gz
Algorithm Hash digest
SHA256 e40e1bed4a8f9dc03e7cdf861b51bb01106badce5281fc71699860f9eb4b7c09
MD5 0042287be4a9cb8ef808b275be6f4b3e
BLAKE2b-256 86a25ea83d12e87a9edbf4d9adbd16365cc8c0b0d7eb67864e8e83fa2e988c2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for spade_mqtt-0.6.0.tar.gz:

Publisher: python-publish.yml on remipannequin/spade_mqtt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spade_mqtt-0.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: spade_mqtt-0.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spade_mqtt-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f9de3e3de9b0880026fcfe3315ee04206a56182f980cb10a9c4d04d8b28f0391
MD5 3afcf7ccd6ae6e12f901625ff3b78bf3
BLAKE2b-256 0da19b028b0f5c1397070cebe10d49b97f5f94d07eb229bd3e4fd7e034856495

See more details on using hashes here.

Provenance

The following attestation bundles were made for spade_mqtt-0.6.0-py2.py3-none-any.whl:

Publisher: python-publish.yml on remipannequin/spade_mqtt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page