mqtt

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 4

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

Please refer to the main README.md

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/v2"
  "gobot.io/x/gobot/v2/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

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:

Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md)

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

View Source
var ErrNilClient = fmt.Errorf("no MQTT client available")

ErrNilClient is returned when a client action can't be taken because the struct has no client

Functions

This section is empty.

Types

type Adaptor

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

Adaptor is the Gobot Adaptor for MQTT

func NewAdaptor

func NewAdaptor(host string, clientID string) *Adaptor

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

func NewAdaptorWithAuth

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

func (a *Adaptor) AutoReconnect() bool

AutoReconnect returns the MQTT AutoReconnect setting

func (*Adaptor) CleanSession

func (a *Adaptor) CleanSession() bool

CleanSession returns the MQTT CleanSession setting

func (*Adaptor) ClientCert

func (a *Adaptor) ClientCert() string

ClientCert returns the MQTT client SSL cert file

func (*Adaptor) ClientKey

func (a *Adaptor) ClientKey() string

ClientKey returns the MQTT client SSL key file

func (*Adaptor) Connect

func (a *Adaptor) Connect() error

Connect returns true if connection to mqtt is established

func (*Adaptor) Disconnect

func (a *Adaptor) Disconnect() error

Disconnect returns true if connection to mqtt is closed

func (*Adaptor) Finalize

func (a *Adaptor) Finalize() error

Finalize returns true if connection to mqtt is finalized successfully

func (*Adaptor) Name

func (a *Adaptor) Name() string

Name returns the MQTT Adaptor's name

func (*Adaptor) On

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) OnWithQOS

func (a *Adaptor) OnWithQOS(event string, qos int, f func(msg Message)) (paho.Token, error)

OnWithQOS allows per-subscribe QOS values to be set and returns a paho.Token

func (*Adaptor) Port

func (a *Adaptor) Port() string

Port returns the Host name

func (*Adaptor) Publish

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

Publish a message under a specific topic

func (*Adaptor) PublishAndRetain

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

PublishAndRetain publishes a message under a specific topic with retain flag

func (*Adaptor) PublishWithQOS

func (a *Adaptor) PublishWithQOS(topic string, qos int, message []byte) (paho.Token, error)

PublishWithQOS allows per-publish QOS values to be set and returns a paho.Token

func (*Adaptor) ServerCert

func (a *Adaptor) ServerCert() string

ServerCert returns the MQTT server SSL cert file

func (*Adaptor) SetAutoReconnect

func (a *Adaptor) SetAutoReconnect(val bool)

SetAutoReconnect sets the MQTT AutoReconnect setting

func (*Adaptor) SetCleanSession

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

func (a *Adaptor) SetClientCert(val string)

SetClientCert sets the MQTT client SSL cert file

func (*Adaptor) SetClientKey

func (a *Adaptor) SetClientKey(val string)

SetClientKey sets the MQTT client SSL key file

func (*Adaptor) SetName

func (a *Adaptor) SetName(n string)

SetName sets the MQTT Adaptor's name

func (*Adaptor) SetQoS

func (a *Adaptor) SetQoS(qos int)

SetQoS sets the QoS value passed into the MTT client on Publish/Subscribe events

func (*Adaptor) SetServerCert

func (a *Adaptor) SetServerCert(val string)

SetServerCert sets the MQTT server SSL cert file

func (*Adaptor) SetUseSSL

func (a *Adaptor) SetUseSSL(val bool)

SetUseSSL sets the MQTT server SSL preference

func (*Adaptor) UseSSL

func (a *Adaptor) UseSSL() bool

UseSSL returns the MQTT server SSL preference

type Driver

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

Driver for mqtt

func NewDriver

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

NewDriver returns a new Gobot MQTT Driver

func (*Driver) Connection

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

Connection returns Connections used by the Driver

func (*Driver) Halt

func (m *Driver) Halt() error

Halt halts the Driver

func (*Driver) Name

func (m *Driver) Name() string

Name returns name for the Driver

func (*Driver) On

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

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

Publish a message to the current device topic

func (*Driver) SetName

func (m *Driver) SetName(name string)

Name sets name for the Driver

func (*Driver) SetTopic

func (m *Driver) SetTopic(topic string)

SetTopic sets the current topic for the Driver

func (*Driver) Start

func (m *Driver) Start() error

Start starts the Driver

func (*Driver) Topic

func (m *Driver) Topic() string

Topic returns the current topic for the Driver

type Message

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