gobot.io/x/gobot/v2@v2.1.0/platforms/mqtt/README.md (about)

     1  # MQTT
     2  
     3  MQTT is an Internet of Things connectivity protocol featuring a lightweight publish/subscribe messaging transport. It is
     4  useful for its small code footprint and minimal network bandwidth usage.
     5  
     6  This repository contains the Gobot adaptor/driver to connect to MQTT servers. It uses the Paho MQTT Golang client package
     7  (<https://eclipse.org/paho/>) created and maintained by the Eclipse Foundation (<https://github.com/eclipse>) thank you!
     8  
     9  For more info about the MQTT machine to machine messaging standard, go to <http://mqtt.org/>.
    10  
    11  ## How to Install
    12  
    13  Install running:
    14  
    15  ```sh
    16  go get -d -u gobot.io/x/gobot/v2/...
    17  ```
    18  
    19  ## How to Use
    20  
    21  Before running the example, make sure you have an MQTT message broker running somewhere you can connect to
    22  
    23  ```go
    24  package main
    25  
    26  import (
    27    "gobot.io/x/gobot/v2"
    28    "gobot.io/x/gobot/v2/platforms/mqtt"
    29    "fmt"
    30    "time"
    31  )
    32  
    33  func main() {
    34    mqttAdaptor := mqtt.NewAdaptor("tcp://0.0.0.0:1883", "pinger")
    35  
    36    work := func() {
    37      mqttAdaptor.On("hello", func(msg mqtt.Message) {
    38        fmt.Println(msg)
    39      })
    40      mqttAdaptor.On("hola", func(msg mqtt.Message) {
    41        fmt.Println(msg)
    42      })
    43      data := []byte("o")
    44      gobot.Every(1*time.Second, func() {
    45        mqttAdaptor.Publish("hello", data)
    46      })
    47      gobot.Every(5*time.Second, func() {
    48        mqttAdaptor.Publish("hola", data)
    49      })
    50    }
    51  
    52    robot := gobot.NewRobot("mqttBot",
    53      []gobot.Connection{mqttAdaptor},
    54      work,
    55    )
    56  
    57    robot.Start()
    58  }
    59  ```
    60  
    61  ## Supported Features
    62  
    63  * Publish messages
    64  * Respond to incoming message events
    65  
    66  ## License
    67  
    68  Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.