我要用Raspberry pi2用gobot来控制GPIO(LED闪烁)

我想用树莓派2尝试电子制作。首先,我想从点亮LED开始,挑战一下基本的部分。此外,我也关注了一个名为Gobot的框架,它可以在Golang中进行硬件控制。

从Raspberry Pi2的GPIO控制进行操作确认开始。

我会确认Raspberry Pi 2上LED的基本操作。我参考了以下文章:https://tool-lab.com/2013/12/raspi-gpio-controlling-breadboard-2/

关于与LED的连接,我已将其连接到物理引脚号为12(“GPIO 18”引脚)。
我将点亮连接到GPIO18引脚的LED并实际测试。

pi@raspberrypi:~ $ sudo echo 18 > /sys/class/gpio/export
pi@raspberrypi:~ $ sudo echo out > /sys/class/gpio/gpio18/direction
pi@raspberrypi:~ $ sudo echo 1 > /sys/class/gpio/gpio18/value
IMG_0094.JPG

由于灯泡成功点亮,我会关掉LED灯,并将其恢复到原始状态。

pi@raspberrypi:~ $ sudo echo 0 > /sys/class/gpio/gpio18/value
pi@raspberrypi:~ $ sudo echo 18 > /sys/class/gpio/unexport

◾️ 准备Golang环境

似乎需要go 1.4或更高版本的golang环境才能运行gobot。

树莓派 Wheezy 用户特别注意:从默认软件仓库安装的 go 版本太旧,无法编译 gobot。您可以按以下步骤安装 go 1.4。

顺便提一下,在使用apt-get install命令安装golang时,会安装一个旧版本。

pi@raspberrypi:~/golang $ apt show golang
Package: golang
Version: 2:1.3.3-1
Installed-Size: 91.1 kB
Maintainer: Go Compiler Team <pkg-golang-devel@lists.alioth.debian.org>
Replaces: golang-weekly
Provides: golang-weekly
Depends: golang-go (>= 2:1.3.3-1), golang-src (>= 2:1.3.3-1), golang-doc (>= 2:1.3.3-1)
Conflicts: golang-weekly
Download-Size: 25.0 kB
Homepage: http://golang.org/
Section: devel
Priority: optional
APT-Sources: http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages
Description: Go programming language compiler - metapackage
 The Go programming language is an open source project to make
 programmers more productive. Go is expressive, concise, clean, and
 efficient. Its concurrency mechanisms make it easy to write programs
 that get the most out of multicore and networked machines, while its
 novel type system enables flexible and modular program construction.
 Go compiles quickly to machine code yet has the convenience of
 garbage collection and the power of run-time reflection. It's a
 fast, statically typed, compiled language that feels like a
 dynamically typed, interpreted language.
 .
 This package is a metapackage that, when installed, guarantees
 that (most of) a full Go development environment is installed.

因此,我们将按照gobot的官方网站上所述的步骤进行环境设置。

pi@raspberrypi:~ $ wget -O - http://dave.cheney.net/paste/go1.4.linux-arm~multiarch-armv6-1.tar.gz|sudo tar -xzC /usr/local -f -
pi@raspberrypi:~ $ echo '# Setup for golang' |sudo tee /etc/profile.d/golang.sh
pi@raspberrypi:~ $ echo 'PATH=$PATH:/usr/local/go/bin'|sudo tee -a /etc/profile.d/golang.sh

运行gobot需要root权限。
因此,接下来将在root用户下进行操作。

pi@raspberrypi:~ $ sudo -i
root@raspberrypi:~#

请在.profile文件中添加以下3行内容。

export GOBIN=$HOME/golang/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin

我会先创建一个工作目录。

root@raspberrypi:~# mkdir golang
root@raspberrypi:~# source .profile
root@raspberrypi:~# mkdir $GOPATH/bin

我会检查go环境的版本。

root@raspberrypi:~# go version
go version go1.4 linux/arm

另外,我们试试运行一下go的示例代码。

package main

import "fmt"

func main() {
    fmt.Println("Hello world!")
}

由于出现了”Hello world!”的输出,可以推断出Golang正常运行。

root@raspberrypi:~# go run hello.go 
Hello world!

通过gobot使LED发光。

终于到了,是时候准备gobot的环境了。

root@raspberrypi:~# go get -d -u github.com/hybridgroup/gobot/... && go install github.com/hybridgroup/gobot/platforms/raspi

这是一个实际的示例代码,用于点亮LED。
关于LED的连接,我们已将其连接到物理引脚12(“GPIO 18″引脚)。因此,我们进行了一些修改。

package main

import (
    "time"

    "github.com/hybridgroup/gobot"
    "github.com/hybridgroup/gobot/platforms/gpio"
    "github.com/hybridgroup/gobot/platforms/raspi"
)

func main() {
    gbot := gobot.NewGobot()

    r := raspi.NewRaspiAdaptor("raspi")
    led := gpio.NewLedDriver(r, "led", "12")

    work := func() {
        gobot.Every(1*time.Second, func() {
            led.Toggle()
        })
    }

    robot := gobot.NewRobot("blinkBot",
        []gobot.Connection{r},
        []gobot.Device{led},
        work,
    )
    gbot.AddRobot(robot)
    gbot.Start()
}

那么,让我们使用Gobot来执行示例代码,试着点亮LED吧。

root@raspberrypi:~# go run raspi_blink.go 
2016/01/03 09:40:12 Initializing Robot blinkBot ...
2016/01/03 09:40:12 Initializing connections...
2016/01/03 09:40:12 Initializing connection raspi ...
2016/01/03 09:40:12 Initializing devices...
2016/01/03 09:40:12 Initializing device led ...
2016/01/03 09:40:12 Starting Robot blinkBot ...
2016/01/03 09:40:12 Starting connections...
2016/01/03 09:40:12 Starting connection raspi...
2016/01/03 09:40:12 Starting devices...
2016/01/03 09:40:12 Starting device led on pin 12...
2016/01/03 09:40:12 Starting work...
IMG_0098.JPG

LED 原本是闪烁而不是常亮的,但最后结果还算是可以接受。
既然能够确认到 LED 的闪烁,那就最后进行编译吧。

root@raspberrypi:~# go install raspi_blink.go

下面将选择启动已构建的二进制文件作为后续步骤。

root@raspberrypi:~# raspi_blink 
2016/01/03 14:09:54 Initializing Robot blinkBot ...
2016/01/03 14:09:54 Initializing connections...
2016/01/03 14:09:54 Initializing connection raspi ...
2016/01/03 14:09:54 Initializing devices...
2016/01/03 14:09:54 Initializing device led ...
2016/01/03 14:09:54 Starting Robot blinkBot ...
2016/01/03 14:09:54 Starting connections...
2016/01/03 14:09:54 Starting connection raspi...
2016/01/03 14:09:54 Starting devices...
2016/01/03 14:09:54 Starting device led on pin 12...
2016/01/03 14:09:54 Starting work...

我认为gobot是物联网工程领域中很有前途的框架。我希望以后能尝试让各种不同的物联网设备运行起来。

◾️ 参考来源

    • Raspberry PiのGPIO制御方法を確認する

 

    RaspberryPi×Go言語で電子工作