Package: w1thermsensor
W1ThermSensorGet the temperature from your w1 therm sensor in a single line of code!It's designed to be used with the Rasperry Pi hardware but also works on a Beagle Bone and others.Raspberry Pi:this package is available in Raspbian aspython-w1thermsensorandpython3-w1thermsensor.Python 2 drop: all w1thermsensor releases from 2.0 are Python 3.5+Supported devicesThe following w1 therm sensor devices are supported:DS18S20DS1822DS18B20DS28EA00DS1825/MAX31850KSetupThe following hardware is needed:w1 therm compatible sensor (some of them can be bought here:Adafruit: DS18B20)wires to connect the sensor to your board (you might need a breadboard, too)a board like the Raspberry Pi or the Beagle BoneOn the Raspberry Pi, you will need to adddtoverlay=w1-gpio(for regular connection) ordtoverlay=w1-gpio,pullup="y"(for parasitic connection) to your /boot/config.txt. The default data pin is GPIO4 (RaspPi connector pin 7), but that can be changed from 4 toxwithdtoverlay=w1-gpio,gpiopin=x.After that, don't forget to reboot.Hardware-connectionRaspi VCC (3V3) Pin 1 -----------------------------   VCC    DS18B20
                                               |
                                               |
                                               R1 = 4k7 ...10k
                                               |
                                               |
Raspi GPIO 4    Pin 7 -----------------------------   Data   DS18B20
       (BCM)    (BOARD)

