Skip to content

Commit 4dcc83c

Browse files
authored
Merge pull request #1 from cjessett/flags
Flags
2 parents a60f4fa + 10cc974 commit 4dcc83c

File tree

2 files changed

+86
-57
lines changed

2 files changed

+86
-57
lines changed

README.md

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@ This script reads an MCP9808 temperature sensor from a Raspberry Pi and updates
44

55
## Prerequisites
66

7-
Create a `.env` file with the following:
8-
```
9-
THING_NAME=<Thing name>
10-
ENDPOINT=<Thing endpoint>
11-
PRIVATE_KEY_PATH=<path to private key>
12-
CERT_PATH=<path to cert>
13-
ROOT_CA_PATH=<path to root ca>
14-
LOG_FILE_PATH=<path to log file>
15-
```
16-
177
### Wire up the MCP9808
188
![fritzing](rpi-temp.jpg)
199

@@ -26,5 +16,37 @@ make
2616

2717
## Execute the script
2818
```
29-
./mcp9808-thing
19+
./mcp9808-thing -thing=<thing_name> -endpoint=<endpoint> -privatekey=<key_path> -cert=<cert_path> -rootca=<rootCA_path> -logfile=<log_path>
20+
```
21+
22+
### Create a start script
23+
24+
Create a `.env` file with the following:
25+
```
26+
export THING_NAME=<Thing name>
27+
export ENDPOINT=<Thing endpoint>
28+
export PRIVATE_KEY_PATH=<path to private key>
29+
export CERT_PATH=<path to cert>
30+
export ROOT_CA_PATH=<path to root ca>
31+
export LOG_FILE_PATH=<path to log file>
32+
```
33+
34+
Create a `start` script to convert env variables to flags and execute the binary
35+
```
36+
#!/bin/bash
37+
38+
source .env
39+
40+
./mcp9808-thing \
41+
-thing=${THING_NAME} \
42+
-endpoint=${ENDPOINT} \
43+
-privatekey=${PRIVATE_KEY_PATH} \
44+
-cert=${CERT_PATH} \
45+
-rootca=${ROOT_CA_PATH} \
46+
-logfile=${LOG_FILE_PATH}
47+
```
48+
49+
Execute the script
50+
```
51+
./start
3052
```

main.go

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,50 @@ package main
22

33
import (
44
"encoding/json"
5+
"flag"
56
"fmt"
67
"io"
7-
"log"
88
"os"
99

10-
"github.com/joho/godotenv"
1110
"github.com/kuzemkon/aws-iot-device-sdk-go/device"
11+
"github.com/sirupsen/logrus"
1212
"periph.io/x/periph/conn/i2c/i2creg"
1313
"periph.io/x/periph/experimental/devices/mcp9808"
1414
"periph.io/x/periph/host"
1515
)
1616

