Skip to content

Commit

Permalink
Fixed mqtt connection
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzen committed Apr 5, 2023
1 parent 94f0384 commit def93b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pwsmqttdispatcher/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config
name: PWS to MQTT dispatcher addon
version: "0.1.15"
version: "0.1.16"
slug: pwsmqttdispatcher
description: Push weather station (PWS) data to MQTT
url: "https://github.com/peterzen/haas-pws-mqtt-addon/tree/main/pwsmqttdispatcher"
Expand Down
22 changes: 8 additions & 14 deletions pwsmqttdispatcher/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ func parseHtml(doc *goquery.Document) []byte {
return jsonData
}

func newMqttClient() mqtt.Client {
func main() {

debugEnabled = false

if os.Getenv("DEBUG_ENABLED") == "true" {
debugEnabled = true
}

pwsIp = os.Getenv("PWS_IP")
if pwsIp == "" {
Expand Down Expand Up @@ -194,18 +200,6 @@ func newMqttClient() mqtt.Client {
defer client.Disconnect(250)

log.Printf("Connected to MQTT broker %s\n", opts.Servers[0])
return client
}

func main() {

debugEnabled = false

if os.Getenv("DEBUG_ENABLED") == "true" {
debugEnabled = true
}

client := newMqttClient()

mqttTopic := os.Getenv(("MQTT_TOPIC"))
if mqttTopic == "" {
Expand All @@ -221,7 +215,7 @@ func main() {
if weatherData != nil {
token := client.Publish(mqttTopic, 0, false, weatherData)
if debugEnabled {
log.Printf("Published data to MQTT topic\n")
log.Printf("Published data to #%s\n", mqttTopic)
}
token.Wait()
if debugEnabled {
Expand Down

0 comments on commit def93b8

Please sign in to comment.