attempt at receiving message for board
All checks were successful
rgb/pipeline/head This commit looks good
All checks were successful
rgb/pipeline/head This commit looks good
This commit is contained in:
45
main.go
45
main.go
@@ -13,6 +13,7 @@ import (
|
|||||||
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
|
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
|
||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/fogleman/gg"
|
"github.com/fogleman/gg"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,6 +26,7 @@ type Animation struct {
|
|||||||
stroke int
|
stroke int
|
||||||
image []image.Image
|
image []image.Image
|
||||||
updown int
|
updown int
|
||||||
|
msg chan mqtt.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -66,26 +68,38 @@ func main() {
|
|||||||
}*/
|
}*/
|
||||||
m, err := rgbmatrix.NewRGBLedMatrix(config)
|
m, err := rgbmatrix.NewRGBLedMatrix(config)
|
||||||
fatal(err)
|
fatal(err)
|
||||||
|
mqMessages := make(chan mqtt.Message)
|
||||||
|
go listener(mqMessages)
|
||||||
tk := rgbmatrix.NewToolKit(m)
|
tk := rgbmatrix.NewToolKit(m)
|
||||||
defer tk.Close()
|
defer tk.Close()
|
||||||
|
go animator(tk, mqMessages)
|
||||||
tk.PlayAnimation(NewAnimation(image.Point{64, 32}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupMQTT() {
|
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 animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) {
|
||||||
|
|
||||||
|
tk.PlayAnimation(NewAnimation(image.Point{64, 32}, mqMessages))
|
||||||
|
|
||||||
|
}
|
||||||
|
func setupMQTT() *mqtt.ClientOptions {
|
||||||
opts := MQTT.NewClientOptions()
|
opts := MQTT.NewClientOptions()
|
||||||
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT")))
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT")))
|
||||||
opts.SetUsername(os.Getenv("MQTTUSER"))
|
opts.SetUsername(os.Getenv("MQTTUSER"))
|
||||||
opts.SetPassword(os.Getenv("MQTTPASS"))
|
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() {
|
func init() {
|
||||||
flag.Parse()
|
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")
|
reader, err := os.Open("marioUp.png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -123,6 +137,7 @@ func NewAnimation(sz image.Point) *Animation {
|
|||||||
stroke: 8,
|
stroke: 8,
|
||||||
image: images,
|
image: images,
|
||||||
updown: 0,
|
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.DrawImageAnchored(imaging.FlipH(a.image[a.updown]), a.position.X, a.position.Y, 0.5, 0.5)
|
||||||
}
|
}
|
||||||
a.ctx.SetColor(color.White)
|
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
|
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
44
mqtt.go
44
mqtt.go
@@ -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)
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user