gobot.io/x/gobot@v1.16.0/examples/mqtt_firmata_blink.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"time"
     9  
    10  	"gobot.io/x/gobot"
    11  	"gobot.io/x/gobot/drivers/gpio"
    12  	"gobot.io/x/gobot/platforms/firmata"
    13  	"gobot.io/x/gobot/platforms/mqtt"
    14  )
    15  
    16  func main() {
    17  	mqttAdaptor := mqtt.NewAdaptor("tcp://test.mosquitto.org:1883", "blinker")
    18  	firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
    19  	led := gpio.NewLedDriver(firmataAdaptor, "13")
    20  
    21  	work := func() {
    22  		mqttAdaptor.On("lights/on", func(msg mqtt.Message) {
    23  			led.On()
    24  		})
    25  		mqttAdaptor.On("lights/off", func(msg mqtt.Message) {
    26  			led.Off()
    27  		})
    28  		data := []byte("")
    29  		gobot.Every(1*time.Second, func() {
    30  			mqttAdaptor.Publish("lights/on", data)
    31  		})
    32  		gobot.Every(2*time.Second, func() {
    33  			mqttAdaptor.Publish("lights/off", data)
    34  		})
    35  	}
    36  
    37  	robot := gobot.NewRobot("mqttBot",
    38  		[]gobot.Connection{mqttAdaptor, firmataAdaptor},
    39  		[]gobot.Device{led},
    40  		work,
    41  	)
    42  
    43  	robot.Start()
    44  }