From 17340399e5d031b8820fa4d3e40d9a799c7e13cf Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 14 Jan 2023 19:29:54 +0000 Subject: [PATCH] attempt at receiving message for board --- main.go | 43 +++++++++++++++++++++++++++++++------------ mqtt.go | 44 -------------------------------------------- 2 files changed, 31 insertions(+), 56 deletions(-) delete mode 100644 mqtt.go diff --git a/main.go b/main.go index 68683bc..04fb585 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix" "github.com/disintegration/imaging" MQTT "github.com/eclipse/paho.mqtt.golang" + mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/fogleman/gg" ) @@ -25,6 +26,7 @@ type Animation struct { stroke int image []image.Image updown int + msg chan mqtt.Message } var ( @@ -66,26 +68,38 @@ func main() { }*/ m, err := rgbmatrix.NewRGBLedMatrix(config) fatal(err) - + mqMessages := make(chan mqtt.Message) + go listener(mqMessages) tk := rgbmatrix.NewToolKit(m) defer tk.Close() + go animator(tk, mqMessages) +} - tk.PlayAnimation(NewAnimation(image.Point{64, 32})) +func listener(mqMessages chan mqtt.Message) { + opts := setupMQTT() + client := MQTT.NewClient(opts) + topic := "home/rgbboard" + if token := client.Connect(); token.Wait() && token.Error() != nil { + panic(token.Error()) + } + client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) { + log.Println("Receiving ", string(msg.Payload()), " on topic: ", msg.Topic()) + mqMessages <- msg + }) } -func setupMQTT() { +func animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) { + + tk.PlayAnimation(NewAnimation(image.Point{64, 32}, mqMessages)) + +} +func setupMQTT() *mqtt.ClientOptions { 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()) - } + return opts } func init() { flag.Parse() @@ -97,7 +111,7 @@ func fatal(err error) { } } -func NewAnimation(sz image.Point) *Animation { +func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation { reader, err := os.Open("marioUp.png") if err != nil { log.Fatal(err) @@ -123,6 +137,7 @@ func NewAnimation(sz image.Point) *Animation { stroke: 8, image: images, updown: 0, + msg: mqMessages, } } @@ -136,7 +151,11 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) { a.ctx.DrawImageAnchored(imaging.FlipH(a.image[a.updown]), a.position.X, a.position.Y, 0.5, 0.5) } a.ctx.SetColor(color.White) - a.ctx.DrawString("mario", 5, 9) + select { + case msg := <-a.msg: + a.ctx.DrawString(string(msg.Payload()), 5, 9) + default: + } return a.ctx.Image(), time.After(time.Millisecond * 50), nil } diff --git a/mqtt.go b/mqtt.go deleted file mode 100644 index 2208d6b..0000000 --- a/mqtt.go +++ /dev/null @@ -1,44 +0,0 @@ -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) - -}