Skip to content

Commit

Permalink
fixed measurement with custom gpio
Browse files Browse the repository at this point in the history
  • Loading branch information
JavanXD committed Apr 19, 2020
1 parent 18685f9 commit 6eb2f0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def measure_all_sensors(debug, filtered_temperature, ds18b20Sensors, bme680Senso
ds18b20_temperature = ds18b20_temperature-float(sensor["offset"])
ds18b20_temperature = float("{0:.2f}".format(ds18b20_temperature)) # round to two decimals
ts_fields.update({sensor["ts_field"]: ds18b20_temperature})
elif 'ts_field' and 'device_id' in sensor:
elif 'ts_field' in sensor:
# Case for filtered_temperature was not filled, use direct measured temperture in this case
ds18b20_temperature = measure_temperature(sensor)
if sensor["ts_field"] and ds18b20_temperature is not None:
Expand Down
5 changes: 2 additions & 3 deletions read_bme680.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initBME680(ts_sensor):
print('BME680: The Temperature Offset is ' + str(offset) + ' °C')
except:
offset = 0
print('BME680: The Temperature Offset is default: ' + str(offset) + '°C')
#print('BME680: The Temperature Offset is default: ' + str(offset) + '°C')

# These oversampling settings can be tweaked to change the balance between accuracy and noise in the data.
sensor.set_humidity_oversample(bme680.OS_2X)
Expand All @@ -57,10 +57,9 @@ def initBME680(ts_sensor):

def initBME680FromMain(ts_sensor):
if isSMBusConnected():
print("A Sensor (eg. BME680/WittyPi) is connected to SMBus.")
return initBME680(ts_sensor)
else:
print("SMBus is not connected.")
print("No I2C sensor connected to SMBus. Please check sensor or remove BME680 from settings.")
return 0

def burn_in_bme680(sensor, burn_in_time):
Expand Down
26 changes: 16 additions & 10 deletions read_ds18b20.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@

def measure_temperature(sensor):
try:
if 'device_id' not in sensor:
sensor["device_id"] = "undefined"

if 'pin' in sensor and isinstance(sensor["pin"], (int)) and sensor["pin"] > 0:
print("GPIO" + str(sensor["pin"]) + " is defined as 3.3V power source for Ds18b20 '" + sensor["device_id"] + "'")
setup_gpio(sensor["pin"])

if sensor["device_id"] != "undefined":

if (os.path.isdir("/sys/bus/w1/devices/" + sensor["device_id"]) == False):
error_log("Info: Resetting 3.3V GPIO " + str(sensor["pin"]) + " because " + sensor["device_id"] + " was missing.")
error_log("Info: Resetting 3.3V GPIO" + str(sensor["pin"]) + " because Ds18b20 with device-id '" + sensor["device_id"] + "' was missing.")
reset_ds18b20_3V(sensor["pin"])

# read 1-wire slave file
with open('/sys/bus/w1/devices/' + sensor["device_id"] + '/w1_slave', 'r') as file:
file_content = file.read()
file.close()
# read 1-wire slave file
with open('/sys/bus/w1/devices/' + sensor["device_id"] + '/w1_slave', 'r') as file:
file_content = file.read()
file.close()

# read temperature and convert temperature
string_value = file_content.split("\n")[1].split(" ")[9]
temperature = float(string_value[2:]) / 1000
temperature = float('%6.2f' % temperature)
# read temperature and convert temperature
string_value = file_content.split("\n")[1].split(" ")[9]
temperature = float(string_value[2:]) / 1000
temperature = float('%6.2f' % temperature)

return temperature
return temperature

except FileNotFoundError:
error_log("Warning: Cannot find Device-ID from Ds18b20 Sensor " + sensor["device_id"])
Expand Down

0 comments on commit 6eb2f0a

Please sign in to comment.