Skip to content

Commit

Permalink
Power cycle sensors on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Fontes authored and Daniel Fontes committed Feb 2, 2024
1 parent a5c8da8 commit 1caeba8
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions dbus-i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,32 @@
from vedbus import VeDbusService, VeDbusItemExport, VeDbusItemImport
from settingsdevice import SettingsDevice # available in the velib_python repository

SensorPowerPin = 27
SCount = 0
dbusservice = None

def checkSensorPowerState():
value = 0
f = open("/sys/class/gpio/gpio"+str(SensorPowerPin)+"/value", "r+")
v = int(f.read())
if v != 1:
# Power on the sensors
logging.info("Power on the sensors on GPIO "+str(SensorPowerPin))
f.write("1")
f.flush()
os.fsync(f.fileno())
return True
f.close()
return False

def toggleSensorPowerState():
logging.info("Power down the sensors on GPIO "+str(SensorPowerPin))
f = open("/sys/class/gpio/gpio"+str(SensorPowerPin)+"/value", "r+")
f.write("0")
f.flush()
os.fsync(f.fileno())
f.close()

def update():
# Calls to update ADC and I2C interfaces have been commented out
# The is in case someone runes this who does not know what they are doing
Expand Down Expand Up @@ -150,7 +173,11 @@ def update_rpi():
#update W1 temp
def update_W1():
#check, create and update 1 Wire devices
if checkSensorPowerState():
return

foundSensors = False

#read list of slaves
if os.path.isfile('/sys/devices/w1_bus_master1/w1_master_slaves'):
fd = open('/sys/devices/w1_bus_master1/w1_master_slaves','r')
Expand All @@ -165,6 +192,7 @@ def update_W1():

#DS18B20 Temp Sensors
if familyID == '28':
foundSensors = True
if ('W1-temp:'+ id) not in dbusservice:
logging.info("1Wire Sensor found with no Service -> Create:")

Expand All @@ -190,6 +218,10 @@ def update_W1():

dbusservice['W1-temp:'+ id]['/Temperature'] = value

# Power cycle the sensors...
if not foundSensors:
toggleSensorPowerState()

#Check 1 Wire Service Connection
for item in dbusservice:
logging.debug("Search for 1Wire Service Current Service: " + item)
Expand Down Expand Up @@ -401,9 +433,9 @@ def new_service(base, type, physical, logical, id, instance, settingId = False):
#dbusservice['cpu-temp']['/ProductName'] = 'Raspberry Pi'

# Persistent settings obejects in settingsDevice will not exist before this is executed
initSettings(newSettings)
# initSettings(newSettings)
# Do something to read the saved settings and apply them to the objects
readSettings(settingObjects)
# readSettings(settingObjects)

# Do a first update so that all the readings appear.
update()
Expand Down

0 comments on commit 1caeba8

Please sign in to comment.