mqtt

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2017 License: Apache-2.0, Apache-2.0 Imports: 2 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/drivers 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/... && go install gobot.io/x/gobot/platforms/mqtt

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(data []byte) {
      fmt.Println("hello")
    })
    mqttAdaptor.On("hola", func(data []byte) {
      fmt.Println("hola")
    })
    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-2017 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

This section is empty.

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) 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(s []byte)) bool

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

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) SetName added in v1.0.0

func (a *Adaptor) SetName(n string)

SetName sets the MQTT Adaptor's name

Jump to

Keyboard shortcuts

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