Connecting AWS iot using paho.mqtt.golang

1,267 views
Skip to first unread message

Eran Lavi

unread,
May 13, 2017, 11:48:44 AM5/13/17
to MQTT

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()"

I am tring to connect to mqtt in my aws iot account.
But for some resone It failes with the error:
"Network Error : %!s()"
Can anyone help me?
What am I doing wrong????
I enable the trace
It looks like It managed to do the connection:

 looking at the log I can that:

  • socket connected to broker.
  • connect started.
but just then it recives:
connect got error EOF.

Here is my code:

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")`

Eran Lavi

unread,
May 13, 2017, 11:50:36 AM5/13/17
to MQTT
Here you can find the complite logging messages:

[client]   Connect()
[store]    memorystore initialized
[client]   about to write new connect msg
[client]   socket connected to broker
[client]   Using MQTT 3.1.1 protocol
[net]      connect started
[net]      connect got error EOF
[client]   Trying reconnect using MQTT 3.1 protocol
[client]   about to write new connect msg
[client]   socket connected to broker
[client]   Using MQTT 3.1 protocol
[net]      connect started
[net]      connect got error EOF
[client]   Failed to connect to a broker
[store]    memorystore closed
panic: Network Error : %!s(<nil>)
 
Reply all
Reply to author
Forward
0 new messages