mqtt

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2016 License: 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 github.com/hybridgroup/gobot/... && go install github.com/hybridgroup/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 (
  "github.com/hybridgroup/gobot"
  "github.com/hybridgroup/gobot/platforms/mqtt"
  "fmt"
  "time"
)

func main() {
  gbot := gobot.NewGobot()

  mqttAdaptor := mqtt.NewMqttAdaptor("server", "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,
  )

  gbot.AddRobot(robot)

  gbot.Start()
}

Supported Features

  • Publish messages
  • Respond to incoming message events

Contributing

For our contribution guidelines, please go to https://github.com/hybridgroup/gobot/blob/master/CONTRIBUTING.md

License

Copyright (c) 2013-2016 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 github.com/hybridgroup/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 MqttAdaptor

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

func NewMqttAdaptor

func NewMqttAdaptor(name string, host string, clientID string) *MqttAdaptor

NewMqttAdaptor creates a new mqtt adaptor with specified name, host and client id

func (*MqttAdaptor) Connect

func (a *MqttAdaptor) Connect() (errs []error)

Connect returns true if connection to mqtt is established

func (*MqttAdaptor) Disconnect

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

Disconnect returns true if connection to mqtt is closed

func (*MqttAdaptor) Finalize

func (a *MqttAdaptor) Finalize() (errs []error)

Finalize returns true if connection to mqtt is finalized succesfully

func (*MqttAdaptor) Name

func (a *MqttAdaptor) Name() string

func (*MqttAdaptor) On

func (a *MqttAdaptor) On(event string, f func(s []byte)) bool

Subscribe to a topic, and then call the message handler function when data is received

func (*MqttAdaptor) Publish

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

Publish a message under a specific topic

Jump to

Keyboard shortcuts

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