-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrato-temp.go
50 lines (40 loc) · 1.15 KB
/
strato-temp.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
package main
import (
"flag"
"fmt"
"github.com/stratoberry/go-bmp085"
"log"
"net"
"os"
"time"
)
var outFile = flag.String("output", "/data/temperature.csv", "output filename")
var udpPort = flag.Int("udp", 8367, "UDP port for sending data")
var updateFreq = flag.Duration("freq", 10, "update frequency in seconds")
// Feb 1st 2014
const EPOCH = 1391212800
func main() {
flag.Parse()
var dev *bmp085.Device
var err error
if dev, err = bmp085.Init(0x77, 1, bmp085.MODE_STANDARD); err != nil {
log.Panicln("Failed to init device", err)
}
f, err := os.OpenFile(*outFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Panicln("Failed to open output file", err)
}
defer f.Close()
conn, _ := net.Dial("udp", fmt.Sprintf("255.255.255.255:%d", *udpPort))
defer conn.Close()
t := time.Tick((*updateFreq) * time.Second)
for now := range t {
if temp, pressure, alt, err := dev.GetData(); err != nil {
log.Panicln("Failed to get data from the device", err)
} else {
stanza := fmt.Sprintf("%d;%.2f;%.2f;%.2f\n", now.Unix()-EPOCH, temp, float64(pressure)/100, alt)
f.WriteString(stanza)
conn.Write([]byte(stanza))
}
}
}