17-
type Shadow struct {
18-
State struct {
19-
Reported struct {
20-
Temp int `json:"temp"`
21-
} `json:"reported"`
22-
} `json:"state"`
23-
Version int
24-
}
17+
var thingName string
18+
var endpoint string
19+
var privateKey string
20+
var cert string
21+
var rootCA string
22+
var logFile string
23+
var log = logrus.New()
24+
25+
func init() {
26+
flag.StringVar(&thingName, "thing", "", "Set this to the AWS IoT thing name")
27+
flag.StringVar(&endpoint, "endpoint", "", "Set this to the AWS IoT endpoint")
28+
flag.StringVar(&privateKey, "privatekey", "", "This must be a full path to the AWS IoT thing private key .pem.key file")
29+
flag.StringVar(&cert, "cert", "", "This must be a full path to the AWS IoT thing cert .pem.crt file")
30+
flag.StringVar(&rootCA, "rootca", "", "This must be a full path to the AWS IoT thing root-CA .crt file")
31+
flag.StringVar(&logFile, "logfile", "", "Set this to the full path for the log file")
32+
flag.Parse()
33+
34+
if thingName == "" || endpoint == "" || privateKey == "" || cert == "" || rootCA == "" || logFile == "" {
35+
flag.PrintDefaults()
36+
log.Panic("missing required flag")
37+
}
2538

26-
func initEnv() error {
27-
home, err := os.UserHomeDir()
39+
// Initilize logging
40+
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
2841
if err != nil {
29-
return err
42+
log.Info("error opening file: %v", err)
3043
}
31-
err = godotenv.Load(fmt.Sprintf("%s/.env", home))
32-
return err
33-
}
3444

35-
func initThing() (*device.Thing, error) {
36-
thingName := device.ThingName(os.Getenv("THING_NAME"))
37-
endpoint := os.Getenv("ENDPOINT")
38-
keyPair := device.KeyPair{
39-
PrivateKeyPath: os.Getenv("PRIVATE_KEY_PATH"),
40-
CertificatePath: os.Getenv("CERT_PATH"),
41-
CACertificatePath: os.Getenv("ROOT_CA_PATH"),
42-
}
45+
mw := io.MultiWriter(os.Stdout, file)
4346

44-
return device.NewThing(keyPair, endpoint, thingName)
47+
log.SetFormatter(&logrus.JSONFormatter{})
48+
log.SetOutput(mw)
4549
}
4650

4751
func readTemp() int {
@@ -73,32 +77,32 @@ func readTemp() int {
7377
return int(measurement.Fahrenheit())
7478
}
7579

76-
func main() {
77-
err := initEnv()
78-
if err != nil {
79-
panic(err)
80-
}
80+
type Shadow struct {
81+
State struct {
82+
Reported struct {
83+
Temp int `json:"temp"`
84+
} `json:"reported"`
85+
} `json:"state"`
86+
Version int
87+
}
8188

82-
logPath := os.Getenv("LOG_FILE_PATH")
83-
f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
84-
if err != nil {
85-
log.Fatalf("error opening file: %v", err)
89+
func main() {
90+
// Initilize a new Thing
91+
keyPair := device.KeyPair{
92+
PrivateKeyPath: privateKey,
93+
CertificatePath: cert,
94+
CACertificatePath: rootCA,
8695
}
87-
defer f.Close()
8896

89-
mw := io.MultiWriter(os.Stdout, f)
90-
log.SetOutput(mw)
91-
92-
// Initilize a new Thing
93-
thing, err := initThing()
97+
thing, err := device.NewThing(keyPair, endpoint, device.ThingName(thingName))
9498
if err != nil {
95-
panic(err)
99+
log.Panic(err)
96100
}
97101

98102
// Subscribe to shadow
99103
shadowChan, err := thing.SubscribeForThingShadowChanges()
100104
if err != nil {
101-
panic(err)
105+
log.Panic(err)
102106
}
103107

104108
// Read temperature from sensor
@@ -108,20 +112,23 @@ func main() {
108112
// Update thing shadow
109113
err = thing.UpdateThingShadow(device.Shadow(shadow))
110114
if err != nil {
111-
panic(err)
115+
log.Panic(err)
112116
}
113117

114118
updatedShadow, ok := <-shadowChan
115119
if !ok {
116-
panic("Failed to read from shadow channel")
120+
log.Panic("Failed to read from shadow channel")
117121
}
118122

119123
unmarshaledUpdatedShadow := &Shadow{}
120124

121125
err = json.Unmarshal(updatedShadow, unmarshaledUpdatedShadow)
122126
if err != nil {
123-
panic(err)
127+
log.Panic(err)
124128
}
125129

126-
log.Printf("Temp: %v Version: %v", unmarshaledUpdatedShadow.State.Reported.Temp, unmarshaledUpdatedShadow.Version)
130+
log.WithFields(logrus.Fields{
131+
"temperature": unmarshaledUpdatedShadow.State.Reported.Temp,
132+
"version": unmarshaledUpdatedShadow.Version,
133+
}).Info("Updated Shadow")
127134
}

0 commit comments

Comments
 (0)