Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ds18b20 driver bug fix #23

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ credentials.py
.vscode/
__pycache__/
*.pyc
/.idea

2 changes: 1 addition & 1 deletion explorer/basic_demos/ds18b20_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
print('and its description is:', ds18b20_1.description)

for _ in range(10):
print('Water temperature=%s Celsius' % ds18b20_1.value())
print('Water temperature= %s Celsius' % ds18b20_1.value())
sleep(1)
30 changes: 19 additions & 11 deletions explorer/lib/ae_drivers/ds18b20.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,57 @@
import time
import os
import glob

DEBUG = 1 # Normally on 0. Non zero enable debug code/exceptions

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'


class AE_DS18B20(_AE_Peripheral_Base):
"""This class is to define a DS18B20 type (water)temperature sensor.
"""

def __init__(self, name, description):
super().__init__(name, description, 'DS18B20')
def _read_temp_raw():

def _read_temp_raw(self):
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines

def _read_temp(self):
lines = _read_temp_raw()
lines = self._read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = _read_temp_raw()
lines = self._read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_string = lines[1][equals_pos + 2:]
temp_c = float(temp_string) / 1000.0
#temp_f = temp_c * 9.0 / 5.0 + 32.0
# temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c


def _str_details(self):
return 'values=%s' % (str(self.value()))

def value(self):
"""Read a temperature value in Celsius from the sensor
"""
try:
return self._read_temp()
except Exception:
except Exception as ex:
if DEBUG:
raise ex
return None

def _str_details(self):
"""Some more default into the __str__ dunder form the base class
"""
return 'device=%s, value=%s ppm' % (self._device,
self.value())

def setup(self, **kwarg):
# One throw away reads to get things going...
self.value()
2 changes: 1 addition & 1 deletion learning_stuff/lcdi2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
import datetime

I2C_ADDR = 0x38 # I2C device address
I2C_ADDR = 0x27 # I2C device address
LCD_WIDTH = 16 # Maximum characters per line

# Define some device constants
Expand Down
14 changes: 12 additions & 2 deletions learning_stuff/read_and_send_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#import bme280 #if you have a BME280 sensor instead of an DHT22
import waterTemp
import co2
import sunrise
import bh1750
import lcdi2c
import paho.mqtt.client as mqtt # paho.mqtt is the mqtt protocol
Expand Down Expand Up @@ -105,7 +106,12 @@ def SMA_send(params):
except Exception as ex:
astro_logger.exception(ex) # Log exception info and continue…
pass

try:
CO2_sun = sunrise.readSunrise()
params["CO2_sun"] = CO2_sun
except Exception as ex:
astro_logger.exception(ex) # Log exception info and continue…
pass
try:
light = bh1750.readLight()
params["light"] = light
Expand Down Expand Up @@ -139,10 +145,14 @@ def SMA_send(params):
lcdi2c.display('water temp', str(round(wt, 2)) + ' C', 5)
else:
lcdi2c.display('could not read', 'water temp', 5)
if co2 is not None:
if CO2 is not None:
lcdi2c.display('co2', str(round(CO2, 2)) + ' ppm', 5)
else:
lcdi2c.display('could not read', 'co2', 5)
if CO2_sun is not None:
lcdi2c.display('co2_sunrise', str(round(CO2_sun, 2)) + ' ppm', 5)
else:
lcdi2c.display('could not read', 'co2_sun', 5)
if light is not None:
lcdi2c.display('light', str(round(light, 2)) + ' lux', 5)
else:
Expand Down
36 changes: 36 additions & 0 deletions learning_stuff/sunrise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python

import smbus
from time import sleep
from ctypes import c_short
from ctypes import c_byte
from ctypes import c_ubyte

DEVICE = 0x68 # Default device I2C address

bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1
# Rev 1 Pi uses bus 0
REG_CO2_DATA = 0x06
sleep(1)


def readSunrise(addr=DEVICE):
data = bus.read_i2c_block_data(addr, REG_CO2_DATA, 2)
co2_true = int.from_bytes(bytes(data), byteorder='big', signed=True)
print(f' raw data is {data}')
return co2_true


def main():
for i in range(0, 11):
test = b'\x02\xBC'
print(f'test is {test[0]} and {test[1]}')
co2 = int.from_bytes(test, byteorder='big', signed=True)
print(f'reference co2 is {co2}')
co2_true = readSunrise(DEVICE)
print(f' true co2 value is {co2_true}')
sleep(2)


if __name__ == "__main__":
main()