hybridgroup.gobot/platforms/mqtt
Thomas Kohler e2b710bfe7
neurosky: use serialport adaptor and move driver to drivers/serial (#1061)
2024-02-12 16:27:08 +01:00
..
LICENSE Build(v2): revert move to v2 subfolder (#932) 2023-05-29 19:23:28 +02:00
README.md examples: fix missing checks of return values (#1060) 2024-02-11 15:34:50 +01:00
doc.go build(style): switch to gofumpt and add linters (#1009) 2023-10-20 20:50:42 +02:00
mqtt_adaptor.go neurosky: use serialport adaptor and move driver to drivers/serial (#1061) 2024-02-12 16:27:08 +01:00
mqtt_adaptor_test.go lint(all): fix issues of errorlint etc (#1037) 2023-11-15 20:51:52 +01:00
mqtt_driver.go lint(all): fix issues of errorlint etc (#1037) 2023-11-15 20:51:52 +01:00
mqtt_driver_test.go lint(all): fix issues of errorlint etc (#1037) 2023-11-15 20:51:52 +01:00

README.md

MQTT

MQTT is an Internet of Things connectivity protocol featuring a lightweight publish/subscribe messaging transport. It is useful for its small code footprint and minimal network bandwidth usage.

This repository contains the Gobot adaptor/driver to connect to MQTT servers. It uses the Paho MQTT Golang client package (https://eclipse.org/paho/) created and maintained by the Eclipse Foundation (https://github.com/eclipse) thank you!

For more info about the MQTT machine to machine messaging standard, go to http://mqtt.org/.

How to Install

Please refer to the main README.md

How to Use

Before running the example, make sure you have an MQTT message broker running somewhere you can connect to

package main

import (
  "gobot.io/x/gobot/v2"
  "gobot.io/x/gobot/v2/platforms/mqtt"
  "fmt"
  "time"
)

func main() {
  mqttAdaptor := mqtt.NewAdaptor("tcp://0.0.0.0:1883", "pinger")

  work := func() {
    _ = mqttAdaptor.On("hello", func(msg mqtt.Message) {
      fmt.Println(msg)
    })
    _ = mqttAdaptor.On("hola", func(msg mqtt.Message) {
      fmt.Println(msg)
    })
    data := []byte("o")
    gobot.Every(1*time.Second, func() {
      mqttAdaptor.Publish("hello", data)
    })
    gobot.Every(5*time.Second, func() {
      mqttAdaptor.Publish("hola", data)
    })
  }

  robot := gobot.NewRobot("mqttBot",
    []gobot.Connection{mqttAdaptor},
    work,
  )

  if err := robot.Start(); err != nil {
		panic(err)
	}
}

Supported Features

  • Publish messages
  • Respond to incoming message events

License

Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.