The Component
Wiring it Up
The little breakout board that the chip is attached to has three pins labeled 'G', 'R', and 'Y'. The 'G' pin goes to ground, the 'R' pin goes to +5V DC, and the 'Y' pin goes to Signal. In my case, the signal is pin 7 on the Raspberry Pi.Enable 1-Wire
From what I have been able to learn the sensor uses 1-Wire for the communications.I am running DietPi on the Raspberry Pi so I could not use the standard 'raspi-config' or the 'Raspberry Pi Configuration' in Raspbian. This left me with the option of adding
dtoverlay=w1-gpio
to the '/boot/config.txt' file. Just a quick edit of a file and I should be good to go. It turns out that on DietPi you need to edit the file '/DietPi/config.txt' in order to have the change survive a reboot.
Once dtoverlay is added to 'DietPi/config.txt' file, the Rasberry Pi will start communicating with the device. Take a look in the folder '/sys/bus/w1/devices'. One of the folders will start with '28-' that folder will contain a file name 'w1-slave' if you read the content of that file you will get something like
72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
72 01 4b 46 7f ff 0e 10 57 t=23125
Notice the 't=' at near the end of the second line. This is the temperature in Celsius. This temperature is also missing a decimal point.
Go Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"path/filepath" | |
"strconv" | |
"strings" | |
) | |
const ( | |
w1root = "/sys/bus/w1/devices" | |
w1master = "/w1_bus_master1/w1_master_slaves" | |
dataFile = "/w1_slave" | |
) | |
var errReadingSensor = errors.New("failed to read sensor") | |
func getSensors() ([]string, error) { | |
data, err := ioutil.ReadFile(filepath.Join(w1root, w1master)) | |
if err != nil { | |
return nil, err | |
} | |
sensors := strings.Split(string(data), "\n") | |
if len(sensors) > 0 { | |
sensors = sensors[:len(sensors)-1] | |
} | |
return sensors, nil | |
} | |
func reading(sensor string) (float64, error) { | |
f := filepath.Join(w1root, "/", sensor, dataFile) | |
data, err := ioutil.ReadFile(f) | |
if err != nil { | |
return 0.0, fmt.Errorf("Could not read sensor %s", f) | |
} | |
raw := string(data) | |
i := strings.LastIndex(raw, "t=") | |
if i == -1 { | |
log.Println(raw) | |
return 0.0, fmt.Errorf("Could not find tempature value") | |
} | |
c, err := strconv.ParseFloat(raw[i+2:len(raw)-1], 64) | |
if err != nil { | |
return 0.0, fmt.Errorf("Coulnd not convert response to float") | |
} | |
return c / 1000.0, nil | |
} | |
func main() { | |
mySensors, err := getSensors() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Sensor Ids: %v\n", mySensors) | |
for _, sensor := range mySensors { | |
t, err := reading(sensor) | |
if err == nil { | |
fmt.Printf("sensor: %s temperature: %.2f°C\n", sensor, t) | |
} | |
} | |
} |
Misc
The RW1820 is very similar to the DS18B20. There is a lot more information about the DS18B20.
Resources
Rayway International - ChineseRW1820 Datasheet - English
1-Wire
Enable 1-Wire Interface on the Raspberry Pi
DietPi
W1-GPIO - One-Wire Interface
Introduction to DS18B20
No comments:
Post a Comment