Skip to content

Instantly share code, notes, and snippets.

@logicalguess
Last active November 5, 2017 19:19
Show Gist options
  • Save logicalguess/4f4391c9eae9d2528cd3313ce26809fd to your computer and use it in GitHub Desktop.
Save logicalguess/4f4391c9eae9d2528cd3313ce26809fd to your computer and use it in GitHub Desktop.
Gobot that blinks an LED and reads the voltage of a pin on an Arduino microcontroller connected to a Raspberry Pi. The code is built for the Raspberry Pi and transferred to it.
package main
import (
"fmt"
"gobot.io/x/gobot"
"gobot.io/x/gobot/api"
"gobot.io/x/gobot/platforms/firmata"
"gobot.io/x/gobot/drivers/gpio"
"time"
"os"
)
// env GOOS=linux GOARCH=arm GOARM=5 go build
// scp combo pi@10.0.1.7:/home/pi/Arduino
func main() {
master := gobot.NewMaster()
api.NewAPI(master).Start()
// /dev/tty.usbmodem1411" on my Mac (used in development)
// /dev/ttyACM0 on Raspberry Pi
f := firmata.NewAdaptor(os.Args[1])
cmd := func(params map[string]interface{}) interface{} {
val, err := f.AnalogRead("1")
if err != nil {
fmt.Println(err)
panic(err)
}
voltage := (float64(val) * 5) / 1024 // if using 3.3V replace 5 with 3.3
tempC := (voltage - 0.5) * 100
tempF := (tempC * 9 / 5) + 32
fmt.Printf("%.2f°C\n", tempC)
fmt.Printf("%.2f°F\n", tempF)
return fmt.Sprintf("%.2f°F\n", tempF)
}
led := gpio.NewLedDriver(f, "13")
work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
})
}
blinker := gobot.NewRobot("blinker",
[]gobot.Connection{f},
[]gobot.Device{led},
work,
)
blinker.AddCommand("Read Temperature", cmd)
master.AddRobot(blinker)
master.Start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment