This is the sample code for read temperature with DS18B20 sensor on One Wire bus.
The script auto-detect the 1W file and parse it to find the temperature value.
- GreenPonik_OneWire_DS18B20.py Library for Raspberry pi
- Table of Contents
- Installation
- Methods
- Examples
- Credits
> git clone https://github.com/GreenPonik/GreenPonik_OneWire_DS18B20.git
cd GreenPonik_OneWire_DS18B20
pip3 install -r requirements.txt
or
> pip3 install greenponik-onewire-ds18b20
from GreenPonik_OneWire_DS18B20 import read_temps
"""
Get temperatues in celcius and fahrenheit
"""
def read_temps():
from time import sleep
from GreenPonik_OneWire_DS18B20 import read_temps
if __name__ == "__main__":
try:
while True:
# read both celcius and fahrenheit temperatures
temperatures = read_temps()
print("celcius temp %.3f" % temperatures[0])
print("fahrenheit temp %.3f" % temperatures[1])
sleep(1)
except Exception as e:
print("Exception occured", e)
Write by Mickael Lehoux, from GreenPonik, 2019