mqtt

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2018 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

README

MQTT

MQTT is an Internet of Things connectivity protocol featuring a lightweight publish/subscribe messaging transport. It is useful for its small code footprint and minimal network bandwidth usage.

This repository contains the Gobot adaptor/driver to connect to MQTT servers. It uses the Paho MQTT Golang client package (https://eclipse.org/paho/) created and maintained by the Eclipse Foundation (https://github.com/eclipse) thank you!

For more info about the MQTT machine to machine messaging standard, go to http://mqtt.org/

How to Install

Install running:

go get -d -u gobot.io/x/gobot/...

How to Use

Before running the example, make sure you have an MQTT message broker running somewhere you can connect to

package main

import (
  "gobot.io/x/gobot"
  "gobot.io/x/gobot/platforms/mqtt"
  "fmt"
  "time"
)

func main() {
  mqttAdaptor := mqtt.NewAdaptor("tcp://0.0.0.0:1883", "pinger")

  work := func() {
    mqttAdaptor.On("hello", func(msg mqtt.Message) {
      fmt.Println(msg)
    })
    mqttAdaptor.On("hola", func(msg mqtt.Message) {
      fmt.Println(msg)
    })
    data := []byte("o")
    gobot.Every(1*time.Second, func() {
      mqttAdaptor.Publish("hello", data)
    })
    gobot.Every(5*time.Second, func() {
      mqttAdaptor.Publish("hola", data)
    })
  }

  robot := gobot.NewRobot("mqttBot",
    []gobot.Connection{mqttAdaptor},
    work,
  )

  robot.Start()
}

Supported Features

  • Publish messages
  • Respond to incoming message events

Contributing

For our contribution guidelines, please go to https://gobot.io/x/gobot/blob/master/CONTRIBUTING.md

License

Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.

Documentation

Overview

Package mqtt provides Gobot adaptor for the mqtt message service.

Installing:

go get gobot.io/x/gobot/platforms/mqtt

For further information refer to mqtt README: https://github.com/hybridgroup/gobot/blob/master/platforms/mqtt/README.md

Index

Constants

View Source
const (
	// Data event when data is available for Driver
	Data = "data"

	// Error event when error occurs in Driver
	Error = "error"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adaptor added in v1.0.0

type Adaptor struct {
	Host string
	// contains filtered or unexported fields
}

Adaptor is the Gobot Adaptor for MQTT

func NewAdaptor added in v1.0.0

func NewAdaptor(host string, clientID string) *Adaptor

NewAdaptor creates a new mqtt adaptor with specified host and client id

func NewAdaptorWithAuth added in v1.0.0

func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor

NewAdaptorWithAuth creates a new mqtt adaptor with specified host, client id, username, and password.

func (*Adaptor) AutoReconnect added in v1.2.0

func (a *Adaptor) AutoReconnect() bool

AutoReconnect returns the MQTT AutoReconnect setting

func (*Adaptor) CleanSession added in v1.7.0

func (a *Adaptor) CleanSession() bool

CleanSession returns the MQTT CleanSession setting

func (*Adaptor) ClientCert added in v1.2.0

func (a *Adaptor) ClientCert() string

ClientCert returns the MQTT client SSL cert file

func (*Adaptor) ClientKey added in v1.2.0

func (a *Adaptor) ClientKey() string

ClientKey returns the MQTT client SSL key file

func (*Adaptor) Connect added in v1.0.0

func (a *Adaptor) Connect() (err error)

Connect returns true if connection to mqtt is established

func (*Adaptor) Disconnect added in v1.0.0

func (a *Adaptor) Disconnect() (err error)

Disconnect returns true if connection to mqtt is closed

func (*Adaptor) Finalize added in v1.0.0

func (a *Adaptor) Finalize() (err error)

Finalize returns true if connection to mqtt is finalized successfully

func (*Adaptor) Name added in v1.0.0

func (a *Adaptor) Name() string

Name returns the MQTT Adaptor's name

func (*Adaptor) On added in v1.0.0

func (a *Adaptor) On(event string, f func(msg Message)) bool

On subscribes to a topic, and then calls the message handler function when data is received

func (*Adaptor) Port added in v1.2.0

func (a *Adaptor) Port() string

Port returns the Host name

func (*Adaptor) Publish added in v1.0.0

func (a *Adaptor) Publish(topic string, message []byte) bool

Publish a message under a specific topic

func (*Adaptor) ServerCert added in v1.2.0

func (a *Adaptor) ServerCert() string

ServerCert returns the MQTT server SSL cert file

func (*Adaptor) SetAutoReconnect added in v1.2.0

func (a *Adaptor) SetAutoReconnect(val bool)

SetAutoReconnect sets the MQTT AutoReconnect setting

func (*Adaptor) SetCleanSession added in v1.7.0

func (a *Adaptor) SetCleanSession(val bool)

SetCleanSession sets the MQTT CleanSession setting. Should be false if reconnect is enabled. Otherwise all subscriptions will be lost

func (*Adaptor) SetClientCert added in v1.2.0

func (a *Adaptor) SetClientCert(val string)

SetClientCert sets the MQTT client SSL cert file

func (*Adaptor) SetClientKey added in v1.2.0

func (a *Adaptor) SetClientKey(val string)

SetClientKey sets the MQTT client SSL key file

func (*Adaptor) SetName added in v1.0.0

func (a *Adaptor) SetName(n string)

SetName sets the MQTT Adaptor's name

func (*Adaptor) SetServerCert added in v1.2.0

func (a *Adaptor) SetServerCert(val string)

SetServerCert sets the MQTT server SSL cert file

func (*Adaptor) SetUseSSL added in v1.2.0

func (a *Adaptor) SetUseSSL(val bool)

SetUseSSL sets the MQTT server SSL preference

func (*Adaptor) UseSSL added in v1.2.0

func (a *Adaptor) UseSSL() bool

UseSSL returns the MQTT server SSL preference

type Driver added in v1.2.0

type Driver struct {
	gobot.Eventer
	gobot.Commander
	// contains filtered or unexported fields
}

Driver for mqtt

func NewDriver added in v1.2.0

func NewDriver(a *Adaptor, topic string) *Driver

NewDriver returns a new Gobot MQTT Driver

func (*Driver) Connection added in v1.2.0

func (m *Driver) Connection() gobot.Connection

Connection returns Connections used by the Driver

func (*Driver) Halt added in v1.2.0

func (m *Driver) Halt() error

Halt halts the Driver

func (*Driver) Name added in v1.2.0

func (m *Driver) Name() string

Name returns name for the Driver

func (*Driver) On added in v1.2.0

func (m *Driver) On(n string, f func(msg interface{})) error

On subscribes to data updates for the current device topic, and then calls the message handler function when data is received

func (*Driver) Publish added in v1.2.0

func (m *Driver) Publish(data interface{}) bool

Publish a message to the current device topic

func (*Driver) SetName added in v1.2.0

func (m *Driver) SetName(name string)

Name sets name for the Driver

func (*Driver) SetTopic added in v1.2.0

func (m *Driver) SetTopic(topic string)

SetTopic sets the current topic for the Driver

func (*Driver) Start added in v1.2.0

func (m *Driver) Start() error

Start starts the Driver

func (*Driver) Topic added in v1.2.0

func (m *Driver) Topic() string

Topic returns the current topic for the Driver

type Message added in v1.5.0

type Message paho.Message

Message is a message received from the broker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL