Do you have example of connection to AWS IoT. I have all certificates and keys, but can't connect to the broker. Got "Network Error : %!s()" |
looking at the log I can that:
here is my code:
` ///Start/////////////////////////////////
cer, err := tls.LoadX509KeyPair("path/f8527f7924-certificate.pem", "path/f8527f7924-private.pem.key")
check(err)
topic := "anom/pred"
qos := 1
//cid := "ClientID"
cid := "ad82c26c-3674-fb9f-44ec-15c0b59a0e66"
connOpts := &MQTT.ClientOptions{
ClientID: cid,
CleanSession: true,
AutoReconnect: true,
MaxReconnectInterval: 1 * time.Second,
KeepAlive: 30 * time.Second,
TLSConfig: tls.Config{Certificates: []tls.Certificate{cer}},
}
host := "a3a5un32wgqe74.iot.us-east-1.amazonaws.com"
mqttPort := 8883
path := "/mqtt"
brokerURL := fmt.Sprintf("tcps://%s:%d%s", host, mqttPort, path)
connOpts.AddBroker(brokerURL)
received := 0
connOpts.SetDefaultPublishHandler(func(client MQTT.Client, msg MQTT.Message) {
s := string(msg.Payload())
log.Printf("%d heard %s %s", received, msg.Topic(), s)
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
log.Printf("message '%s' not a time", s)
} else {
log.Printf("elasped %dms", int(time.Now().Sub(t).Seconds()*1000))
}
received++
})
mqttClient := MQTT.NewClient(connOpts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
// Subscribe
go func() {
log.Printf("subscribing")
if token := mqttClient.Subscribe(topic, byte(qos), nil); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
log.Printf("subscribed")
}()
log.Println("[MQTT] Connected")`