-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathmqtt.go
63 lines (55 loc) · 1.43 KB
/
mqtt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"log"
"math/rand"
"net/url"
"strconv"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func connect(clientId string, uri *url.URL) mqtt.Client {
opts := createClientOptions(clientId, uri)
client := mqtt.NewClient(opts)
token := client.Connect()
for !token.WaitTimeout(3 * time.Second) {
}
if err := token.Error(); err != nil {
log.Fatal(err)
}
return client
}
func createClientOptions(clientId string, uri *url.URL) *mqtt.ClientOptions {
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s", uri.Host))
opts.SetUsername("mqtt")
password := ""
opts.SetPassword(password)
opts.SetClientID(clientId)
return opts
}
func listen(uri *url.URL, topic string) {
client := connect("sub", uri)
client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
})
}
func main() {
uri, err := url.Parse("tcp://10.0.0.22:1883")
if err != nil {
log.Fatal(err)
}
topic := "sensors"
go listen(uri, topic)
client := connect("pub", uri)
timer := time.NewTicker(1 * time.Second)
for t := range timer.C {
fmt.Println(t)
var min int64 = 10
var max int64 = 100
var random int64 = (rand.Int63n(max-min) + min)
nsec := time.Now().UnixNano()
payload := "weather,location=us-midwest temperature=" + strconv.FormatInt(random, 10) + " " + strconv.FormatInt(nsec, 10)
client.Publish("sensors", 0, false, payload)
}
}