From c7f4f1724205017e4b757902e03f5ff0c340bef8 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sun, 8 Jan 2023 22:56:24 +0000 Subject: [PATCH] adding MQ --- go.mod | 6 +++++- go.sum | 8 ++++++++ main.go | 43 ++++++++++++++++++++++++++++--------------- mqtt.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 mqtt.go diff --git a/go.mod b/go.mod index a16030c..5539f24 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v0.0.0-20220709235716-bf87ebdb3e55 github.com/disintegration/imaging v1.6.2 + github.com/eclipse/paho.mqtt.golang v1.4.2 github.com/fogleman/gg v1.3.0 ) @@ -12,12 +13,15 @@ require ( dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037 // indirect github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + github.com/gorilla/websocket v1.4.2 // indirect github.com/jezek/xgb v1.0.0 // indirect golang.org/x/exp/shiny v0.0.0-20220713135740-79cabaa25d75 // indirect golang.org/x/image v0.0.0-20220617043117-41969df76e82 // indirect golang.org/x/mobile v0.0.0-20220518205345-8578da9835fd // indirect + golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) -replace gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v0.0.0-20220709235716-bf87ebdb3e55 => /usr/src/app +//replace gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v0.0.0-20220709235716-bf87ebdb3e55 => /usr/src/app diff --git a/go.sum b/go.sum index 4017f09..183ed6d 100644 --- a/go.sum +++ b/go.sum @@ -5,12 +5,16 @@ gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v0.0.0-202207092 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4= +github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA= github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 h1:WtGNWLvXpe6ZudgnXrq0barxBImvnnJoMEhXAzcbM0I= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jezek/xgb v1.0.0 h1:s2rRzAV8KQRlpsYA7Uyxoidv1nodMF0m6dIG6FhhVLQ= github.com/jezek/xgb v1.0.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= @@ -38,11 +42,15 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index f0f260f..68683bc 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "image" "image/color" _ "image/jpeg" @@ -11,9 +12,21 @@ import ( rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix" "github.com/disintegration/imaging" + MQTT "github.com/eclipse/paho.mqtt.golang" "github.com/fogleman/gg" ) +type Animation struct { + ctx *gg.Context + position image.Point + dir image.Point + height int + width int + stroke int + image []image.Image + updown int +} + var ( rows = flag.Int("led-rows", 32, "number of rows supported") cols = flag.Int("led-cols", 32, "number of columns supported") @@ -37,7 +50,7 @@ func main() { config.ShowRefreshRate = *show_refresh config.InverseColors = *inverse_colors config.DisableHardwarePulsing = *disable_hardware_pulsing - + setupMQTT() /*bounds := mario.Bounds() var histogram [16][4]int for y := bounds.Min.Y; y < bounds.Max.Y; y++ { @@ -60,6 +73,20 @@ func main() { tk.PlayAnimation(NewAnimation(image.Point{64, 32})) } +func setupMQTT() { + opts := MQTT.NewClientOptions() + opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT"))) + opts.SetUsername(os.Getenv("MQTTUSER")) + opts.SetPassword(os.Getenv("MQTTPASS")) + opts.SetDefaultPublishHandler(messagePubHandler) + opts.OnConnect = connectHandler + opts.OnConnectionLost = connectLostHandler + client := MQTT.NewClient(opts) + if token := client.Connect(); token.Wait() && token.Error() != nil { + panic(token.Error()) + } + +} func init() { flag.Parse() } @@ -70,17 +97,6 @@ func fatal(err error) { } } -type Animation struct { - ctx *gg.Context - position image.Point - dir image.Point - height int - width int - stroke int - image []image.Image - updown int -} - func NewAnimation(sz image.Point) *Animation { reader, err := os.Open("marioUp.png") if err != nil { @@ -114,9 +130,6 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) { defer a.updatePosition() a.ctx.SetColor(color.Black) a.ctx.Clear() - //a.ctx.SetColor(color.RGBA{0, 255, 0, 255}) - //a.ctx.DrawCircle(float64(8), float64(8), float64(a.stroke)) - //a.ctx.Fill() if a.dir.X == 1 { a.ctx.DrawImageAnchored(a.image[a.updown], a.position.X, a.position.Y, 0.5, 0.5) } else { diff --git a/mqtt.go b/mqtt.go new file mode 100644 index 0000000..2208d6b --- /dev/null +++ b/mqtt.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + + MQTT "github.com/eclipse/paho.mqtt.golang" + mqtt "github.com/eclipse/paho.mqtt.golang" +) + +func publish(client MQTT.Client, bps float32) { + + topic := "WirelessBandwidth/state" + //Payload := "{\"Mbps\":\"" + fmt.Sprintf("%f", bps) + "\"}" + token := client.Publish(topic, 1, false, Payload) + token.Wait() + //fmt.Println(fmt.Sprintf("%f",bps)) +} + +func subscribe(client MQTT.Client, bps float32) { + + topic := "home/rgbboard" + //Payload := "{\"Mbps\":\"" + fmt.Sprintf("%f", bps) + "\"}" + sub := client.Subscribe() + token := client.Publish(topic, 1, false, Payload) + token.Wait() + //fmt.Println(fmt.Sprintf("%f",bps)) +} + +var messagePubHandler MQTT.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { + fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic()) +} + +var connectHandler MQTT.OnConnectHandler = func(client mqtt.Client) { + fmt.Println("Connected") + //topic := "homeassistant/sensor/WirelessBandwidth/config" + //Payload := "{\"name\":\"WirelessBandwidth\",\"state_class\":\"measurement\",\"unit_of_measurement\":\"Mbps\", \"device_class\":\"monetary\", \"state_topic\":\"WirelessBandwidth/state\",\"unique_id\":\"WirelessBandwidth\",\"value_template\":\"{{ value_json.Mbps }}\"}" + //token := client.Publish(topic, 1, false, Payload) + //token.Wait() +} + +var connectLostHandler MQTT.ConnectionLostHandler = func(client mqtt.Client, err error) { + fmt.Printf("Connect lost: %v", err) + +}