Skip to content

Instantly share code, notes, and snippets.

@kidoman
Created November 20, 2013 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidoman/7557729 to your computer and use it in GitHub Desktop.
Save kidoman/7557729 to your computer and use it in GitHub Desktop.
Test Go on RPi
package main
import (
rpio "github.com/stianeikeland/go-rpio"
"log"
"net/http"
)
func main() {
log.Print("Starting up...")
err := rpio.Open()
if err != nil {
panic(err)
}
defer func() {
log.Print("Cleaning up...")
rpio.Close()
}()
pin := rpio.Pin(10)
pin.Output()
defer func() {
log.Print("Setting pin 10 to low")
pin.Low()
}()
http.HandleFunc("/what", func(resp http.ResponseWriter, req *http.Request) {
defer pin.Output()
pin.Input()
state := pin.Read()
if state == rpio.Low {
resp.Write([]byte("Low"))
} else {
resp.Write([]byte("High"))
}
})
http.HandleFunc("/on", func(resp http.ResponseWriter, req *http.Request) {
pin.High()
})
http.HandleFunc("/off", func(resp http.ResponseWriter, req *http.Request) {
pin.Low()
})
quit := make(chan bool)
http.HandleFunc("/shutdown", func(resp http.ResponseWriter, req *http.Request) {
quit <- true
})
go http.ListenAndServe(":8080", nil)
<-quit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment