Skip to content

Commit

Permalink
Added TLS support to the mqtt-2-influx (part of #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyLandman committed Dec 12, 2020
1 parent 1df3668 commit e22c11e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions server/mqtt-2-influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"bytes"
"crypto/tls"
"fmt"
"log"
"net/http"
Expand All @@ -43,8 +44,9 @@ func getMqttTopics() []string {
var (
clientData map[string]Client
mqttURL string = "mqtt:1883"
mqttTLS bool = false
influxURL string = "influxdb:8086"
influxWriteStatement string = "http://" + influxURL + "/write?db=" + influxDatabase
influxWriteStatement string = "calculated-later"
)

type sensorData struct {
Expand All @@ -66,6 +68,7 @@ type Client struct {

type Server struct {
MqttAddress string `toml:"mqttAddress"`
MqttTLS bool `toml:"mqttTLS"`
InfluxAddress string `toml:"influxAddress"`
}

Expand All @@ -87,6 +90,7 @@ func loadConfig() error {
if len(config.Server.MqttAddress) > 0 {
mqttURL = config.Server.MqttAddress
}
mqttTLS = config.Server.MqttTLS
if len(config.Server.InfluxAddress) > 0 {
influxURL = config.Server.InfluxAddress
}
Expand Down Expand Up @@ -124,14 +128,20 @@ func createKeyValuePairs(m map[string]interface{}) string {
}

func createClient() (libmqtt.Client, error) {

var tlsConfig *tls.Config
if mqttTLS {
tlsConfig = &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}
} else {
tlsConfig = nil
}
return libmqtt.NewClient(
// enable keepalive (10s interval) with 20% tolerance
libmqtt.WithKeepalive(10, 1.2),
// enable auto reconnect and set backoff strategy
libmqtt.WithAutoReconnect(true),
libmqtt.WithBackoffStrategy(time.Second, 5*time.Second, 1.2),
libmqtt.WithRouter(libmqtt.NewRegexRouter()),
libmqtt.WithCustomTLS(tlsConfig),
)
}

Expand Down

0 comments on commit e22c11e

Please sign in to comment.