Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Last active September 20, 2017 21:43
Show Gist options
  • Save deadprogram/ec17881e3543f94e282384ce829bf40c to your computer and use it in GitHub Desktop.
Save deadprogram/ec17881e3543f94e282384ce829bf40c to your computer and use it in GitHub Desktop.
Simple Golang dashboard to show MQTT server activity by subscribing to all messages.
// how to run:
// go run mqtt.go tcp://iot.eclipse.org:1883 /topic/name
package main
import (
"os"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gizak/termui"
)
func main() {
var count float64
elapsed := 0
opts := mqtt.NewClientOptions().AddBroker(os.Args[1]).SetClientID("termui")
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
topic := "#"
if len(os.Args) > 2 {
topic = os.Args[2]
}
c.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
count++
})
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
data := make([]float64, 10)
lc0 := termui.NewLineChart()
lc0.BorderLabel = "MQTT Messages Per Second"
lc0.Data = data
lc0.Width = 60
lc0.Height = 30
lc0.X = 0
lc0.Y = 0
lc0.AxesColor = termui.ColorWhite
lc0.LineColor = termui.ColorGreen | termui.AttrBold
termui.Render(lc0)
termui.Handle("/sys/kbd/q", func(termui.Event) {
c.Disconnect(250)
termui.StopLoop()
})
termui.Handle("/timer/1s", func(e termui.Event) {
if elapsed >= 60 {
elapsed = 0
count = 0
data = nil
}
data = append(data, count)
elapsed++
count = 0
lc0.Data = data
termui.Render(lc0)
})
termui.Loop()
}
@deadprogram
Copy link
Author

mqttdash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment