func main() { flag.Parse() // Initialize Intel Edison edisonAdaptor := edison.NewEdisonAdaptor("edison") edisonAdaptor.Connect() lightSensor := gpio.NewGroveLightSensorDriver(edisonAdaptor, "light", *sensorLightPin, *sensorPollingInterval) lightSensor.Start() gobot.On(lightSensor.Event("data"), func(data interface{}) { raw := float64(data.(int)) // convert to lux resistance := (1023.0 - raw) * 10.0 / raw * 15.0 light = 10000.0 / math.Pow(resistance, 4.0/3.0) lightUpdated = time.Now() log.Debugln("illuminance: ", light) }) soundSensor := gpio.NewGroveSoundSensorDriver(edisonAdaptor, "sound", *sensorSoundPin, *sensorPollingInterval) soundSensor.Start() gobot.On(soundSensor.Event("data"), func(data interface{}) { sound = float64(data.(int)) soundUpdated = time.Now() log.Debugln("sound level: ", sound) }) tempSensor := gpio.NewGroveTemperatureSensorDriver(edisonAdaptor, "temp", *sensorTempPin, *sensorPollingInterval) tempSensor.Start() gobot.On(tempSensor.Event("data"), func(data interface{}) { celsius = data.(float64) fahrenheit = celsius*1.8 + 32 tempUpdated = time.Now() log.Debugln("temperature: ", celsius) }) // Initialize prometheus exporter exporter := NewExporter() prometheus.MustRegister(exporter) log.Infof("Listening on: %s", *listenAddress) http.Handle(*metricPath, prometheus.Handler()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` <html> <head> <title>IoT Edison exporter</title> </head> <body> <h1>Prometheus exporter for sensor metrics from Intel Edison</h1> <p><a href='` + *metricPath + `'>Metrics</a></p> </body> </html> `)) }) log.Fatal(http.ListenAndServe(*listenAddress, nil)) }
/* This function is used if only the rotary port is in play. func mapValueToColor(i int) (int, int, int) { // Masking to account for very sensitive rotary red := ((i & (1 << 1)) << 2) | ((i & (1 << 4)) << 0) | ((i & (1 << 7)) >> 2) green := ((i & (1 << 2)) << 1) | ((i & (1 << 5)) >> 1) | ((i & (1 << 8)) >> 3) blue := ((i & (1 << 3)) << 0) | ((i & (1 << 6)) >> 2) | ((i & (1 << 9)) >> 4) return red, green, blue } */ func main() { gbot := gobot.NewGobot() board := edison.NewEdisonAdaptor("edison") screen := i2c.NewGroveLcdDriver(board, "screen") sensorRotary := gpio.NewGroveRotaryDriver(board, "sensor", "0") sensorLight := gpio.NewGroveLightSensorDriver(board, "light", "1") sensorTemp := gpio.NewGroveTemperatureSensorDriver(board, "temp", "2") work := func() { var r, g, b int gobot.On(sensorRotary.Event("data"), func(data interface{}) { r = data.(int) >> 2 }) gobot.On(sensorLight.Event("data"), func(data interface{}) { fmt.Printf("%d\n", data) g = data.(int) * 255 / 790 }) gobot.Every(time.Millisecond*500, func() { b = int(gobot.ToScale(gobot.FromScale(sensorTemp.Temperature(), 25, 35), 0, 255)) screen.Clear() screen.Home() screen.SetRGB(r, g, b) screen.Write(fmt.Sprintf("#%02X%02X%02X", r, g, b)) }) } robot := gobot.NewRobot("screenBot", []gobot.Connection{board}, []gobot.Device{screen, sensorRotary, sensorLight, sensorTemp}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() board := edison.NewEdisonAdaptor("board") sensor := gpio.NewGroveLightSensorDriver(board, "sensor", "0") work := func() { gobot.On(sensor.Event("data"), func(data interface{}) { fmt.Println("sensor", data) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{board}, []gobot.Device{sensor}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() a := api.NewAPI(gbot) a.Start() board := firmata.NewFirmataAdaptor("arduino", os.Args[1]) // digital devices button = gpio.NewGroveButtonDriver(board, "button", "2") blue = gpio.NewGroveLedDriver(board, "blue", "3") green = gpio.NewGroveLedDriver(board, "green", "4") red = gpio.NewGroveLedDriver(board, "red", "5") buzzer = gpio.NewGroveBuzzerDriver(board, "buzzer", "7") touch = gpio.NewGroveTouchDriver(board, "touch", "8") // analog devices rotary = gpio.NewGroveRotaryDriver(board, "rotary", "0") sensor = gpio.NewGroveTemperatureSensorDriver(board, "sensor", "1") sound = gpio.NewGroveSoundSensorDriver(board, "sound", "2") light = gpio.NewGroveLightSensorDriver(board, "light", "3") work := func() { Reset() gobot.On(button.Event(gpio.Push), func(data interface{}) { TurnOff() fmt.Println("On!") blue.On() }) gobot.On(button.Event(gpio.Release), func(data interface{}) { Reset() }) gobot.On(touch.Event(gpio.Push), func(data interface{}) { Doorbell() }) gobot.On(rotary.Event("data"), func(data interface{}) { fmt.Println("rotary", data) }) gobot.On(sound.Event("data"), func(data interface{}) { DetectSound(data.(int)) }) gobot.On(light.Event("data"), func(data interface{}) { DetectLight(data.(int)) }) gobot.Every(1*time.Second, func() { CheckFireAlarm() }) } robot := gobot.NewRobot("airlock", []gobot.Connection{board}, []gobot.Device{button, blue, green, red, buzzer, touch, rotary, sensor, sound, light}, work, ) gbot.AddRobot(robot) gbot.Start() }