Create a gist now

Instantly share code, notes, and snippets.

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