1-wire-temp/parse.go

44 lines
733 B
Go
Raw Permalink Normal View History

2021-10-02 11:30:37 +00:00
package main
import (
"fmt"
2021-10-02 11:39:18 +00:00
"io/ioutil"
2021-10-02 11:30:37 +00:00
"regexp"
"strconv"
"strings"
)
var reg = regexp.MustCompile("t=\\d+")
2021-10-02 11:39:18 +00:00
func ParseAndSetTemp() {
f, err := ioutil.ReadFile("/sys/bus/w1/devices/" + sensor + "/w1_slave")
//f, err := ioutil.ReadFile("testsensorfile")
if err != nil {
fmt.Printf("error opening sensor file: %s\n", err)
return
}
2021-10-02 11:30:37 +00:00
2021-10-02 12:41:13 +00:00
ct := parse(string(f))
fmt.Printf("Setting current temperature to %f\n", ct)
currentTemp.Set(ct)
2021-10-02 11:30:37 +00:00
}
func parse(w1Out string) float64 {
t := reg.FindString(w1Out)
if t == "" {
return 0
}
temp := strings.TrimLeft(t, "t=")
f, err := strconv.ParseFloat(temp, 64)
if err != nil {
fmt.Printf("error parsing temperature %s: %s\n", temp, err)
return 0
}
return f / 1000
}