Skip to content

Instantly share code, notes, and snippets.

@conoro
Created November 8, 2015 11:50
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 conoro/935e0fa3acc90d066b9a to your computer and use it in GitHub Desktop.
Save conoro/935e0fa3acc90d066b9a to your computer and use it in GitHub Desktop.
Control H801 Wifi RGB LED Controller from Go
package main
import (
"fmt"
. "github.com/hugozhu/rpi"
"net"
"time"
)
func CheckError(err error) {
if err != nil {
fmt.Println("Error: ", err)
}
}
func main() {
var state = "low"
buf := []byte{0xfb, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0x97, 0x9d, 0x00}
WiringPiSetup()
PinMode(PIN_GPIO_7, INPUT)
ServerAddr, err := net.ResolveUDPAddr("udp", "192.168.0.128:30977")
CheckError(err)
Conn, err := net.DialUDP("udp", nil, ServerAddr)
CheckError(err)
defer Conn.Close()
_, err = Conn.Write(buf)
if err != nil {
fmt.Println(err)
}
i := 0
for {
pin := DigitalRead(PIN_GPIO_7)
fmt.Println("GPIO7: ", pin)
i++
if pin == 1 {
if state == "low" {
buf = []byte{0xfb, 0xeb, 0xff, 0x00, 0x00, 0x00, 0x00, 0x9f, 0x97, 0x9d, 0x00}
state = "high"
_, err := Conn.Write(buf)
if err != nil {
fmt.Println(err)
}
fmt.Println("msg sent high", i)
}
} else {
if state == "high" {
buf = []byte{0xfb, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0x97, 0x9d, 0x00}
state = "low"
_, err := Conn.Write(buf)
if err != nil {
fmt.Println(err)
}
fmt.Println("msg sent low", i)
}
}
time.Sleep(time.Second * 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment