2023-05-20 14:25:21 +02:00
|
|
|
//go:build example
|
2017-03-13 11:01:39 -04:00
|
|
|
// +build example
|
2023-05-20 14:25:21 +02:00
|
|
|
|
2017-03-13 11:01:39 -04:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2014-11-04 07:12:02 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-11 15:34:50 +01:00
|
|
|
"fmt"
|
2014-11-06 12:28:04 -08:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/firmata"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/mqtt"
|
2014-11-04 07:12:02 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-03 16:58:43 +02:00
|
|
|
mqttAdaptor := mqtt.NewAdaptor("tcp://test.mosquitto.org:1883", "blinker")
|
|
|
|
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
|
|
|
|
led := gpio.NewLedDriver(firmataAdaptor, "13")
|
2014-11-04 07:12:02 -08:00
|
|
|
|
2014-11-04 08:54:06 -08:00
|
|
|
work := func() {
|
2024-02-11 15:34:50 +01:00
|
|
|
_ = mqttAdaptor.On("lights/on", func(msg mqtt.Message) {
|
|
|
|
if err := led.On(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2014-11-04 08:54:06 -08:00
|
|
|
})
|
2024-02-11 15:34:50 +01:00
|
|
|
_ = mqttAdaptor.On("lights/off", func(msg mqtt.Message) {
|
|
|
|
if err := led.Off(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2014-11-04 08:54:06 -08:00
|
|
|
})
|
|
|
|
data := []byte("")
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
mqttAdaptor.Publish("lights/on", data)
|
|
|
|
})
|
|
|
|
gobot.Every(2*time.Second, func() {
|
|
|
|
mqttAdaptor.Publish("lights/off", data)
|
|
|
|
})
|
|
|
|
}
|
2014-11-04 07:12:02 -08:00
|
|
|
|
2014-11-04 08:54:06 -08:00
|
|
|
robot := gobot.NewRobot("mqttBot",
|
|
|
|
[]gobot.Connection{mqttAdaptor, firmataAdaptor},
|
|
|
|
[]gobot.Device{led},
|
|
|
|
work,
|
|
|
|
)
|
2014-11-04 07:12:02 -08:00
|
|
|
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-04 07:12:02 -08:00
|
|
|
}
|