Compare commits

...

4 commits

Author SHA1 Message Date
38b4e0abdc Add w1-therm gokrazy module
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2024-06-15 22:02:15 +02:00
fde9a29d1a Rename configuration init 2024-06-15 21:37:38 +02:00
3526812da8 Add default sensors to config 2024-06-15 21:37:21 +02:00
2b1dd61ecc Replace ioutil with os 2024-06-15 21:37:00 +02:00
5 changed files with 99 additions and 6 deletions

3
cmd/w1-therm/README.md Normal file
View file

@ -0,0 +1,3 @@
This package provides support for using 1-wire DS18B20 with gokrazy!
The package just loads the kernel module.

91
cmd/w1-therm/main.go Normal file
View file

@ -0,0 +1,91 @@
// w1-therm is a gokrazy helper that loads 1-wire kernel modules on boot.
//
// Example:
//
// Include the w1-therm package in your gokr-packer command:
// % gokr-packer -update=yes \
// github.com/gokrazy/breakglass \
// git.joco.dk/snr/fermentord/cmd/w1-therm
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
)
func logic() error {
flag.Parse()
for _, mod := range []string{
"kernel/drivers/w1/wire.ko",
"kernel/drivers/w1/masters/w1-gpio.ko",
"kernel/drivers/w1/slaves/w1-therm.ko",
} {
if err := loadModule(mod); err != nil && !os.IsNotExist(err) {
return err
}
}
dev := "w1_bus_master1"
target, err := checkOneWireInterface(dev)
if err != nil {
log.Printf("Bluetooth interface %v not found.", target)
} else {
fmt.Printf("Bluetooth device %v: %v\n", dev, target)
}
// gokrazy should not supervise this process even when manually started.
os.Exit(125)
return nil
}
func checkOneWireInterface(device string) (string, error) {
name := fmt.Sprintf("/sys/bus/w1/devices/%v", device)
target, err := os.Readlink(name)
if err != nil {
return "", fmt.Errorf("OneWire bus master %v not found", device)
}
return target, nil
}
func loadModule(mod string) error {
f, err := os.Open(filepath.Join("/lib/modules", release, mod))
if err != nil {
return err
}
defer f.Close()
if err := unix.FinitModule(int(f.Fd()), "", 0); err != nil {
if err != unix.EEXIST &&
err != unix.EBUSY &&
err != unix.ENODEV &&
err != unix.ENOENT {
return fmt.Errorf("FinitModule(%v): %v", mod, err)
}
}
modname := strings.TrimSuffix(filepath.Base(mod), ".ko")
log.Printf("modprobe %v", modname)
return nil
}
var release = func() string {
var uts unix.Utsname
if err := unix.Uname(&uts); err != nil {
fmt.Fprintf(os.Stderr, "minitrd: %v\n", err)
os.Exit(1)
}
return string(uts.Release[:bytes.IndexByte(uts.Release[:], 0)])
}()
func main() {
if err := logic(); err != nil {
log.Fatal(err)
}
}

View file

@ -23,9 +23,9 @@ tilt = "DWJONDAHL.ingest.fermentor.ingest_tilt_reading"
port = 8000 port = 8000
[sensors] [sensors]
wort = "" wort = "28-021892458c02"
chamber = "" chamber = "28-02319245083c"
ambient = "" ambient = "28-02319245022b"
weight = 0.8 weight = 0.8
[i2c] [i2c]

View file

@ -6,7 +6,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -225,7 +224,7 @@ func triggerBulkRead() error {
} }
func pollBusState() (BulkReadBusState, error) { func pollBusState() (BulkReadBusState, error) {
b, err := ioutil.ReadFile("/sys/bus/w1/devices/w1_bus_master1/therm_bulk_read") b, err := os.ReadFile("/sys/bus/w1/devices/w1_bus_master1/therm_bulk_read")
if err != nil { if err != nil {
return BulkReadIdle, err return BulkReadIdle, err
} }
@ -241,7 +240,7 @@ func pollBusState() (BulkReadBusState, error) {
// read returns the temperature of the specified sensor in millidegrees celcius. // read returns the temperature of the specified sensor in millidegrees celcius.
func read(sensor string) (int64, error) { func read(sensor string) (int64, error) {
path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave") path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave")
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return NaN, err return NaN, err
} }