gobot.io/x/gobot@v1.16.0/examples/mqtt_driver_ping.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 // TO RUN: 6 // go run ./examples/mqtt_driver_ping.go <SERVER> 7 // 8 // EXAMPLE: 9 // go run ./examples/mqtt_driver_ping.go ssl://iot.eclipse.org:8883 10 // 11 package main 12 13 import ( 14 "fmt" 15 "os" 16 "time" 17 18 "gobot.io/x/gobot" 19 "gobot.io/x/gobot/platforms/mqtt" 20 ) 21 22 func main() { 23 mqttAdaptor := mqtt.NewAdaptor(os.Args[1], "pinger") 24 mqttAdaptor.SetAutoReconnect(true) 25 26 holaDriver := mqtt.NewDriver(mqttAdaptor, "hola") 27 helloDriver := mqtt.NewDriver(mqttAdaptor, "hello") 28 29 work := func() { 30 helloDriver.On(mqtt.Data, func(data interface{}) { 31 fmt.Println("hello") 32 }) 33 34 holaDriver.On(mqtt.Data, func(data interface{}) { 35 fmt.Println("hola") 36 }) 37 38 data := []byte("o") 39 gobot.Every(1*time.Second, func() { 40 helloDriver.Publish(data) 41 }) 42 43 gobot.Every(5*time.Second, func() { 44 holaDriver.Publish(data) 45 }) 46 } 47 48 robot := gobot.NewRobot("mqttBot", 49 []gobot.Connection{mqttAdaptor}, 50 []gobot.Device{helloDriver, holaDriver}, 51 work, 52 ) 53 54 robot.Start() 55 }