Documentation ¶
Overview ¶
package awsiotcore eases interaction with AWS IoT Core over MQTT. It handles TLS configuration and authentication.
Example ¶
package main import ( "log" "time" "github.com/mtraver/awsiotcore" ) func main() { d := awsiotcore.Device{ Endpoint: "my-endpoint", DeviceID: "my-device", // roots.pem should contain the root CA certs described in the README. CACerts: "roots.pem", CertPath: "my-device.x509", PrivKeyPath: "my-device.pem", } client, err := d.NewClient() if err != nil { log.Fatalf("Failed to make MQTT client: %v", err) } if token := client.Connect(); !token.Wait() || token.Error() != nil { log.Fatalf("Failed to connect to MQTT broker: %v", token.Error()) } if token := client.Publish(d.TelemetryTopic(), 1, false, []byte("{\"temp\": 18.0}")); !token.Wait() || token.Error() != nil { log.Printf("Failed to publish: %v", token.Error()) } client.Disconnect(250) time.Sleep(500 * time.Millisecond) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeviceIDFromCert ¶
DeviceIDFromCert gets the Common Name from an X.509 cert, which for the purposes of this package is considered to be the device ID.
Types ¶
type Device ¶
type Device struct { Endpoint string DeviceID string `json:"device_id"` TelemetryTopicOverride string `json:"telemetry_topic"` // CACerts must contain the path to a .pem file containing Amazon's trusted root certs. See the README for more info. CACerts string `json:"ca_certs_path"` CertPath string `json:"cert_path"` PrivKeyPath string `json:"priv_key_path"` }
Device represents an AWS IoT device.
func (*Device) Broker ¶
func (d *Device) Broker() MQTTBroker
func (*Device) NewClient ¶
func (d *Device) NewClient(options ...func(*Device, *mqtt.ClientOptions) error) (mqtt.Client, error)
NewClient creates a github.com/eclipse/paho.mqtt.golang Client that may be used to connect to the device's MQTT broker using TLS. By default it sets up a github.com/eclipse/paho.mqtt.golang ClientOptions with the minimal options required to establish a connection:
- Broker
- Client ID set to the device's ID
- TLS configuration that supplies root CA certs, the device's cert, and Server Name Indication (SNI) (required by AWS IoT)
By passing in options you may customize the ClientOptions. Options are functions with this signature:
func(*Device, *mqtt.ClientOptions) error
They modify the ClientOptions. The option functions are applied to the ClientOptions in the order given before the Client is created. For example, if you wish to set the connect timeout, you might write this:
func ConnectTimeout(t time.Duration) func(*Device, *mqtt.ClientOptions) error { return func(d *Device, opts *mqtt.ClientOptions) error { opts.SetConnectTimeout(t) return nil } }
No options are required to establish a connection but they allow for customizability.
For more information about connecting to AWS IoT MQTT brokers see https://docs.aws.amazon.com/iot/latest/developerguide/iot-connect-devices.html.
func (*Device) TelemetryTopic ¶
TelemetryTopic returns the MQTT topic to which the device should publish telemetry events.
type MQTTBroker ¶
MQTTBroker represents an MQTT server.
func (*MQTTBroker) String ¶
func (b *MQTTBroker) String() string
String returns a string representation of the MQTTBroker.