Skip to content

Commit

Permalink
stabilizing against GPIO failures in RPi5
Browse files Browse the repository at this point in the history
  • Loading branch information
outdoorbits committed Dec 29, 2023
1 parent f2d3f13 commit 5f836bf
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 50 deletions.
68 changes: 37 additions & 31 deletions scripts/displaymenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lib_language
import lib_network
import lib_storage
import lib_system


class menu(object):
Expand All @@ -41,6 +42,8 @@ def __init__(self,DISPLAY_LINES,setup):

self.WORKING_DIR = os.path.dirname(__file__)

self.board_model_number = lib_system.get_pi_model(number_only=True)

self.const_MEDIA_DIR = self.__setup.get_val('const_MEDIA_DIR')
self.conf_DISP_FRAME_TIME = self.__setup.get_val('conf_DISP_FRAME_TIME')
self.conf_RSYNC_SERVER = self.__setup.get_val('conf_RSYNC_SERVER')
Expand Down Expand Up @@ -225,37 +228,40 @@ def __init__(self,DISPLAY_LINES,setup):
self.GPIO_init()

def GPIO_init(self):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

if self.conf_MENU_BUTTON_COMBINATION:
if self.conf_MENU_BUTTON_COMBINATION.isnumeric():
ButtonsConfigFile = f"{self.WORKING_DIR}/{self.const_BUTTONS_CONFIG_FILE}"
ButtonCombinationNumber = int(self.conf_MENU_BUTTON_COMBINATION)
elif self.conf_MENU_BUTTON_COMBINATION[0:1] == 'c':
ButtonsConfigFile = f"{self.const_MEDIA_DIR}/{self.const_BUTTONS_PRIVATE_CONFIG_FILE}"
ButtonCombinationNumber = int(self.conf_MENU_BUTTON_COMBINATION[1:])

if os.path.isfile(ButtonsConfigFile):
ConfigLines = []
with open(ButtonsConfigFile,'r') as f:
ConfigLines = f.readlines()

ConfigLineNumber = 0
for ConfigLine in ConfigLines:
ConfigLine = ConfigLine.strip()
if ConfigLine:
if ConfigLine[0:1] != '#':
ConfigLineNumber += 1
if ConfigLineNumber == ButtonCombinationNumber:
ConfigLine = ConfigLine.split(':',1)[0]
ButtonDefs = ConfigLine.split(',')
for ButtonDef in ButtonDefs:
GPIO_PIN, ButtonFunction = ButtonDef.split('=')
GPIO_PIN = int(GPIO_PIN)

self.GPIO_config_button(GPIO_PIN,ButtonFunction)
self.buttonevent_timestamp[GPIO_PIN] = 0
if self.board_model_number == 5:
pass
else:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

if self.conf_MENU_BUTTON_COMBINATION:
if self.conf_MENU_BUTTON_COMBINATION.isnumeric():
ButtonsConfigFile = f"{self.WORKING_DIR}/{self.const_BUTTONS_CONFIG_FILE}"
ButtonCombinationNumber = int(self.conf_MENU_BUTTON_COMBINATION)
elif self.conf_MENU_BUTTON_COMBINATION[0:1] == 'c':
ButtonsConfigFile = f"{self.const_MEDIA_DIR}/{self.const_BUTTONS_PRIVATE_CONFIG_FILE}"
ButtonCombinationNumber = int(self.conf_MENU_BUTTON_COMBINATION[1:])

if os.path.isfile(ButtonsConfigFile):
ConfigLines = []
with open(ButtonsConfigFile,'r') as f:
ConfigLines = f.readlines()

ConfigLineNumber = 0
for ConfigLine in ConfigLines:
ConfigLine = ConfigLine.strip()
if ConfigLine:
if ConfigLine[0:1] != '#':
ConfigLineNumber += 1
if ConfigLineNumber == ButtonCombinationNumber:
ConfigLine = ConfigLine.split(':',1)[0]
ButtonDefs = ConfigLine.split(',')
for ButtonDef in ButtonDefs:
GPIO_PIN, ButtonFunction = ButtonDef.split('=')
GPIO_PIN = int(GPIO_PIN)

self.GPIO_config_button(GPIO_PIN,ButtonFunction)
self.buttonevent_timestamp[GPIO_PIN] = 0

