Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/creativeprojects/go-homie
Homie for MQTT convention library
https://github.com/creativeprojects/go-homie
go golang homie iot mqtt mqtt-homie
Last synced: 27 days ago
JSON representation
Homie for MQTT convention library
- Host: GitHub
- URL: https://github.com/creativeprojects/go-homie
- Owner: creativeprojects
- License: mit
- Created: 2020-10-05T17:28:35.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-25T15:38:47.000Z (7 months ago)
- Last Synced: 2024-06-20T14:28:11.982Z (5 months ago)
- Topics: go, golang, homie, iot, mqtt, mqtt-homie
- Language: Go
- Homepage:
- Size: 45.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go Reference](https://pkg.go.dev/badge/github.com/creativeprojects/go-homie.svg)](https://pkg.go.dev/github.com/creativeprojects/go-homie)
[![Build](https://github.com/creativeprojects/go-homie/actions/workflows/build.yml/badge.svg)](https://github.com/creativeprojects/go-homie/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/creativeprojects/go-homie/graph/badge.svg?token=CLCV5OHNP5)](https://codecov.io/gh/creativeprojects/go-homie)
[![Go Report Card](https://goreportcard.com/badge/github.com/creativeprojects/go-homie)](https://goreportcard.com/report/github.com/creativeprojects/go-homie)# Homie convention for Go
The [Homie convention](https://homieiot.github.io/) 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:
``` go
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:
```go
// 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:
```go
func onSet(topic, value string, dataType homie.PropertyType) {
if value == "" {
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](https://github.com/homieiot/convention/blob/develop/extensions/documents/homie_legacy_firmware_extension.md)
```go
device := NewDevice("deviceID", "deviceName")
device.AddExtension(NewLegacyFirmware("C0:FF:EE:01", "192.168.26.5", "awesome-firmware", "version"))
```### Legacy Stats
See [Specifications](https://github.com/homieiot/convention/blob/develop/extensions/documents/homie_legacy_stats_extension.md)
```go
stats := NewLegacyStats()
NewDevice("deviceID", "deviceName").
AddExtension(stats).
OnSet(onSet)stats.
SetInterval(60).
SetUptime(0)
```
## More informationSee the [example](https://github.com/creativeprojects/go-homie/blob/main/example/main.go)
See [go doc](https://pkg.go.dev/github.com/creativeprojects/go-homie)