2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-07-13 08:08:46 +08:00
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass serial port to use as the first param:
|
|
|
|
|
|
|
|
go run examples/firmata_temp36.go /dev/ttyACM0
|
|
|
|
*/
|
|
|
|
|
2016-04-02 05:24:43 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-21 02:50:42 +08:00
|
|
|
"fmt"
|
2017-07-13 08:08:46 +08:00
|
|
|
"os"
|
2016-04-02 05:24:43 +08:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/firmata"
|
2016-04-02 05:24:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-07-13 08:08:46 +08:00
|
|
|
firmataAdaptor := firmata.NewAdaptor(os.Args[1])
|
2016-04-02 05:24:43 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
val, err := firmataAdaptor.AnalogRead("0")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("sensorBot",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2016-04-02 05:24:43 +08:00
|
|
|
}
|