You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is an example showing how to use this library:
package main
import (
"context""log""github.com/vaelen/iot"// Your client must include the paho package// to use the default Eclipse Paho MQTT client.
_ "github.com/vaelen/iot/paho"
)
funcmain() {
ctx:=context.Background()
id:=&iot.ID{
DeviceID: "deviceName",
Registry: "my-registry",
Location: "asia-east1",
ProjectID: "my-project",
}
credentials, err:=iot.LoadRSACredentials("rsa_cert.pem", "rsa_private.pem")
iferr!=nil {
panic("Couldn't load credentials")
}
options:=iot.DefaultOptions(id, credentials)
options.DebugLogger=log.Printlnoptions.InfoLogger=log.Printlnoptions.ErrorLogger=log.Printlnoptions.ConfigHandler=func(thing iot.Thing, config []byte) {
// Do something here to process the updated config and create an updated state stringstate:= []byte("ok")
thing.PublishState(ctx, state)
}
thing:=iot.New(options)
err=thing.Connect(ctx, "ssl://mqtt.googleapis.com:443")
iferr!=nil {
panic("Couldn't connect to server")
}
deferthing.Disconnect(ctx)
// This publishes to /eventsthing.PublishEvent(ctx, []byte("Top level telemetry event"))
// This publishes to /events/athing.PublishEvent(ctx, []byte("Sub folder telemetry event"), "a")
// This publishes to /events/a/bthing.PublishEvent(ctx, []byte("Sub folder telemetry event"), "a", "b")
}
Thanks to Infostellar for supporting my development of this project.