Working mqtt examples
This commit is contained in:
parent
a9cb290d1f
commit
ccbe8b21f5
|
@ -1,35 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/mqtt"
|
||||
"github.com/hybridgroup/gobot/platforms/firmata"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/firmata"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/platforms/mqtt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
mqttAdaptor := mqtt.NewMqttAdaptor("server", "localhost:1883")
|
||||
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
|
||||
led := gpio.NewLedDriver(firmataAdaptor, "led", "13")
|
||||
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://localhost:1883")
|
||||
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
|
||||
led := gpio.NewLedDriver(firmataAdaptor, "led", "13")
|
||||
|
||||
work := func() {
|
||||
mqttAdaptor.On('lights/on', func(data interface{}) {
|
||||
led.On()
|
||||
})
|
||||
mqttAdaptor.On('lights/off', func(data interface{}) {
|
||||
led.Off()
|
||||
})
|
||||
}
|
||||
work := func() {
|
||||
mqttAdaptor.On("lights/on", func(data interface{}) {
|
||||
led.On()
|
||||
})
|
||||
mqttAdaptor.On("lights/off", func(data interface{}) {
|
||||
led.Off()
|
||||
})
|
||||
data := []byte("")
|
||||
gobot.Every(1*time.Second, func() {
|
||||
mqttAdaptor.Publish("lights/on", data)
|
||||
})
|
||||
gobot.Every(2*time.Second, func() {
|
||||
mqttAdaptor.Publish("lights/off", data)
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("mqttBot",
|
||||
[]gobot.Connection{mqttAdaptor, firmataAdaptor},
|
||||
[]gobot.Device{led},
|
||||
work,
|
||||
)
|
||||
robot := gobot.NewRobot("mqttBot",
|
||||
[]gobot.Connection{mqttAdaptor, firmataAdaptor},
|
||||
[]gobot.Device{led},
|
||||
work,
|
||||
)
|
||||
|
||||
gbot.AddRobot(robot)
|
||||
gbot.AddRobot(robot)
|
||||
|
||||
gbot.Start()
|
||||
gbot.Start()
|
||||
}
|
||||
|
|
|
@ -1,39 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/mqtt"
|
||||
"fmt"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/mqtt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883")
|
||||
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883")
|
||||
|
||||
work := func() {
|
||||
mqttAdaptor.On("hello", func(data interface{}) {
|
||||
fmt.Println("hello")
|
||||
})
|
||||
mqttAdaptor.On("hola", func(data interface{}) {
|
||||
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)
|
||||
})
|
||||
}
|
||||
work := func() {
|
||||
mqttAdaptor.On("hello", func(data interface{}) {
|
||||
fmt.Println("hello")
|
||||
})
|
||||
mqttAdaptor.On("hola", func(data interface{}) {
|
||||
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,
|
||||
)
|
||||
robot := gobot.NewRobot("mqttBot",
|
||||
[]gobot.Connection{mqttAdaptor},
|
||||
work,
|
||||
)
|
||||
|
||||
gbot.AddRobot(robot)
|
||||
gbot.AddRobot(robot)
|
||||
|
||||
gbot.Start()
|
||||
gbot.Start()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue