Skip to content

Commit 9743e38

Browse files
authored
devices/ds18b20: issue 59 add demo (#9)
1 parent 5565104 commit 9743e38

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

ds18b20/main.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2024 The Periph Authors. All rights reserved.
2+
// Use of this source code is governed under the Apache License, Version 2.0
3+
// that can be found in the LICENSE file.
4+
//
5+
// This program enumerates onewire buses for DS18B20 sensors and
6+
// continuously reading them.
7+
//
8+
// If you get the error no buses were found, ensure that onewire buses
9+
// are enabled. On a Raspberry Pi, run raspi-config, Interface Options,
10+
// and enable 1-Wire. Remember to reboot after enabling it.
11+
package main
12+
13+
import (
14+
"fmt"
15+
"log"
16+
"time"
17+
18+
"periph.io/x/conn/v3/onewire"
19+
"periph.io/x/conn/v3/onewire/onewirereg"
20+
"periph.io/x/devices/v3/ds18b20"
21+
"periph.io/x/host/v3"
22+
)
23+
24+
// For the Dallas onewire devices, the conversion time is dependent on the
25+
// resolution. Refer to the Datasheet for more information.
26+
const DefaultBits = 10
27+
28+
// enumerateBusForSensors searches for addresses on the bus and if it finds
29+
// a DS18X20 series device, returns it.
30+
func enumerateBusForSensors(bus onewire.Bus) []*ds18b20.Dev {
31+
result := make([]*ds18b20.Dev, 0)
32+
addresses, err := bus.Search(false)
33+
if err != nil {
34+
log.Print(" Search error:", err)
35+
return result
36+
}
37+
38+
for _, address := range addresses {
39+
family := ds18b20.Family(address & 0xff)
40+
if family == ds18b20.DS18S20 || family == ds18b20.DS18B20 {
41+
log.Printf("Found Device %s Address %#016X\n", family, address)
42+
dev, err := ds18b20.New(bus, address, DefaultBits)
43+
if err != nil {
44+
log.Print(err)
45+
} else {
46+
result = append(result, dev)
47+
}
48+
}
49+
}
50+
return result
51+
}
52+
53+
func main() {
54+
if _, err := host.Init(); err != nil {
55+
log.Fatal(err)
56+
}
57+
58+
buses := make([]onewire.Bus, 0)
59+
sensors := make([]*ds18b20.Dev, 0)
60+
for _, ref := range onewirereg.All() {
61+
log.Printf("Found One-Wire Bus %s\n", ref.Name)
62+
busCloser, err := ref.Open()
63+
if err != nil {
64+
fmt.Println(" Open error:", err)
65+
continue
66+
}
67+
bus := busCloser.(onewire.Bus)
68+
newSensors := enumerateBusForSensors(bus)
69+
if len(newSensors) > 0 {
70+
// Do an initial convert on all devices on the bus.
71+
err = ds18b20.ConvertAll(bus, DefaultBits)
72+
if err != nil {
73+
log.Println(err)
74+
}
75+
buses = append(buses, bus)
76+
sensors = append(sensors, newSensors...)
77+
}
78+
}
79+
if len(buses) == 0 {
80+
log.Fatal("no onewire buses found.")
81+
}
82+
if len(sensors) == 0 {
83+
log.Fatal("no DS18X20 sensors found.")
84+
}
85+
86+
// This demo uses StartAll()/LastTemp() to read without any latency.
87+
// You can also use Sense() to read the units, but there will be
88+
// the conversion latency.
89+
for {
90+
out := fmt.Sprintf("%d ", time.Now().Unix())
91+
for _, dev := range sensors {
92+
t, err := dev.LastTemp()
93+
if err == nil {
94+
out += fmt.Sprintf("%.2f ", t.Celsius())
95+
} else {
96+
log.Print(err)
97+
out += "err "
98+
}
99+
100+
}
101+
fmt.Println(out)
102+
103+
for _, bus := range buses {
104+
// Start the conversion cycle running while we're sleeping ...
105+
err := ds18b20.StartAll(bus)
106+
if err != nil {
107+
log.Println(err)
108+
}
109+
}
110+
time.Sleep(time.Duration(1000-(time.Now().UnixMilli()%1000)) * time.Millisecond)
111+
}
112+
}

0 commit comments

Comments
 (0)