Raspberry als 'remote' Komponente von IPS mit Webcam, Wassersteuerung und 3G/2G Anbin

Hallo zusammen,

wollte Euch mal mein aktuellstes Projekt vorstellen.

Da ich ein Gartenhäuschen ohne Strom und ‚Internet‘-Anbindung etwas automatisieren möchte, habe ich folgendes Konzept entwickelt:

1.Das Häuschen bekommt eine ordentlich dimensionierte PV Analge mit Akku der den Raspberry mit Strom versorgt.

  1. An dem Raspberry hängen 1-Wire Temperatur-Sensoren, 4 Relays und eine Webcam.

  2. Bewegungserkennung mit dem Raspberry und Meldung an IPS, GPIOS (Relais) über IP-Symcon schalten und Temperatur auslesen ebenfalls.

  3. Das ganze natürlich sicher über 3G / 2G angebunden.

In meiner Laborumgebung läuft das alles super bisher.
Für Interessenten und Mitbastler gibt’s hier alles dokumentiert:

Installation Raspberry:
http://4-host.de/se/eine-raspberry-grundinstallation-mit-php-und-apache/

Raspberry Watchdog
http://4-host.de/se/raspberry-watchdog/

Webcam mit Bewegungserkennung
http://4-host.de/se/webcam-mit-bewegungserkennung-raspberry-mit-motion/

USB-Stick für Speicherauslagerung
http://4-host.de/se/raspberry-usb-stick-als-speicher-verwenden/

3G/2G Surfstick
http://4-host.de/se/raspberry-mit-usb-surfstick-3g-2g-dauerhaft-ins-internet/

Remote GPIOs schlaten mit PHP Interface
http://4-host.de/se/raspberry-gpios-mit-php-schalten/

Raspberry Reverse SSH Tunnel
http://4-host.de/se/raspberry-reverse-ssh-tunnel/

Auf der IPS-Seite dann folgendes:
Dummy-Instanz
-> Variable AN/AUS für GPIO
-> Script für AN/AUS und Pull-abfrage

IPS-Script:


<?
	$C["HOST"]     = "localhost:11080"; //SSH Tunnel zu Remote Raspberry
	$C["POLLING"]  = 600; // Seconds for plling

	$C["MAPVARGPIO"] = Array(
                      35191 => 17,
	                  );

	$urlsw = "http://".$C["HOST"]."/control/?mode=SWGPIO";
	$urlrq = "http://".$C["HOST"]."/control/?mode=GETGPIO";

// iF TRIGGER by Variable
	if(!empty($_IPS['VARIABLE'])){
	   $value = ($_IPS['VALUE'])? 1 : 0;
	   $gpio  = 	$C["MAPVARGPIO"][$_IPS["VARIABLE"]];
		$ret  = file_get_contents($urlsw."&id=$gpio&on=$value");
		$ret  = json_decode($ret);
		if($ret->STATUS == "OK"){
			SetValue($_IPS["VARIABLE"], $_IPS['VALUE']);
		}
	 return;
	}
	
// Update Status
 if($_IPS['SENDER'] = "Execute" || $_IPS['SENDER'] = "WebFront" ){
   foreach ($C["MAPVARGPIO"] as $varid => $gpio){
      unset($ret);
      $ret  = file_get_contents($urlrq."&id=$gpio");
		$ret  = json_decode($ret);
		if($ret->STATUS == "OK"){
			$val = ($ret->MSG == 1)? true : false;
			SetValue($varid, $val);
			print 'Statusupdate for GPIO'.$gpio.': '.$ret->STATUS.", Value: ".$val;
		}

   }

 }
 IPS_SetScriptTimer($_IPS['SELF'],$C["POLLING"]);
?>

Am Raspberry läuft dann folgendes Script in /var/www/control/index.php


<?php
  // Valid GPIOS
  $C["GPIOS"] = Array(17,22);
  
  $mode = @$_GET["mode"];
  switch($mode){
    case "SWGPIO":  $on = intval(@$_GET["on"]);
                    $id  = intval(@$_GET["id"]); 
                    SwitchGPIO($id, $on);
                    break;

    case "GETGPIO": $id  = intval(@$_GET["id"]);
                    GetGPIO($id);
                    break;                    
  }

function GetGPIO($id){
  global $C;
  if(!in_array($id, $C["GPIOS"])){
    Status(false, "GPIO $id not allowed.");
  }
 if(!is_dir("/sys/class/gpio/gpio$id/")){
    Status(false,"GPIO $id not active.");
 }
 $fn = "/sys/class/gpio/gpio$id/value"; 
 if(!file_exists($fn)){
  Status(false, "GPIO $id Valuefile does not exist.");
 }
 $val = trim(file_get_contents($fn));
 Status(true,$val);
}

function SwitchGPIO($id, $on){
   // Vorher beim Booten Berechtigungen setzen!
   // sudo usermod -aG sudo usermod -aG gpio www-data

  global $C;
  if(!in_array($id, $C["GPIOS"])){
    Status(false, "GPIO $id not allowed.");
  }
  if(!in_array($on, Array(0,1))){
    Status(false, "GPIO status $on not allowed.");
  }

  // check if GPIO is open
  if(!is_dir("/sys/class/gpio/gpio$id/")){
   file_put_contents("/sys/class/gpio/export",$id);
   sleep(1);
   file_put_contents("/sys/class/gpio/gpio$id/direction","out");
  }
  if(!is_dir("/sys/class/gpio/gpio$id/")){
     Status(false,"Cannot access GPIO $id");
  }

  // Set correct status
  $fn = '/sys/class/gpio/gpio'.$id.'/value';
  $val = file_get_contents($fn);
  if($val == $on){
    Status(true, "Switch status already $on");
  }

  file_put_contents($fn,$on);
  usleep(250000);

  // Check status
  $val = file_get_contents($fn);
  if($val == $on){
    Status(true, "Switch status now: $on");
  } else {
    Status(false, "Switch status wrong");
  }
}


Status(false,"Wrong Parameters");

function Status($stat, $txt){
  $ret["STATUS"] = ($stat)? "OK" : "ERROR";
  $ret["MSG"]    = $txt;
  echo json_encode ($ret);
  die; 
}
  
function warning_handler($errno, $errstr) { 
  echo $errno;
  return false;
}  
?>


Mit dem neuen IPS könnte man das über die JSONRPC Schnittstelle noch etwas besser hinbekommen.