Raspi GND       Pin 6 -----------------------------   GND    DS18B20Soft-pull-upAlternatively to the hardware pull-up made by a physical resistor, or to the above mentioned software configurationdtoverlay=w1-gpio,pullup="y"in /boot/config.txt, the following soft pull-up can be used:importRPi.GPIOasGPIOGPIO.setmode(GPIO.BCM)GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_UP)When using this software pull-up, 1-Wire devices will be visible to the kernel only while the program pulls the GPIO pin up.Hw device connection verificationRun the following command:ls-l/sys/bus/w1/devicesYou should check the availability of one or more filenames starting with "28-".Filenames starting with "00-" possibly mean that the pull-up resistor is missing.1-Wire devices can be plugged in dynamically and are visible to the kernel driver just after their hw connection.To test reading the temperature, issue the following command:foriin/sys/bus/w1/devices/28-*;docat$i/w1_slave;doneInstallationFrom PIPThis possibility is supported on all distributions:pip install w1thermsensorNote: maybe root privileges are requiredUse theasyncextra to add support for asyncio andAsyncW1ThermSensor:pip install w1thermsensor[async]On Raspbian usingapt-getIf you are using thew1thermsensormodule on a Rasperry Pi running Raspbian you can install it from the official repository:sudoapt-getinstallpython3-w1thermsensorNote:For older versions of this package you might get the following error:ImportError: No module named 'pkg_resources'which indicates that you need to installpython-setuptoolsorpython3-setuptoolsrespectively.Manually build and install the debian packagedebuild -us -uc
dpkg -i ../python3-w1thermsensor_*.debUsage as python packageThe usage is very simple and the interface clean..
All examples are with theDS18B20sensor - It works the same way for the other supported devices.Basic usage with one sensor (implicit)fromw1thermsensorimportW1ThermSensor,Unitsensor=W1ThermSensor()temperature_in_celsius=sensor.get_temperature()temperature_in_fahrenheit=sensor.get_temperature(Unit.DEGREES_F)temperature_in_all_units=sensor.get_temperatures([Unit.DEGREES_C,Unit.DEGREES_F,Unit.KELVIN])The need kernel modules will be automatically loaded in the constructor of theW1ThermSensorclass.If something went wrong an exception is raised.The first found sensor will be takenBasic usage with one sensor (explicit)The DS18B20 sensor with the ID00000588806awill be taken.fromw1thermsensorimportW1ThermSensor,Sensorsensor=W1ThermSensor(sensor_type=Sensor.DS18B20,sensor_id="00000588806a")temperature_in_celsius=sensor.get_temperature()Multiple sensorsWith theget_available_sensorsclass-method you can get the ids of all available sensors.fromw1thermsensorimportW1ThermSensorforsensorinW1ThermSensor.get_available_sensors():print("Sensor%shas temperature%.2f"%(sensor.id,sensor.get_temperature()))Only sensors of a specific therm sensor type:fromw1thermsensorimportW1ThermSensor,SensorforsensorinW1ThermSensor.get_available_sensors([Sensor.DS18B20]):print("Sensor%shas temperature%.2f"%(sensor.id,sensor.get_temperature()))Set sensor resolutionSome w1 therm sensors support changing the resolution for the temperature reads.w1thermsensorenables to do so with theW1ThermSensor.set_resolution()method:sensor=W1ThermSensor(sensor_type=Sensor.DS18B20,sensor_id="00000588806a")sensor.set_resolution(9)If thepersistargument is set toFalsethis value
is "only" stored in the volatile SRAM, so it is reset when
the sensor gets power-cycled.If thepersistargument is set toTruethe current set
resolution is stored into the EEPROM. Since the EEPROM has a limited
amount of writes (>50k), this command should be used wisely.sensor=W1ThermSensor(sensor_type=Sensor.DS18B20,sensor_id="00000588806a")sensor.set_resolution(9,persist=True)Note: this is supported since Linux Kernel 4.7Note: this requiresrootprivilegesDisable kernel module auto loadingUpon import of thew1thermsensorpackage thew1-thermandw1-gpiokernel modules get loaded automatically.
This requires the python process to run as root. Sometimes that's not what you want, thus you can disable the auto loading
and load the kernel module yourself prior to talk to your sensors withw1thermsensor.You can disable the auto loading feature by setting theW1THERMSENSOR_NO_KERNEL_MODULEenvironment variable to1:# set it globally for your shell so that sub-processes will inherit it.exportW1THERMSENSOR_NO_KERNEL_MODULE=1# set it just for your Python processW1THERMSENSOR_NO_KERNEL_MODULE=1pythonmy_awesome_thermsensor_script.pyEvery other values assigned toW1THERMSENSOR_NO_KERNEL_MODULEwill casew1thermsensorto load the kernel modules.Note: the examples above also apply for the CLI tool usage. See below.Correcting Temperatures / Sensor CalibrationCalibrating the temperature sensor relies on obtaining a measured high and measured low value that
have known reference values that can be used for correcting the sensor's readings.  The simplest
way to do this is to measure the melting point and boiling point of water since those values are
known.  This method will only work with waterproof sensors - you will need a different mechanism
for obtaining measured values if you are not using a waterproof sensor.In order to obtain themeasured_low_point, fill a container to 80% with ice and add water to the
ice until the ice is floating and water is at the surface.  Submerse your sensor in the ice water,
ensuring it does not touch the container.  Wait 5 minutes for the temperature to stabilize in the
container and then once the sensor readings have stabilized for approximately 30 seconds (readings
remain consistent), record the value as themeasured_low_pointIn order to obtain themeasured_high_point, bring a pot of water to a rapid boil.  Place your
sensor in the boiling water, ensuring that it does not touch the pot.  Allow the sensor to come up
to temperature and once it has stabilized for approximately 30 seconds (readings remain
consistent), record the value as themeasured_high_pointGenerally speaking, thereference_low_pointshould be left at 0.0 unless you have some special
situation that changes the melting point of water.  Because melting does not involve a gaseous
phase change, the effects of air pressure and altitude on the melting point are minimal.Thereference_high_pointon the other hand is greatly impacted by air pressure (and thus
altitude).  For example, the boiling point of water is 100.0C at sea level, and is approximately
72C at the summit of Mount Everest (8848m above sea level).  While air pressure is what actually
dictates boiling point, generally speaking altitude is a close enough approximation for most use
cases.Engineering Toolboxhas a page that gives you the boiling point of water at different altitudes.This method is derived fromthis Instructable.fromw1thermsensor.calibration_dataimportCalibrationDatafromw1thermsensorimportW1ThermSensor,Unitcalibration_data=CalibrationData(measured_high_point=measured_high_point,measured_low_point=measured_low_point,reference_high_point=reference_high_point,reference_low_point=reference_low_point,# optional, defaults to 0.0)sensor=W1ThermSensor(calibration_data=calibration_data)corrected_temperature_in_celsius=sensor.get_corrected_temperature()corrected_temperature_in_fahrenheit=sensor.get_corrected_temperature(Unit.DEGREES_F)corrected_temperature_in_all_units=sensor.get_corrected_temperatures([Unit.DEGREES_C,Unit.DEGREES_F,Unit.KELVIN])Async InterfaceThew1thermsensorpackage implements an async interfaceAsyncW1ThermSensorfor asyncio.The following methods are supported:get_temperature()get_temperatures()get_resolution()For example:fromw1thermsensorimportAsyncW1ThermSensor,Unitsensor=AsyncW1ThermSensor()temperature_in_celsius=awaitsensor.get_temperature()temperature_in_fahrenheit=awaitsensor.get_temperature(Unit.DEGREES_F)temperature_in_all_units=awaitsensor.get_temperatures([Unit.DEGREES_C,Unit.DEGREES_F,Unit.KELVIN])Usage as CLI toolThe w1thermsensor module can be used as CLI tool since version0.3.0.Please note that the CLI tool will only get installed with the Raspbian Python 3 package(sudo apt-get install python3-w1thermsensor)List sensorsList all available sensors:$ w1thermsensor ls
$ w1thermsensor ls --json  # show results in JSON formatList only sensors of a specific type:$ w1thermsensor ls --type DS1822
$ w1thermsensor ls --type DS1822 --type MAX31850K  # specify multiple sensor types
$ w1thermsensor ls --type DS1822 --json  # show results in JSON formatShow temperaturesShow temperature of all available sensors: (Same synopsis asls)$ w1thermsensor all --type DS1822
$ w1thermsensor all --type DS1822 --type MAX31850K  # specify multiple sensor types
$ w1thermsensor all --type DS1822 --json  # show results in JSON formatShow temperature of a single sensor:$ w1thermsensor get 1  # 1 is the id obtained by the ls command
$ w1thermsensor get --hwid 00000588806a --type DS18B20
$ w1thermsensor get 1  # show results in JSON formatShow temperature of a single sensor in the given resolution$ w1thermsensor get 1 --resolution 10
$ w1thermsensor get --hwid 00000588806a --type DS18B20 --resolution 11Change temperature read resolution and write to EEPROM# w1thermsensor resolution 10 1
# w1thermsensor resolution --hwid 00000588806a --type DS18B20 11Note: this requiresrootprivilegesContributionI'm happy about all types of contributions to this project! :beers:This project is published underMIT.ATimo Furrerproject.- :tada: -