-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsample.go
More file actions
48 lines (37 loc) · 1.14 KB
/
sample.go
File metadata and controls
48 lines (37 loc) · 1.14 KB
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
package main
import (
"fmt"
"os"
"github.com/gerp/dht22"
)
// Dht22DataPin **************************************************************************************
// Change to whatever your sensor data output is connected to rasp-PI GPIO data socket
// In mine is 24
// ***************************************************************************************************
const (
dht22DataPin = 24
)
func sampleTempHum() (temperatureVal float32, humidityValue float32, errString string) {
var errRead error
temperatureVal, humidityValue, errRead = dht22.Read(dht22DataPin)
if errRead != nil {
errCombined := fmt.Sprintf("Reading samplingLocation, humidity readings failed: %v\n", errRead)
return 0, 0, errCombined
}
return temperatureVal, humidityValue, ""
}
func main() {
args := os.Args[1:]
celcius, humidity, errString := sampleTempHum()
if errString != "" {
fmt.Printf("%s\n", errString)
os.Exit(1)
}
if len(args) == 0 {
// Verbose mode
fmt.Printf("Temperature: %2.1fC, Humidity: %2.1f%%\n", celcius, humidity)
} else {
// Shell mode, set them in shell variable
fmt.Printf("SAMPLE='%.2f,%.2f'\n", celcius, humidity)
}
}