Linux Format

GPIO with Go

Les Pounder introduces us to the basics of Go – another language that can be used to make projects on the Raspberry Pi.

-

Les Pounder introduces us to the basics of Go – another language that can be used to make projects on the Raspberry Pi.

Controllin­g the General Purpose Input Output (GPIO) pins of the Raspberry Pi is usually done using languages such as Python, but in this tutorial we look at Go,an open source project developed by a team of Google and community enthusiast­s. The language is easy to understand if you’ve already learnt Python or Arduino. Our project is a simple tool to check the status of a server and report the status using LEDS.

Hardware setup<

Please refer to the circuit diagram in the download for this issue for wiring guidance.

Software setup

To install Go, visit https://go.org/dl and download the latest version for ARMV6 (go1.14). Next, open a terminal and navigate to the download directory. Extract the contents of the download into the /usr/local/ directory, creating a new directory called go. Replace

with the name of your file.

$ sudo tar -C /usr/local -xzf

Next we tell the system where to find the files necessary to build and run our code and to do this we need to make a change to a file called .profile, hidden in our home directory. Open the file using your favourite text editor (we used geany). The file is hidden so in the open file dialog press Ctrl+h to show hidden files.

At the bottom of the file add the following and save.

export Path=$path:/usr/local/go/bin

Reboot the Raspberry Pi, then open a terminal and install the GPIO package for Go using Go’s package manager to download and install.

$ go get github.com/stianeikel­and/go-rpio/

Close the terminal and open your text editor. Save your file as server-test.go. Our first line creates a package, main , which runs when the code is executed.

package main

Now we import the packages of code that will enable our project to print to the screen ( fmt ), create delays ( time ), interact with Raspbian ( os ), control the GPIO ( go-rpio ) and basic network connectivi­ty ( net/http ).

import (

“fmt”

“time”

“os” “github.com/stianeikel­and/go-rpio” “net/http” )

To control the two LEDS we create two variables that will refer to the GPIO pin for each LED: var ( red = rpio.pin(17) green = rpio.pin(27) )

The main function is called when the code is executed. The first line of code inside the function is to open a connection to the GPIO and this uses a test to check if the connection was made. If there’s an error, then the informatio­n is saved to a variable, err , which is then printed to the screen before the code exits. func main() { if err := rpio.open(); err != nil { fmt.println(err) os.exit(1)

}

Now, outside of the if condition, is a deferred closure of the GPIO connection that unmaps the memory used for the connection, making it available for the system.

defer rpio.close()

To check that our LEDS can be controlled via the GPIO, the next few lines will set the GPIO pins as outputs, then turn on the red and green LEDS for two seconds and then turn off the LEDS:

red.output() green.output() red.high() green.high() time.sleep(2 * time.second) red.low() green.low()

Next, the For loop has no range, so it’ll continue indefinite­ly, similar to a while True loop in Python. The first line of the loop creates two variables: resp which contains the response, and err which contains errors. The contents of these variables are created when we use http.get to make a request to the server.

for {

resp, err := http.get(“http://bigl.es/”)

Still inside the loop we now create a conditiona­l statement that’ll handle the action to take based upon a response code. When our request is sent to the server it returns data stored in resp which is the response to our request. Part of this response is an HTTP status code, which provides a quick reference for issues. The ideal code is 200 (Ok), which signifies a connection was made correctly. If that is the case then the green LED should turn on and the red LED turn off. We also print that the server is up, then wait for 10 seconds.

if resp.statuscode == 200 { green.high() red.low() fmt.println(“server Up\n”) time.sleep(10 * time.second)

But what happens if the server goes down? Then our else condition is activated and it will turn off the green LED, turn on the red LED and then print that the server is down. It waits 10 seconds before the loop repeats.

} else { green.low() red.high() fmt.println(“server Down\n”) time.sleep(10 * time.second) }

The last line of code prints the HTTP status code and errors to the terminal. Then the for loop and the main function are closed using closing brackets.

fmt.println(resp.statuscode,err) } }

With the code complete, save the file and open a terminal, navigate to the directory where your code has been saved. To test that the code works, in the terminal type the following to run the code.

$ go run server-test.go

The LEDS will light up, turn off, then an LED will light up based on if the server is up. With a successful test we can now build the project into an executable file. In the terminal type the following.

$ go build server-test.go

With the executable built we can now run the code from the terminal.

./server-test

So there we have it, a simple introducti­on to Go, a language that is both easy to use and understand.

 ??  ?? We can build our Go code in Geany. Click Build>build and then click Build>run to start the code.
We can build our Go code in Geany. Click Build>build and then click Build>run to start the code.
 ??  ?? Geany is a jack-of-all-trades code editor that runs well even on older Raspberry Pi. It has many plugins and templates for popular languages.
Geany is a jack-of-all-trades code editor that runs well even on older Raspberry Pi. It has many plugins and templates for popular languages.
 ??  ?? The circuit is just two LEDS connected to the GPIO via a breadboard. The LEDS tell us if our server’s up or down.
The circuit is just two LEDS connected to the GPIO via a breadboard. The LEDS tell us if our server’s up or down.

Newspapers in English

Newspapers from Australia