/
main.go
132 lines (118 loc) · 3.18 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/all"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
type Data struct {
Load1 float64 `json:"cpuload"`
Load5 float64 `json:"cpuload5"`
Load15 float64 `json:"cpuload15"`
CPUTemp float64 `json:"cputemp"`
}
type Payload struct {
DataPoints Data `json:"d"`
Timestamp time.Time `json:"ts"`
}
var (
host Platform
configFile *string
config *Config
quickstartBaseURL string = "http://quickstart.internetofthings.ibmcloud.com/#/device/"
)
func init() {
configFile = flag.String("conf", "", "IoT app configuration file")
flag.Parse()
switch h, _, err := embd.DetectHost(); h {
case embd.HostRPi:
host = NewRPi()
case embd.HostBBB:
host = NewBeagleBone()
default:
if err != nil {
panic(err)
}
}
}
func actionHandler(client *MQTT.Client, message MQTT.Message) {
fmt.Println("Received action message on", message.Topic(), "-", string(message.Payload()))
action := strings.ToLower(string(message.Payload()))
switch action {
case "off":
host.LedsOff()
case "on":
host.LedsOn()
case "toggle":
host.LedsToggle()
case "slide":
host.LedsCycle(3)
}
}
func SendData(client *MQTT.Client, endChan chan struct{}) {
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-endChan:
return
case <-ticker.C:
var p Payload
p.DataPoints.Load1, p.DataPoints.Load5, p.DataPoints.Load15 = getLoadAvg()
p.DataPoints.CPUTemp = getCPUTemp(host.CPUTempFile())
p.Timestamp = time.Now()
payloadBytes, err := json.Marshal(p)
if err == nil {
client.Publish(config.PubTopic, 0, false, payloadBytes)
} else {
fmt.Println(err.Error())
}
}
}
}
func main() {
endChan := make(chan struct{})
host.LedsOff()
config = ParseConfig(*configFile)
fmt.Println("Device ID:", config.DeviceID)
fmt.Println("Connecting to MQTT broker:", config.BrokerAddress)
opts := MQTT.NewClientOptions().AddBroker(config.BrokerAddress).SetClientID(config.ClientID)
if !config.QuickStart {
tlsConfig := &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}
opts.SetUsername(config.Username).SetPassword(config.AuthToken).SetTLSConfig(tlsConfig)
}
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
fmt.Println("Connected")
host.LedsCycle(3)
go SendData(client, endChan)
fmt.Println("Sending Data")
if config.QuickStart {
fmt.Println("Go to the following link to see your device data;")
fmt.Println(quickstartBaseURL + config.DeviceID + "/sensor/")
} else {
var token MQTT.Token
fmt.Println("Subscribing for action messages")
if token = client.Subscribe("iot-2/cmd/+/fmt/text", 0, actionHandler); token.Wait() && token.Error() != nil {
fmt.Println("Error subscribing for action messages")
} else {
for topic, qos := range token.(*MQTT.SubscribeToken).Result() {
fmt.Println("Subscribed to", topic, "at Qos", qos)
}
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
host.Close()
}