gobot.io/x/gobot/v2@v2.1.0/examples/mqtt_firmata_blink.go (about)

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