Created
March 26, 2022 22:03
-
-
Save rustyeddy/482556caef8010b1b0cc266007e9aec6 to your computer and use it in GitHub Desktop.
Adding MQTT to a Go program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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