// Demos the Gobot Every function, which provides a way // to trigger recurring functionality. func blinkLedOverAndOver(gbot *gobot.Gobot) { // Create an instance of our chosen adapter type // and pass it to the LED driver. The names given here // are used in the management functionality. beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "P9_15") // Robots in the Gobot colletion run a "work" function // when they fire work := func() { gobot.Every(1*time.Second, func() { led.Toggle() }) } // A Robot is a board or device, and is one of the things managed by a Gobot. // Here we make one with the adaptor and led objects we made above. // The constructor creates a new named robot, provided a connection and a device // which will map to something like a GPIO pin. // A Robot can be composed of as many Connections and Devices as you like, // meaning that you can create something out of a group of supported hardware pieces // and treat it in code as a single logical unit. robot := gobot.NewRobot("blinkBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{led}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "P9_14") work := func() { brightness := uint8(0) fadeAmount := uint8(5) gobot.Every(100*time.Millisecond, func() { led.Brightness(brightness) brightness = brightness + fadeAmount if brightness == 0 || brightness == 255 { fadeAmount = -fadeAmount } }) } robot := gobot.NewRobot("pwmBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{led}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") blinkm := i2c.NewBlinkMDriver(beagleboneAdaptor, "blinkm") work := func() { gobot.Every(3*time.Second, func() { r := byte(gobot.Rand(255)) g := byte(gobot.Rand(255)) b := byte(gobot.Rand(255)) blinkm.Rgb(r, g, b) color, _ := blinkm.Color() fmt.Println("color", color) }) } robot := gobot.NewRobot("blinkmBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{blinkm}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") led := gpio.NewDirectPinDriver(beagleboneAdaptor, "led", "P8_10") button := gpio.NewDirectPinDriver(beagleboneAdaptor, "button", "P8_9") work := func() { gobot.Every(500*time.Millisecond, func() { if button.DigitalRead() == 1 { led.DigitalWrite(1) } else { led.DigitalWrite(0) } }) } robot := gobot.NewRobot("pinBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{led}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") button := gpio.NewMakeyButtonDriver(beagleboneAdaptor, "button", "P8_9") work := func() { button.On(gpio.ButtonPush, func(data interface{}) { fmt.Println("button pressed") }) button.On(gpio.ButtonRelease, func(data interface{}) { fmt.Println("button released") }) } robot := gobot.NewRobot("makeyBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{button}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") sensor := gpio.NewAnalogSensorDriver(beagleboneAdaptor, "sensor", "P9_33") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "P9_14") work := func() { sensor.On(sensor.Event("data"), func(data interface{}) { brightness := uint8( gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255), ) fmt.Println("sensor", data) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{sensor, led}, work, ) gbot.AddRobot(robot) gbot.Start() }
// Run some simple GPIO interactions // by taking control of the Direct Pin abstraction func setDirectPin(state int) { beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") gpioPin := gpio.NewDirectPinDriver(beagleboneAdaptor, "myDevice", "P9_12") // Initialize the internal representation of the pinout beagleboneAdaptor.Connect() // Cast to byte because we are returning an int from a function // and not passing in an int literal. gpioPin.DigitalWrite(byte(state)) }
func main() { // Use Gobot to control BeagleBone's digital pins directly beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") gpioPin := gpio.NewDirectPinDriver(beagleboneAdaptor, "myDevice", "P9_12") // Initialize the internal representation of the pinout beagleboneAdaptor.Connect() // Cast to byte because we are returning an int from a function // and not passing in an int literal. gpioPin.DigitalWrite(byte(myStateFunction())) }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "P9_12") work := func() { gobot.Every(1*time.Second, func() { led.Toggle() }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("blinkBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{led}, work)) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") servo := gpio.NewServoDriver(beagleboneAdaptor, "servo", "P9_14") work := func() { gobot.Every(1*time.Second, func() { i := uint8(gobot.Rand(180)) fmt.Println("Turning", i) servo.Move(i) }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("servoBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{servo}, work)) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") sensor := gpio.NewAnalogSensorDriver(beagleboneAdaptor, "sensor", "P9_33") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "P9_14") work := func() { gobot.Every(100*time.Millisecond, func() { val := sensor.Read() brightness := uint8(gobot.ToScale(gobot.FromScale(float64(val), 0, 1024), 0, 255)) fmt.Println("sensor", val) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("sensorBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{sensor, led}, work)) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "usr0") work := func() { gobot.Every(1*time.Second, func() { led.Toggle() }) } robot := gobot.NewRobot("blinkBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{led}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { var code string beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") //NewDirectPinDriver returns a pointer - this wasn't immediately obvious to me splate := gpio.NewDirectPinDriver(beagleboneAdaptor, "splate", "P9_11") c := &serial.Config{Name: "/dev/ttyUSB0", Baud: 9600} u, err := serial.OpenPort(c) if err != nil { fmt.Print(err) os.Exit(1) } //Configure ZMQ publisher publisher, err := zmq.NewSocket(zmq.PUB) if err != nil { fmt.Print(err) os.Exit(1) } publisher.Bind("tcp://*:5556") //Configure ZMQ publisher go http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Code: ")) w.Write([]byte(code)) }) // the anonymous function here allows us to call openDoor with splate remaining in scope go http.HandleFunc("/open", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Okay")) openDoor(*splate, publisher) }) go http.ListenAndServe(":8080", nil) buf := make([]byte, 16) for { n, err := io.ReadFull(u, buf) if err != nil { fmt.Print(err) os.Exit(1) } // We need to strip the stop and start bytes from the tag, so we only assign a certain range of the slice code = string(buf[1 : n-3]) var request bytes.Buffer request.WriteString("https://members.pumpingstationone.org/rfid/check/FrontDoor/") request.WriteString(code) resp, err := http.Get(request.String()) if err != nil { fmt.Printf("Whoops!") publisher.SendMessage("door.rfid.error", fmt.Sprintf("Auth Server Error: %s", err)) os.Exit(1) } if resp.StatusCode == 200 { fmt.Println("Success!") publisher.SendMessage("door.rfid.accept", "RFID Accepted") code = "" openDoor(*splate, publisher) } else if resp.StatusCode == 403 { fmt.Println("Membership status: Expired") publisher.SendMessage("door.rfid.deny", "RFID Denied") } else { fmt.Println("Code not found") publisher.SendMessage("door.rfid.deny", "RFID not found") } } }
func main() { var code string beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") //NewDirectPinDriver returns a pointer - this wasn't immediately obvious to me splate := gpio.NewDirectPinDriver(beagleboneAdaptor, "splate", "P9_11") c := &serial.Config{Name: "/dev/ttyUSB0", Baud: 9600} u, err := serial.OpenPort(c) if err != nil { fmt.Print(err) os.Exit(1) } //Configure ZMQ publisher publisher, err := zmq.NewSocket(zmq.PUB) if err != nil { fmt.Print(err) os.Exit(1) } publisher.Bind("tcp://*:5556") //Configure ZMQ publisher go http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Code: ")) w.Write([]byte(code)) }) // the anonymous function here allows us to call openDoor with splate remaining in scope go http.HandleFunc("/open", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Okay")) openDoor(*splate, publisher) }) go http.ListenAndServe(":8080", nil) buf := make([]byte, 16) for { n, err := io.ReadFull(u, buf) if err != nil { fmt.Print(err) os.Exit(1) } // We need to strip the stop and start bytes from the tag, so we only assign a certain range of the slice code = string(buf[1 : n-3]) // Now open the cache db to check if it's already here cacheDB, err = bolt.Open("rfid-tags.db", 0600, nil) if err != nil { fmt.Println(err) } cacheDB.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucketIfNotExists([]byte("RFIDBucket")) if err != nil { return fmt.Errorf("create bucket: %s", err) } return nil }) // Before checking the site for the code, let's check our cache if checkCacheDBForTag(code) == false { var request bytes.Buffer request.WriteString("https://members.pumpingstationone.org/rfid/check/FrontDoor/") request.WriteString(code) resp, err := http.Get(request.String()) if err != nil { fmt.Printf("Whoops!") publisher.SendMessage("door.rfid.error", fmt.Sprintf("Auth Server Error: %s", err)) os.Exit(1) } if resp.StatusCode == 200 { // We got 200 back, so we're good to add this // tag to the cache addTagToCacheDB(code) fmt.Println("Success!") publisher.SendMessage("door.rfid.accept", "RFID Accepted") code = "" openDoor(*splate, publisher) } else if resp.StatusCode == 403 { fmt.Println("Membership status: Expired") publisher.SendMessage("door.rfid.deny", "RFID Denied") } else { fmt.Println("Code not found") publisher.SendMessage("door.rfid.deny", "RFID not found") } } else { // If we're here, we found the tag in the cache, so // let's just go and open the door for 'em fmt.Println("Success!") publisher.SendMessage("door.rfid.accept", "RFID Accepted") code = "" openDoor(*splate, publisher) } cacheDB.Close() } }