gobot.io/x/gobot@v1.16.0/examples/ollie_mqtt.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 package main 6 7 import ( 8 "os" 9 "strconv" 10 "time" 11 12 "gobot.io/x/gobot" 13 "gobot.io/x/gobot/platforms/ble" 14 "gobot.io/x/gobot/platforms/mqtt" 15 "gobot.io/x/gobot/platforms/sphero/ollie" 16 ) 17 18 const ( 19 FRENTE = 0 20 DERECHA = 90 21 ATRAS = 180 22 IZQUIERDA = 270 23 ) 24 25 func main() { 26 bleAdaptor := ble.NewClientAdaptor(os.Args[1]) 27 ollie := ollie.NewDriver(bleAdaptor) 28 29 mqttAdaptor := mqtt.NewAdaptor("tcp://iot.eclipse.org:1883", "ollie") 30 31 work := func() { 32 ollie.SetRGB(255, 0, 255) 33 34 mqttAdaptor.On("sensores/dial", func(msg mqtt.Message) { 35 val, _ := strconv.Atoi(string(msg.Payload())) 36 37 if val > 2000 { 38 ollie.SetRGB(0, 255, 0) 39 return 40 } 41 if val > 1000 { 42 ollie.SetRGB(255, 255, 0) 43 return 44 } 45 ollie.SetRGB(255, 0, 0) 46 }) 47 48 mqttAdaptor.On("rover/frente", func(msg mqtt.Message) { 49 ollie.Roll(40, FRENTE) 50 gobot.After(1*time.Second, func() { 51 ollie.Stop() 52 }) 53 }) 54 55 mqttAdaptor.On("rover/derecha", func(msg mqtt.Message) { 56 ollie.Roll(40, DERECHA) 57 gobot.After(1*time.Second, func() { 58 ollie.Stop() 59 }) 60 }) 61 62 mqttAdaptor.On("rover/atras", func(msg mqtt.Message) { 63 ollie.Roll(40, ATRAS) 64 gobot.After(1*time.Second, func() { 65 ollie.Stop() 66 }) 67 }) 68 69 mqttAdaptor.On("rover/izquierda", func(msg mqtt.Message) { 70 ollie.Roll(40, IZQUIERDA) 71 gobot.After(1*time.Second, func() { 72 ollie.Stop() 73 }) 74 }) 75 } 76 77 robot := gobot.NewRobot("ollieBot", 78 []gobot.Connection{bleAdaptor, mqttAdaptor}, 79 []gobot.Device{ollie}, 80 work, 81 ) 82 83 robot.Start() 84 }