def GPIO_config_button(self,GPIO_PIN,ButtonFunction):
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down = self.GPIO_MENU_BUTTON_RESISTOR_PULL)
Expand Down
4 changes: 3 additions & 1 deletion scripts/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"camera_identifier": "Identifikation",
"camera_model": "Kamera-Modell",
"camera_serial": "Seriennummer",
"cameras": "Kameras/Smartphones"
"cameras": "Kameras/Smartphones",
"system": "System",
"model": "Modell"
},
"config": {
"config": "Einstellungen",
Expand Down
4 changes: 3 additions & 1 deletion scripts/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@
"camera_identifier": "identification",
"camera_model": "Camera model",
"camera_serial": "Serial number",
"cameras": "Cameras/smartphones"
"cameras": "Cameras/smartphones",
"system": "System",
"model": "Model"
},
"sysconditions": {
"under_voltage_cur": "The voltage is currently too low.",
Expand Down
4 changes: 3 additions & 1 deletion scripts/lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"camera_identifier": "Identificación",
"camera_model": "Modelo de cámara",
"camera_serial": "Número de serie",
"cameras": "Cámaras/teléfonos inteligentes"
"cameras": "Cámaras/teléfonos inteligentes",
"system": "Sistema",
"model": "Modelo"
},
"config": {
"config": "Ajustes",
Expand Down
4 changes: 3 additions & 1 deletion scripts/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"camera_identifier": "Identifiant",
"camera_model": "Modèle de caméra",
"camera_serial": "Numéro de série",
"cameras": "Appareils photo/smartphones"
"cameras": "Appareils photo/smartphones",
"system": "Système",
"model": "Modèle"
},
"config": {
"config": "Réglages",
Expand Down
38 changes: 38 additions & 0 deletions scripts/lib_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,49 @@ def get_abnormal_system_conditions(lan):

return(Conditions)

def get_pi_model(number_only=False):

model = ''

try:
with open('/proc/device-tree/model') as f:
model = f.read()
except:
model = 'unknown'

if number_only:
if model.startswith('Raspberry Pi Model B'):
model = 1

elif model.startswith('Raspberry Pi Zero'):
model = 0

elif model.startswith('Raspberry Pi 2'):
model = 2

elif model.startswith('Raspberry Pi 3'):
model = 3

elif model.startswith('Raspberry Pi 4'):
model = 4

elif model.startswith('Raspberry Pi 5'):
model = 5

else:
model = -1

return(model)

if __name__ == "__main__":
if len(sys.argv)>1:
if sys.argv[1] == 'get_uptime_sec':
print(get_uptime_sec())

if sys.argv[1] == 'get_abnormal_system_conditions':
lan = lib_language.language()
print(get_abnormal_system_conditions(lan))

if sys.argv[1] == 'get_pi_model':
print(get_pi_model())

38 changes: 23 additions & 15 deletions scripts/sysinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,44 @@
<h1 class="text-center" style="margin-bottom: 1em; letter-spacing: 3px;"><?php echo L::sysinfo_sysinfo; ?></h1>

<div class="card">
<h3><?php echo L::sysinfo_system; ?></h3>
<?php
$temp = shell_exec('cat /sys/class/thermal/thermal_zone*/temp');
$temp = round((float) $temp / 1000, 1);
$model = shell_exec("sudo python3 ${WORKING_DIR}/lib_system.py get_pi_model");

$cpuusage = 100 - (float) shell_exec("vmstat | tail -1 | awk '{print $15}'");
$temp = shell_exec('cat /sys/class/thermal/thermal_zone*/temp');
$temp = round((float) $temp / 1000, 1);

$mem_ram_frac = shell_exec("free | grep Mem | awk '{print $3/$2 * 100.0}'");
$mem_ram_all = shell_exec("free | grep Mem | awk '{print $2 / 1024}'");
$mem_ram = round((float) $mem_ram_frac, 1) . " % * " . round((float) $mem_ram_all) . " MB";
$cpuusage = 100 - (float) shell_exec("vmstat | tail -1 | awk '{print $15}'");

$mem_swap_frac = shell_exec("free | grep Swap | awk '{print $3/$2 * 100.0}'");
$mem_swap_all = shell_exec("free | grep Swap | awk '{print $2 / 1024}'");
$mem_swap = round($mem_swap_frac, 1) . " % * " . round($mem_swap_all) . " MB";
$mem_ram_frac = shell_exec("free | grep Mem | awk '{print $3/$2 * 100.0}'");
$mem_ram_all = shell_exec("free | grep Mem | awk '{print $2 / 1024}'");
$mem_ram = round((float) $mem_ram_frac, 1) . " % * " . round((float) $mem_ram_all) . " MB";

$abnormal_conditions = shell_exec("sudo python3 ${WORKING_DIR}/lib_system.py get_abnormal_system_conditions");
$mem_swap_frac = shell_exec("free | grep Swap | awk '{print $3/$2 * 100.0}'");
$mem_swap_all = shell_exec("free | grep Swap | awk '{print $2 / 1024}'");
$mem_swap = round($mem_swap_frac, 1) . " % * " . round($mem_swap_all) . " MB";

$abnormal_conditions = shell_exec("sudo python3 ${WORKING_DIR}/lib_system.py get_abnormal_system_conditions");

echo '<table>';

echo "<tr><td width='30%'>" . L::sysinfo_model . ": </td><td><strong>" . $model . "</strong></td></tr>";

if (isset($temp) && is_numeric($temp)) {
echo "<p>" . L::sysinfo_temp . ": <strong>" . $temp . "°C</strong></p>";
echo "<tr><td>" . L::sysinfo_temp . ": </td><td><strong>" . $temp . "°C</strong></td></tr>";
}

if (isset($cpuusage) && is_numeric($cpuusage)) {
echo "<p>" . L::sysinfo_cpuload . ": <strong>" . $cpuusage . "%</strong></p>";
echo "<tr><td>" . L::sysinfo_cpuload . ": </td><td><strong>" . $cpuusage . "%</strong></td></tr>";
}

echo "<p>" . L::sysinfo_memory_ram . ": <strong>" . $mem_ram . "</strong></p>";
echo "<tr><td>" . L::sysinfo_memory_ram . ": </td><td><strong>" . $mem_ram . "</strong></td></tr>";

echo "<p>" . L::sysinfo_memory_swap . ": <strong>" . $mem_swap . "</strong></p>";
echo "<tr><td>" . L::sysinfo_memory_swap . ": </td><td><strong>" . $mem_swap . "</strong></td></tr>";

echo "<p>" . L::sysinfo_conditions . ": <strong>" . $abnormal_conditions . "</strong></p>";
echo "<tr><td>" . L::sysinfo_conditions . ": </td><td><strong>" . $abnormal_conditions . "</strong></td></tr>";

echo '</table>';
?>
</div>

Expand Down

0 comments on commit 5f836bf

Please sign in to comment.