67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
const broker = "tcp://mqtt.seeseepuff.be:1883"
|
|
const telemetryTopic = "spider/telemetry/"
|
|
|
|
func publishServoBatteryTelemetry(client mqtt.Client) {
|
|
value, err := GetBatteryServo()
|
|
if err != nil {
|
|
return
|
|
}
|
|
token := client.Publish(telemetryTopic+"servo_battery", 0, false, fmt.Sprintf("%f", value))
|
|
token.Wait()
|
|
if token.Error() != nil {
|
|
log.Printf("Error publishing servo battery state: %v\n", token.Error())
|
|
}
|
|
}
|
|
|
|
func publishCpuBatteryTelemetry(client mqtt.Client) {
|
|
value, err := GetBatteryCPU()
|
|
if err != nil {
|
|
return
|
|
}
|
|
token := client.Publish(telemetryTopic+"cpu_battery", 0, false, fmt.Sprintf("%f", value))
|
|
token.Wait()
|
|
if token.Error() != nil {
|
|
log.Printf("Error publishing servo battery state: %v\n", token.Error())
|
|
}
|
|
}
|
|
|
|
func publishTelemetry(client mqtt.Client) {
|
|
go publishServoBatteryTelemetry(client)
|
|
go publishCpuBatteryTelemetry(client)
|
|
}
|
|
|
|
func main() {
|
|
opts := mqtt.NewClientOptions()
|
|
opts.AddBroker(broker)
|
|
opts.SetClientID("spider-host-client")
|
|
client := mqtt.NewClient(opts)
|
|
|
|
token := client.Connect()
|
|
token.Wait()
|
|
if token.Error() != nil {
|
|
log.Fatalf("Error connecting to MQTT broker: %v\n", token.Error())
|
|
}
|
|
|
|
InitBattery()
|
|
|
|
ticker := time.NewTicker(3 * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
publishTelemetry(client)
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
publishTelemetry(client)
|
|
}
|
|
}
|
|
}
|