add: included mqtt connection, establish mqtt client

Integrated mqtt client connection and the connection to the broker.
This commit is contained in:
Oliver 2025-10-27 10:36:07 +01:00
parent 928c33380d
commit 9cad525d84
11 changed files with 175 additions and 0 deletions

0
.env Normal file
View file

8
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/ESP32Project.iml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ESP32Project.iml" filepath="$PROJECT_DIR$/.idea/ESP32Project.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

8
backend/go.sum Normal file
View file

@ -0,0 +1,8 @@
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=

40
backend/mqtt.go Normal file
View file

@ -0,0 +1,40 @@
package mqttclient
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type MQTTBroker struct {
MQTTBroker string
MQTTUser string
MQTTPW string
MQTTTopic string
ClientID string
}
// Todo: .env variables instead of clear
// MQTTBroker{
// QTTBroker: "8b758ea22b9f4c0f94ac43c9b09a254f.s1.eu.hivemq.cloud",
//MQTTUser: "esp32_1",
//MQTTPW: "Testuser123",
//MQTTTopic: "test",
//}
func EstablishESPConnection(config MQTTBroker) mqtt.Client {
opts := mqtt.NewClientOptions()
opts.AddBroker(config.MQTTBroker)
opts.SetClientID(config.ClientID)
opts.SetUsername(config.MQTTUser)
opts.SetPassword(config.MQTTPW)
opts.OnConnect = func(c mqtt.Client) {
fmt.Println("✅ Verbunden mit MQTT-Broker:", config.MQTTBroker)
}
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
return client
}

View file

@ -0,0 +1,50 @@
package mqttclient
import (
"fmt"
"log/slog"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type MQTTBroker struct {
MQTTBroker string
MQTTUser string
MQTTPW string
MQTTTopic string
ClientID string
MQTTPort int
}
// Todo: .env variables instead of clear
var config = MQTTBroker{
MQTTBroker: "8b758ea22b9f4c0f94ac43c9b09a254f.s1.eu.hivemq.cloud",
MQTTUser: "oliver1",
MQTTPW: "tESTUSER1234",
MQTTTopic: "esp32/oliver1/metrics",
MQTTPort: 8883,
}
func EstablishESPConnection(config MQTTBroker) mqtt.Client {
opts := mqtt.NewClientOptions()
// opts.AddBroker(config.MQTTBroker)
brokerURL := fmt.Sprintf("tls://%s:%d", config.MQTTBroker, config.MQTTPort)
opts.AddBroker(brokerURL)
opts.SetClientID(config.ClientID)
opts.SetUsername(config.MQTTUser)
opts.SetPassword(config.MQTTPW)
opts.OnConnect = func(c mqtt.Client) {
fmt.Println("✅ Verbunden mit MQTT-Broker:", config.MQTTBroker)
}
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
slog.Error(
"cannot establish connection to ESP",
slog.Any("reason", token.Error()),
slog.String("broker_url", brokerURL),
)
panic(error.Error)
}
return client
}

27
cmd/main.go Normal file
View file

@ -0,0 +1,27 @@
package main
import (
"fmt"
"esp32/backend/mqttclient"
)
func main() {
config := mqttclient.MQTTBroker{
MQTTBroker: "8b758ea22b9f4c0f94ac43c9b09a254f.s1.eu.hivemq.cloud",
MQTTUser: "oliver1",
MQTTPW: "tESTUSER1234",
MQTTTopic: "esp32/oliver1/metrics",
MQTTPort: 8883,
ClientID: "GoClient-12345",
}
client := mqttclient.EstablishESPConnection(config)
defer client.Disconnect(250)
token := client.Publish(config.MQTTTopic, 0, false, "Hallo MQTT!")
token.Wait()
fmt.Println("Programm läuft und hält die Verbindung. Beenden mit Ctrl+C.")
select {}
}

11
go.mod Normal file
View file

@ -0,0 +1,11 @@
module esp32
go 1.25.3
require github.com/eclipse/paho.mqtt.golang v1.5.1
require (
github.com/gorilla/websocket v1.5.3 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sync v0.17.0 // indirect
)

8
go.sum Normal file
View file

@ -0,0 +1,8 @@
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=