openpdu/src/mqtt.go

34 lines
639 B
Go

package main
import (
"fmt"
"os"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
func init() {
// set the protocol, ip and port of the broker.
opts := MQTT.NewClientOptions().AddBroker("tcp://localhost:1883")
// set the id to the client.
opts.SetClientID("Device-pub")
// create a new client.
c := MQTT.NewClient(opts)
token := c.Connect()
token.Wait()
if token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
message := "hello this is the trial message"
c.Publish("some_topic", 0, false, message)
//c.Subscribe("some_topic", 0, nil);
c.Disconnect(250)
}
// https://girishjoshi.io/post/golang-paho-mqtt/