Skip to content

Instantly share code, notes, and snippets.

@rustyeddy
Created March 26, 2022 22:03
Show Gist options
  • Save rustyeddy/482556caef8010b1b0cc266007e9aec6 to your computer and use it in GitHub Desktop.
Save rustyeddy/482556caef8010b1b0cc266007e9aec6 to your computer and use it in GitHub Desktop.
Adding MQTT to a Go program
package main
import (
"fmt"
"log"
"os"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var (
mqttc mqtt.Client
)
func mqtt_connect() {
if config.DebugMQTT {
mqtt.DEBUG = log.New(os.Stdout, "", 0)
mqtt.ERROR = log.New(os.Stdout, "", 0)
}
id := "sensorStation"
connOpts := mqtt.NewClientOptions().AddBroker(config.Broker).SetClientID(id).SetCleanSession(true)
mqttc = mqtt.NewClient(connOpts)
if token := mqttc.Connect(); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment