2014-11-29 07:34:42 +08:00
|
|
|
# MQTT
|
2014-11-05 00:41:56 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
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.
|
2014-11-05 00:41:56 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
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!
|
2016-02-10 06:52:59 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
For more info about the MQTT machine to machine messaging standard, go to <http://mqtt.org/>.
|
2014-11-05 00:41:56 +08:00
|
|
|
|
2014-11-29 07:34:42 +08:00
|
|
|
## How to Install
|
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md)
|
2014-11-05 00:41:56 +08:00
|
|
|
|
2014-11-29 07:34:42 +08:00
|
|
|
## How to Use
|
2014-11-05 00:41:56 +08:00
|
|
|
|
2014-11-29 07:34:42 +08:00
|
|
|
Before running the example, make sure you have an MQTT message broker running somewhere you can connect to
|
2014-11-05 00:41:56 +08:00
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/mqtt"
|
2014-11-05 00:41:56 +08:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-09-26 16:13:37 +08:00
|
|
|
mqttAdaptor := mqtt.NewAdaptor("tcp://0.0.0.0:1883", "pinger")
|
2014-11-05 00:41:56 +08:00
|
|
|
|
|
|
|
work := func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("hello", func(msg mqtt.Message) {
|
2017-04-19 01:49:17 +08:00
|
|
|
fmt.Println(msg)
|
2014-11-05 00:41:56 +08:00
|
|
|
})
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("hola", func(msg mqtt.Message) {
|
2017-04-19 01:49:17 +08:00
|
|
|
fmt.Println(msg)
|
2014-11-05 00:41:56 +08:00
|
|
|
})
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-05 00:41:56 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Supported Features
|
|
|
|
|
|
|
|
* Publish messages
|
|
|
|
* Respond to incoming message events
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
2018-02-14 15:24:39 +08:00
|
|
|
Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.
|