|
@@ -54,11 +54,15 @@ func main() {
|
|
|
broker := os.Getenv("broker_mqtt")
|
|
broker := os.Getenv("broker_mqtt")
|
|
|
ClientID := os.Getenv("clientid_mqtt")
|
|
ClientID := os.Getenv("clientid_mqtt")
|
|
|
topic := os.Getenv("topic_mqtt")
|
|
topic := os.Getenv("topic_mqtt")
|
|
|
|
|
+ username := os.Getenv("login_mqtt")
|
|
|
|
|
+ password := os.Getenv("password_mqtt")
|
|
|
|
|
|
|
|
opts := mqtt.NewClientOptions()
|
|
opts := mqtt.NewClientOptions()
|
|
|
opts.AddBroker(broker)
|
|
opts.AddBroker(broker)
|
|
|
opts.SetClientID(ClientID)
|
|
opts.SetClientID(ClientID)
|
|
|
opts.SetConnectTimeout(5 * time.Second)
|
|
opts.SetConnectTimeout(5 * time.Second)
|
|
|
|
|
+ opts.SetUsername(username)
|
|
|
|
|
+ opts.SetPassword(password)
|
|
|
|
|
|
|
|
client := mqtt.NewClient(opts)
|
|
client := mqtt.NewClient(opts)
|
|
|
if token := client.Connect(); token.Wait() && token.Error() != nil { log.Fatalf("%v\n", token.Error()) }
|
|
if token := client.Connect(); token.Wait() && token.Error() != nil { log.Fatalf("%v\n", token.Error()) }
|
|
@@ -68,11 +72,13 @@ func main() {
|
|
|
city, temp := WeatherReq()
|
|
city, temp := WeatherReq()
|
|
|
payload := fmt.Sprintf("City: %s, Temperature: %.2f°C", city, temp)
|
|
payload := fmt.Sprintf("City: %s, Temperature: %.2f°C", city, temp)
|
|
|
|
|
|
|
|
- token := client.Publish(topic, 0, false, payload)
|
|
|
|
|
|
|
+ token := client.Publish(topic, 1, false, payload)
|
|
|
|
|
+ ticket := time.NewTicker(2 * time.Second)
|
|
|
|
|
+ defer ticket.Stop()
|
|
|
token.Wait()
|
|
token.Wait()
|
|
|
if err = token.Error(); err != nil {
|
|
if err = token.Error(); err != nil {
|
|
|
log.Fatalf("%v\n", token.Error)
|
|
log.Fatalf("%v\n", token.Error)
|
|
|
} else {
|
|
} else {
|
|
|
fmt.Println("Sent in", topic)
|
|
fmt.Println("Sent in", topic)
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|