homieiot / convention

🏡 The Homie Convention: a lightweight MQTT convention for the IoT
https://homieiot.github.io/
Other
710 stars 59 forks source link

Provide basic implementation examples #186

Open bkanuka opened 4 years ago

bkanuka commented 4 years ago

I would love to see some small sample programs that implement the Homie spec in different languages. For example a sample project that shows a Homie device implemented in Python + Paho MQTT. The same small program written in Node, etc.

I would imagine this as a totally seperate "examples" repository under the homieiot group on github with a directory for each language. I don't mean just a list of libraries (this exists already), rather a "from scratch" implementation of the spec. This would help a lot of people (like myself...) who learn better from examples, and could also serve as a prototype for developing Homie-compliant programs.

davidgraeff commented 4 years ago

True. Would be nice having some simple examples.

mjcumming commented 4 years ago

Here is a simple python example using the homie4 library


import time

from homie.device_dimmer import Device_Dimmer

import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

mqtt_settings = {
    "MQTT_BROKER": "OpenHAB",
    "MQTT_PORT": 1883,
}

class My_Dimmer(Device_Dimmer):
    def set_dimmer(self, percent):
        print(
            "Received MQTT message to set the dimmer to {}. Must replace this method".format(
                percent
            )
        )
        super().set_dimmer(percent)

try:

    dimmer = My_Dimmer(
        name="Test Dimmer", device_id="testdimmer", mqtt_settings=mqtt_settings
    )

    while True:
        dimmer.update_dimmer(0)
        time.sleep(15)
        dimmer.update_dimmer(50)
        time.sleep(15)
        dimmer.update_dimmer(100)
        time.sleep(15)

except (KeyboardInterrupt, SystemExit):
    print("Quitting.")
    dimmer.close()
rroemhild commented 4 years ago

Here a simple MicroPython LED example with Microhomie: https://github.com/microhomie/microhomie/blob/master/examples/led/main.py

We have more examples in the example directory.

ThomDietrich commented 4 years ago

Might also be related: #51

2bndy5 commented 4 years ago

+1 for an example. Though I'm hoping for something more like a library-agnostic flowchart. The list of libraries are helpful, but their APIs tend to differ which makes interpreting the specs harder.

bggardner commented 7 months ago

Since the OP requested an example with Python + Paho MQTT, I just rewrote my Python module and the README has an example: https://github.com/bggardner/pyhomie

2bndy5 commented 7 months ago

I have since wrote/published the Circuitpython_Home library. In its docs, I detail the Homie topology in comparison to equivalent representations of API and MQTT topics.

FYI it also works with paho-mqtt as adafruit_minimqtt was designed to be a minimal drop-in replacement.