Skip to content
Permalink
Browse files

NEMS GPIO Extender Server active

  • Loading branch information...
NEMS Linux
NEMS Linux committed Jul 5, 2019
1 parent ce2abb2 commit f7209e678aeb6b726895da3ec57df197c00a5bbe
Showing with 76 additions and 5 deletions.
  1. +43 −0 gpio-extender/gpioe-client
  2. +26 −0 gpio-extender/gpioe-server
  3. +7 −5 warninglight
@@ -0,0 +1,43 @@
#!/usr/bin/env php
<?php
// This script reads json pin state data from a NEMS Linux server
// running the NEMS Tools GPIO Extender Server on port 9595.
if (file_exists('/usr/local/bin/gpio')) {
$hardware=1;
} else {
$hardware=0;
}
// Can be an IP address or FQDN
$serverIP = '127.0.0.1'; // to be overridden with config file later
$url = $serverIP . ':9595'; // need to specify the port of the GPIOE Server
if ($serverIP == '127.0.0.1' || $serverIP == trim(shell_exec('/usr/local/bin/nems-info ip'))) {
// You can't run the client on the same server as the server... you'd then have two GPIO daemons controlling the GPIO
die('Cannot run a NEMS Tools GPIO Extender Client on this server.' . PHP_EOL . 'You need a NEMS Tools client device. Please read the docs.' . PHP_EOL);
}
while (1 == 1) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$GPIO = json_decode($data);
if (json_last_error() === 0) {
echo 'Connection successful.';
if ($hardware == 0) {
die(' However, no Raspberry Pi GPIO detected.' . PHP_EOL);
}
echo PHP_EOL;
foreach ($GPIO as $pin=>$state) {
if ($hardware != 0) shell_exec('/usr/local/bin/gpio -g write ' . $pin . ' ' . $state);
}
} else {
echo 'Could not find a running NEMS Tools GPIO Extender Server at ' . $serverIP . PHP_EOL;
exit();
}
sleep(10); // Refresh every 10 seconds
}
?>
@@ -0,0 +1,26 @@
#!/bin/bash

# Check if a PID exists. If yes, check if it is running and abort if yes.
if [[ -e /var/run/nems-gpioe-server.pid ]]; then
running=$(cat /var/run/nems-gpioe-server.pid)
if ps -p $running > /dev/null
then
echo "Already running"
exit
fi
fi

# Store the PID (so you can more easily kill the process)
echo $$ > /var/run/nems-gpioe-server.pid

# Turn on all lights while server is starting
echo '{"24":1,"23":1,"18":1}' > /var/log/nems/nems-tools/gpio-extender.json

# Install socat if not already installed
if [[ ! `which socat` ]]; then
apt update
yes | apt install socat
fi

# Load the server on port 9595
socat -T 1 -d tcp-l:9595,reuseaddr,fork,crlf system:"cat /var/log/nems/nems-tools/gpio-extender.json";
@@ -51,6 +51,7 @@ file_put_contents('/var/run/warninglight.pid',$pid);
// Setup GPIO
echo '[' . date('Y-m-d g:i:sa') . '] Initializing warninglight... ';
$GPIO = new stdClass();
$GPIO->pins = array('ok'=>24,'unknown'=>23,'warning'=>23,'critical'=>18,'siren'=>18);
if (file_exists('/usr/local/bin/gpio')) {
$hardware=1;
} else {
@@ -59,7 +60,6 @@ file_put_contents('/var/run/warninglight.pid',$pid);
if ($platform >= 0 || $platfrom <= 9) {
// Raspberry Pi
if ($hardware == 1) {
$GPIO->pins = array('ok'=>24,'unknown'=>23,'warning'=>23,'critical'=>18,'siren'=>18);
foreach ($GPIO->pins as $status => $pin) {
shell_exec('/usr/local/bin/gpio -g mode ' . $pin . ' output');
}
@@ -88,7 +88,7 @@ file_put_contents('/var/run/warninglight.pid',$pid);
lightOn('unknown',1000);
echo 'Done.';
} else {
echo 'Cannot initialize hardware (only supported on Pi).';
echo 'Cannot initialize local hardware. GPIO Extender enabled.';
}
} else {
echo 'Daemon loaded, but hardware is not compatible with this SBC.' . PHP_EOL;
@@ -289,16 +289,18 @@ function setstate($state) {
function lightOn($thisstatus,$delay=100000) {
global $GPIO,$hardware;
if ($hardware==0) return;
$chosenpin = '';
foreach ($GPIO->pins as $status=>$pin) {
if ($status == $thisstatus || $thisstatus == 'all') {
shell_exec('/usr/local/bin/gpio -g write ' . $pin . ' 1');
if ($hardware != 0) shell_exec('/usr/local/bin/gpio -g write ' . $pin . ' 1');
$chosenpin = $pin;
$GPIO->state[$pin] = 1;
} elseif ($pin != $chosenpin) {
shell_exec('/usr/local/bin/gpio -g write ' . $pin . ' 0');
if ($hardware != 0) shell_exec('/usr/local/bin/gpio -g write ' . $pin . ' 0');
$GPIO->state[$pin] = 0;
}
}
file_put_contents('/var/log/nems/nems-tools/gpio-extender.json',json_encode($GPIO->state));
usleep($delay);
}

0 comments on commit f7209e6

Please sign in to comment.
You can’t perform that action at this time.