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.5.tar.gz (22.4 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.5-py2.py3-none-any.whl (17.5 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: spade_mqtt-0.5.tar.gz
  • Upload date:
  • Size: 22.4 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.5.tar.gz
Algorithm Hash digest
SHA256 246d5db3c40560bd4d062cb4423adadeaa45f2cf7ee75dc35fa3e12ffcf30705
MD5 891bee4e78897bb3c2c381d000888e62
BLAKE2b-256 3e4bb861152bdd2d360bb98c51dcaace945413985f057fa8846ff586b99379c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for spade_mqtt-0.5.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.5-py2.py3-none-any.whl.

File metadata

  • Download URL: spade_mqtt-0.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.5 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.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 43516c66b3cf8e45e35c515ea3c779fb7ec13cc2c5107b35a37d6282fb14412d
MD5 23265932ba513cb519c9a1d8769d6481
BLAKE2b-256 42419b8461a4319473cd3c0526e0c6652607614810a2a05c9cc6f01c76882fb8

See more details on using hashes here.

Provenance

The following attestation bundles were made for spade_mqtt-0.5-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