Implemented additional logic req
All checks were successful
Build Docker image / build (push) Successful in 1m2s
All checks were successful
Build Docker image / build (push) Successful in 1m2s
This commit is contained in:
parent
5be69f9baf
commit
8673fabccd
1 changed files with 10 additions and 6 deletions
16
main.go
16
main.go
|
@ -67,6 +67,16 @@ func main() {
|
||||||
opts.SetUsername(os.Getenv("MQTT_USERNAME"))
|
opts.SetUsername(os.Getenv("MQTT_USERNAME"))
|
||||||
opts.SetPassword(os.Getenv("MQTT_PASSWORD"))
|
opts.SetPassword(os.Getenv("MQTT_PASSWORD"))
|
||||||
opts.SetAutoReconnect(true)
|
opts.SetAutoReconnect(true)
|
||||||
|
opts.SetConnectionLostHandler(func(client mqtt.Client, err error) {
|
||||||
|
log.Printf("Connection lost: %v", err)
|
||||||
|
})
|
||||||
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
||||||
|
topic := os.Getenv("MQTT_TOPIC")
|
||||||
|
log.Println("Connected to MQTT broker, subscribing to topic...")
|
||||||
|
if token := client.Subscribe(topic, 0, mqttMessageHandler); token.Wait() && token.Error() != nil {
|
||||||
|
log.Printf("Error subscribing to MQTT topic %s: %v", topic, token.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Connect to MQTT broker
|
// Connect to MQTT broker
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
|
@ -74,12 +84,6 @@ func main() {
|
||||||
log.Fatal(token.Error())
|
log.Fatal(token.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe to MQTT topic
|
|
||||||
topic := os.Getenv("MQTT_TOPIC")
|
|
||||||
if token := client.Subscribe(topic, 0, mqttMessageHandler); token.Wait() && token.Error() != nil {
|
|
||||||
log.Fatal(token.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep the program running
|
// Keep the program running
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue