Git Product home page Git Product logo

go-homie's Introduction

Go Reference Build codecov Go Report Card

Homie convention for Go

The Homie convention defines a standardized way of how IoT devices and services announce themselves and their data on the MQTT broker.

This library is MQTT implementation agnostic: it only generates and manages topics and values, not actually sending them through the wire.

See an example on how to define a Homie device:

device := homie.NewDevice("my-sensor", "MQTT ESP8266 agent")
device.
    AddNode("bme280", "BME280 via ESP8266EX", "bme280").
    AddProperty("temperature", "Temperature", homie.TypeFloat).SetUnit("°C").Node().
    AddProperty("pressure", "Pressure", homie.TypeFloat).SetUnit("hPa").Node().
    AddProperty("humidity", "Humidity", homie.TypeFloat).SetUnit("%")

Send the Homie attributes and or values to the MQTT client:

// all homie attributes
for _, attribute := range device.GetHomieAttributes() {
    publish(attribute.Topic, attribute.Value)
}

// all property values
for _, attribute := range device.GetValues() {
    if attribute.Value != "" {
        publish(attribute.Topic, attribute.Value)
    }
}

Your publish function can use the MQTT client of your choice.

You can set a callback for sending property values for you when you set them:

func onSet(topic, value string, dataType homie.PropertyType) {
    if value == "<nil>" {
        value = ""
    }
    if value == "" && dataType != homie.TypeString {
        // don't send a blank string on anything else than a string data type
        return
    }
    publish(topic, value)
}

// either install a global callback on the device...
device.OnSet(onSet)

// ...or install the callback on this property only
device.Node("bmp280").Property("temperature").OnSet(onSet)

// new values will be published for you
device.Node("bme280").Property("temperature").Set(20.0)

Supported extensions

Legacy Firmware

See Specifications

device := NewDevice("deviceID", "deviceName")
device.AddExtension(NewLegacyFirmware("C0:FF:EE:01", "192.168.26.5", "awesome-firmware", "version"))

Legacy Stats

See Specifications

stats := NewLegacyStats()
NewDevice("deviceID", "deviceName").
    AddExtension(stats).
    OnSet(onSet)

stats.
    SetInterval(60).
    SetUptime(0)

More information

See the example

See go doc

go-homie's People

Contributors

creativeprojects avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

tieske

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.