Nettemp + ESP Tasmota + MQTT

Awatar użytkownika

Topic author
philips
Posty: 23
Rejestracja: czw sie 15, 2019 10:45 am

Nettemp + ESP Tasmota + MQTT

#1

Post autor: philips » ndz sie 18, 2019 7:56 pm

Hej,

Pytanie :
Jak w ESP Tasmota ustawić wysyłanie do MQTT aby Nettemp mógł to czytać ?
Spróbowałem ustawień z EasyESP ale nie działa:
W Publish Template:

Kod: Zaznacz cały

/%ip%/%sysname%/%valname%
Subscribe Template:

Kod: Zaznacz cały

/%ip%/%sysname%/#
bo pojawia się takie:
Przechwytywanie vvv.JPG
Przechwytywanie vvv.JPG (44.11 KiB) Przejrzano 7378 razy
Próbowałem coś samemu rozkiminić z https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT ale chyba nie wiele z tego rozumiem.

Cel:
Chodzi o to by ustawić Nettemp przez MQTT tak by działało jednocześnie z Domoticzem na Tasmocie, da sie ?
A może da się jako przesyłać dane bezpośrednio z Domoticza do Nettempa ?

Wyjaśnienie:
Domoticz jest mi potrzebny do wyliczania punktu rosy i zaawansowanego sterownika temperatury w "Zdarzeniach" (tych puzlach) ale Nettempowych wykresów nic nie zastąpi - są po prostu obłędne. Dlatego nie chce rezygnować z Nettempa, wręcz wolałbym przejść na niego całkowicie gdyż jego szata graficzna jest dla mnie fajniejsza

uzi18
beta tester
Posty: 93
Rejestracja: czw lip 11, 2019 8:07 pm
Otrzymał  piwo: 3 razy

Re: Nettemp + ESP Tasmota + MQTT

#2

Post autor: uzi18 » ndz sie 18, 2019 8:30 pm

czemu nie espeasy?

Wysłane z mojego MI MAX przy użyciu Tapatalka

Awatar użytkownika

Topic author
philips
Posty: 23
Rejestracja: czw sie 15, 2019 10:45 am

Re: Nettemp + ESP Tasmota + MQTT

#3

Post autor: philips » ndz sie 18, 2019 8:44 pm

Bo mam kłopot z zainstalowaniem EasyESP na SonOFF RF R2 Power. Działa mi na razie jedynie tylko Tasmota.
Więcej na ten temat tu: http://nettemp.tk/viewtopic.php?f=9&t=41

uzi18
beta tester
Posty: 93
Rejestracja: czw lip 11, 2019 8:07 pm
Otrzymał  piwo: 3 razy

Re: Nettemp + ESP Tasmota + MQTT

#4

Post autor: uzi18 » ndz sie 18, 2019 9:20 pm

bo trzeba odpowiedni wsad wrzucic

Wysłane z mojego MI MAX przy użyciu Tapatalka

Awatar użytkownika

pasiekazj
Twórca oprogramowania
Posty: 227
Rejestracja: pn lip 01, 2019 12:08 pm
Otrzymał  piwo: 16 razy
Kontakt:

Re: Nettemp + ESP Tasmota + MQTT

#5

Post autor: pasiekazj » pn sie 19, 2019 8:33 am

philips pisze:
ndz sie 18, 2019 7:56 pm
Hej,

Pytanie :
Jak w ESP Tasmota ustawić wysyłanie do MQTT aby Nettemp mógł to czytać ?
Spróbowałem ustawień z EasyESP ale nie działa:
W Publish Template:

Kod: Zaznacz cały

/%ip%/%sysname%/%valname%
Subscribe Template:

Kod: Zaznacz cały

/%ip%/%sysname%/#
bo pojawia się takie: Przechwytywanie vvv.JPG
Próbowałem coś samemu rozkiminić z https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT ale chyba nie wiele z tego rozumiem.

Cel:
Chodzi o to by ustawić Nettemp przez MQTT tak by działało jednocześnie z Domoticzem na Tasmocie, da sie ?
A może da się jako przesyłać dane bezpośrednio z Domoticza do Nettempa ?

Wyjaśnienie:
Domoticz jest mi potrzebny do wyliczania punktu rosy i zaawansowanego sterownika temperatury w "Zdarzeniach" (tych puzlach) ale Nettempowych wykresów nic nie zastąpi - są po prostu obłędne. Dlatego nie chce rezygnować z Nettempa, wręcz wolałbym przejść na niego całkowicie gdyż jego szata graficzna jest dla mnie fajniejsza
Bo w tasmocie pewnie inaczej nazywają się zmienne.
Co do przesyłania danych z Domoticza do NT jak najbardziej się da. Mam tak zrobione u siebie. Ja mam zrobione to za pomocą skryptu w dzVents:

Kod: Zaznacz cały


return {
	on = {
		
		timer = {'every 1 minutes'}
	},
	execute = function(domoticz, device)
	    
	    local v11 = domoticz.utils.round(domoticz.devices('Łazienka duża').temperature,1)

	    local url = 'http://192.168.50.4/receiver.php?key=123456789&value=' .. v11 .. '&type=temp&rom=domo_laz_duz&device=ip&ip=192.168.50.2'
	    domoticz.openURL(url)
	    

	end
}
Nie jest to kod idealny bo zamiast nazwy można by użyć IDX ale działa poprawnie.
Rpi 4 + SSD 240 GB + nettemp.
http://mmsite.pl

uzi18
beta tester
Posty: 93
Rejestracja: czw lip 11, 2019 8:07 pm
Otrzymał  piwo: 3 razy

Re: Nettemp + ESP Tasmota + MQTT

#6

Post autor: uzi18 » pn sie 19, 2019 9:45 am

kiedys wam. proponowalem rozwiazanie, tzn nasluchiwanie na mqtt informacji wysylanych do / z domoticz i wrzucanie tego na mqtt nettempa, ale. najlepiej byloby dodac taka funkcje bezpośrednio w nettemp

Wysłane z mojego MI MAX przy użyciu Tapatalka

Awatar użytkownika

Topic author
philips
Posty: 23
Rejestracja: czw sie 15, 2019 10:45 am

Re: Nettemp + ESP Tasmota + MQTT

#7

Post autor: philips » pn sie 19, 2019 7:42 pm

pasiekazj : Dzięki - spróbuje tego rozwiązania, w zasadzie załatwiło by sprawę.
uzi18: Twoja propozycja była za to idealna.

PS. Sugeruje rozwiązanie z kodem wrzucić do oddzielnego wątku, jest bardzo cenne.

uzi18
beta tester
Posty: 93
Rejestracja: czw lip 11, 2019 8:07 pm
Otrzymał  piwo: 3 razy

Re: Nettemp + ESP Tasmota + MQTT

#8

Post autor: uzi18 » pn sie 19, 2019 9:26 pm

jak. reaktywuje moje rpi, to moze zrobie jakis skrypt do tego

Wysłane z mojego MI MAX przy użyciu Tapatalka


rejf
Posty: 5
Rejestracja: pt wrz 06, 2019 7:06 am
Otrzymał  piwo: 1 raz

Re: Nettemp + ESP Tasmota + MQTT

#9

Post autor: rejf » pn sty 20, 2020 2:17 pm

Ja tez korzystam z tasmota. Mam sonoff basic r3 i podłączone do niego DS18b20. Tak na szybko przerobiłem sobie skrypt mqtt. W mojej wersji czyta temperaturę z DS i stan przekaźnika.

/var/www/nettemp/modules/mqtt/subscribe.php

Kod: Zaznacz cały

<?php

/*

# IP IP/device/name/type
192.168.0.1/gpio/18

# localhost IP/device/addr/name/type
localhost/gpio/18/dht22/humid 
localhost/gpio/18/dht22/temp
localhost/i2c/55/BMP/temp
localhost/1wire/rom/temp

*/


$ROOT=(dirname(dirname(dirname(__FILE__))));
require("phpMQTT.php");
include("$ROOT/receiver.php");
define("LOCAL","local");
$date = date("Y-m-d H:i:s"); 


$server = "localhost";     // change if necessary
$port = 1883;                     // change if necessary
$username = "test";                   // set your username
$password = "test123";                   // set your password
$client_id = "phpMQTT-subscriber"; // make sure this is unique for connecting to sever - you could use uniqid()

$mqtt = new phpMQTT($server, $port, $client_id);

if(!$mqtt->connect(true, NULL, $username, $password)) {
	exit(1);
}

$topics['#'] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);

while($mqtt->proc()){
		
}

$mqtt->close();


function procmsg($topic, $msg){
		echo "Msg Recieved:\n";
		echo "Topic: {$topic}\n\n";
		echo "\t$msg\n\n";


    $ttp=(explode("/",$topic));
    foreach($ttp as $p) {
	$arr[]=$p;
    }

    global $date;

    global $local_rom;
    global $local_type;
    global $local_val;
    global $local_device;
    global $local_i2c;
    global $local_current;
    global $local_name;
    global $local_ip;
    global $local_gpio;
    global $local_usb;
	global $local_tskname;

    $output = trim($msg);

    $gpio='';
    $local_gpio='';
    
  if ($arr['3']=='gpio') {
		
	$ip=$arr['1'];
	$name=$arr['2'];
	$type=$arr['3'];
	$gpio=$arr['4'];
	$tskname=$arr['5'];
    
	$local_device	=	'ip_mqtt';
	$local_type	=	$type;
	$local_val	=	$output;
	$local_name	=	$name;
	$local_ip	=	$ip;
	$local_gpio	=	$gpio;
	$local_tskname = $tskname;
	$local_rom=$local_device."_".$local_name."_".$local_type."_".$local_gpio."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
  }
  elseif($arr['2']=='tasmota'){
 	
   if($arr['3']=='SENSOR'){
    
    if (strpos($output, 'DS18B20') !== false) {
      $output=str_replace('"','',$output); 
      preg_match('/DS18B20:{(.*)}/msU', $output, $ds18b20);
      preg_match('/Temperature:(.*)/', $ds18b20[1], $temp_ds18b20);
      preg_match('/Id:(.*),/', $ds18b20[1], $ds18b20_id);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="temp";
	   $tskname=$ds18b20_id[1];
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$temp_ds18b20[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    }
    
   }
   if($arr['3']=='RESULT'){
    if (strpos($output, 'POWER') !== false) {
    $output=str_replace('"','',$output); 
    preg_match('/POWER:(.*)}/msU', $output, $power);
    $ip=$arr['1'];
	  $name=$arr['2'];
	  $type="switch";
	  $tskname=$ip;
    $local_device	=	'ip_mqtt';
	  $local_type	=	$type;
	  if($power[1]=="ON") $local_val	=	1; elseif($power[1]=="OFF") $local_val	=	0; else $local_val	=	2; 
	  $local_name	=	$name;
	  $local_ip	=	$ip;
	  $local_tskname = $tskname;
	  $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    }
   }   
   
   }
   else {
	$ip=$arr['1'];
	$name=$arr['2'];
	$type=$arr['3'];
	$tskname=$arr['4'];
    
    $local_device	=	'ip_mqtt';
	$local_type	=	$type;
	$local_val	=	$output;
	$local_name	=	$name;
	$local_ip	=	$ip;
	$local_tskname = $tskname;
	$local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
    echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
    db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    
   
  }
}
?>

Po zmianie tego pliku trzeba:
1. w tasmota mqtt ustawić:
temat: (ip Twojego tasmota)
Pełny teamt: %prefix%/%topic%/tasmota
2. ręcznie zabić proces: /usr/bin/php -f /var/www/nettemp/modules/mqtt/subscribe.php

Teraz powinno działać prawidłowo

Ty masz SonOFF RF R2 Power więc będziesz musiał go dostosowac do siebie.
Jak nie ogarniesz to napisz co ma byc wyświetlene w netemp.

rejf
Posty: 5
Rejestracja: pt wrz 06, 2019 7:06 am
Otrzymał  piwo: 1 raz

Re: Nettemp + ESP Tasmota + MQTT

#10

Post autor: rejf » pn sty 20, 2020 2:28 pm

Coś tu Tobie naskrobałem....

@philips daj znać czy działa poprawnie, bo teoretycznie działa prawidłowo:

Kod: Zaznacz cały

<?php

/*

# IP IP/device/name/type
192.168.0.1/gpio/18

# localhost IP/device/addr/name/type
localhost/gpio/18/dht22/humid 
localhost/gpio/18/dht22/temp
localhost/i2c/55/BMP/temp
localhost/1wire/rom/temp

*/


$ROOT=(dirname(dirname(dirname(__FILE__))));
require("phpMQTT.php");
include("$ROOT/receiver.php");
define("LOCAL","local");
$date = date("Y-m-d H:i:s"); 


$server = "localhost";     // change if necessary
$port = 1883;                     // change if necessary
$username = "test";                   // set your username
$password = "test123";                   // set your password
$client_id = "phpMQTT-subscriber"; // make sure this is unique for connecting to sever - you could use uniqid()

$mqtt = new phpMQTT($server, $port, $client_id);

if(!$mqtt->connect(true, NULL, $username, $password)) {
	exit(1);
}

$topics['#'] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);

while($mqtt->proc()){
		
}

$mqtt->close();


function procmsg($topic, $msg){
		echo "Msg Recieved:\n";
		echo "Topic: {$topic}\n\n";
		echo "\t$msg\n\n";


    $ttp=(explode("/",$topic));
    foreach($ttp as $p) {
	$arr[]=$p;
    }

    global $date;

    global $local_rom;
    global $local_type;
    global $local_val;
    global $local_device;
    global $local_i2c;
    global $local_current;
    global $local_name;
    global $local_ip;
    global $local_gpio;
    global $local_usb;
	global $local_tskname;

    $output = trim($msg);

    $gpio='';
    $local_gpio='';
    
  if ($arr['3']=='gpio') {
		
	$ip=$arr['1'];
	$name=$arr['2'];
	$type=$arr['3'];
	$gpio=$arr['4'];
	$tskname=$arr['5'];
    
	$local_device	=	'ip_mqtt';
	$local_type	=	$type;
	$local_val	=	$output;
	$local_name	=	$name;
	$local_ip	=	$ip;
	$local_gpio	=	$gpio;
	$local_tskname = $tskname;
	$local_rom=$local_device."_".$local_name."_".$local_type."_".$local_gpio."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
  }
  elseif($arr['2']=='tasmota'){
 	
   if($arr['3']=='SENSOR'){
    
    if (strpos($output, 'DS18B20') !== false) {
      $output=str_replace('"','',$output); 
      preg_match('/DS18B20:{(.*)}/msU', $output, $ds18b20);
      preg_match('/Temperature:(.*)/', $ds18b20[1], $temp_ds18b20);
      preg_match('/Id:(.*),/', $ds18b20[1], $ds18b20_id);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="temp";
	   $tskname=$ds18b20_id[1];
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$temp_ds18b20[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    }
   if (strpos($output, 'ENERGY') !== false) {
      $output=str_replace('"','',$output); 
      preg_match('/ENERGY:{(.*)}/msU', $output, $energy);
      preg_match('/Total:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="kwatt";
	   $tskname="Total";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);    
    
      preg_match('/Yesterday:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="kwatt";
	   $tskname="Yesterday";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  

      preg_match('/Today:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="kwatt";
	   $tskname="Today";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name); 
                      
      preg_match('/Period:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Period";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  

      preg_match('/Power:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Power";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);   

      preg_match('/ApparentPower:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="ApparentPower";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);   

      preg_match('/ReactivePower:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="ReactivePower";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);   
      
      preg_match('/Factor:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Factor";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  
      
      preg_match('/Voltage:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="volt";
	   $tskname="Voltage";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  

      preg_match('/Current:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Current";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  
   } 
    
    
   }
   if($arr['3']=='RESULT'){
    if (strpos($output, 'POWER') !== false) {
    $output=str_replace('"','',$output); 
    preg_match('/POWER:(.*)}/msU', $output, $power);
    $ip=$arr['1'];
	  $name=$arr['2'];
	  $type="switch";
	  $tskname=$ip;
    $local_device	=	'ip_mqtt';
	  $local_type	=	$type;
	  if($power[1]=="ON") $local_val	=	1; elseif($power[1]=="OFF") $local_val	=	0; else $local_val	=	2; 
	  $local_name	=	$name;
	  $local_ip	=	$ip;
	  $local_tskname = $tskname;
	  $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    }
   }   
   
   }
   else {
	$ip=$arr['1'];
	$name=$arr['2'];
	$type=$arr['3'];
	$tskname=$arr['4'];
    
    $local_device	=	'ip_mqtt';
	$local_type	=	$type;
	$local_val	=	$output;
	$local_name	=	$name;
	$local_ip	=	$ip;
	$local_tskname = $tskname;
	$local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
    echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
    db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    
   
  }
}
?>

rejf
Posty: 5
Rejestracja: pt wrz 06, 2019 7:06 am
Otrzymał  piwo: 1 raz

Re: Nettemp + ESP Tasmota + MQTT

#11

Post autor: rejf » śr lut 26, 2020 1:28 pm

Istnieje możliwość wprowadzenia zmian do 3 plików(oczywiście po sprawdzeniu jeszcze przez kogoś), aby nettemp gadał z tasmota?
Ja testowałem na sonoff basic r3 i u mnie wszystko działa poprawnie.. Czyta temperaturę i załącza/wyłącza przekaźnik.

/var/www/nettemp/modules/mqtt/subscribe.php

Kod: Zaznacz cały

<?php

/*

# IP IP/device/name/type
192.168.0.1/gpio/18

# localhost IP/device/addr/name/type
localhost/gpio/18/dht22/humid 
localhost/gpio/18/dht22/temp
localhost/i2c/55/BMP/temp
localhost/1wire/rom/temp

*/


$ROOT=(dirname(dirname(dirname(__FILE__))));
require("phpMQTT.php");
include("$ROOT/receiver.php");
define("LOCAL","local");
$date = date("Y-m-d H:i:s"); 


$server = "localhost";     // change if necessary
$port = 1883;                     // change if necessary
$username = "test";                   // set your username
$password = "test123";                   // set your password
$client_id = "phpMQTT-subscriber"; // make sure this is unique for connecting to sever - you could use uniqid()

$mqtt = new phpMQTT($server, $port, $client_id);

if(!$mqtt->connect(true, NULL, $username, $password)) {
	exit(1);
}

$topics['#'] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);

while($mqtt->proc()){
		
}

$mqtt->close();


function procmsg($topic, $msg){
		echo "Msg Recieved:\n";
		echo "Topic: {$topic}\n\n";
		echo "\t$msg\n\n";


    $ttp=(explode("/",$topic));
    foreach($ttp as $p) {
	$arr[]=$p;
    }

    global $date;

    global $local_rom;
    global $local_type;
    global $local_val;
    global $local_device;
    global $local_i2c;
    global $local_current;
    global $local_name;
    global $local_ip;
    global $local_gpio;
    global $local_usb;
	global $local_tskname;

    $output = trim($msg);

    $gpio='';
    $local_gpio='';
    
  if ($arr['3']=='gpio') {
		
	$ip=$arr['1'];
	$name=$arr['2'];
	$type=$arr['3'];
	$gpio=$arr['4'];
	$tskname=$arr['5'];
    
	$local_device	=	'ip_mqtt';
	$local_type	=	$type;
	$local_val	=	$output;
	$local_name	=	$name;
	$local_ip	=	$ip;
	$local_gpio	=	$gpio;
	$local_tskname = $tskname;
	$local_rom=$local_device."_".$local_name."_".$local_type."_".$local_gpio."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
  }
  elseif($arr['2']=='tasmota'){
 	
   if($arr['3']=='SENSOR'){
    
    if (strpos($output, 'DS18B20') !== false) {
      $output=str_replace('"','',$output); 
      preg_match('/DS18B20:{(.*)}/msU', $output, $ds18b20);
      preg_match('/Temperature:(.*)/', $ds18b20[1], $temp_ds18b20);
      preg_match('/Id:(.*),/', $ds18b20[1], $ds18b20_id);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="temp";
	   $tskname=$ds18b20_id[1];
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$temp_ds18b20[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    }
   if (strpos($output, 'ENERGY') !== false) {
      $output=str_replace('"','',$output); 
      preg_match('/ENERGY:{(.*)}/msU', $output, $energy);
      preg_match('/Total:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="kwatt";
	   $tskname="Total";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);    
    
      preg_match('/Yesterday:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="kwatt";
	   $tskname="Yesterday";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  

      preg_match('/Today:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="kwatt";
	   $tskname="Today";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name); 
                      
      preg_match('/Period:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Period";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  

      preg_match('/Power:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Power";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);   

      preg_match('/ApparentPower:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="ApparentPower";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);   

      preg_match('/ReactivePower:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="ReactivePower";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);   
      
      preg_match('/Factor:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Factor";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  
      
      preg_match('/Voltage:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="volt";
	   $tskname="Voltage";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  

      preg_match('/Current:(.*),/msU', $energy[1], $va);
      $ip=$arr['1'];
	   $name=$arr['2'];
	   $type="watt";
	   $tskname="Current";
     $local_device	=	'ip_mqtt';
	   $local_type	=	$type;
	   $local_val	=	$va[1];
	   $local_name	=	$name;
	   $local_ip	=	$ip;
	   $local_tskname = $tskname;
	   $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);  
   } 
    
    
   }
   if($arr['3']=='RESULT' OR $arr['3']=='STATE'){
    if (strpos($output, 'POWER') !== false) {
    $output=str_replace('"','',$output); 
    if($arr['3']=='STATE') preg_match('/POWER:(.*),/msU', $output, $power); else preg_match('/POWER:(.*)}/msU', $output, $power);
    $ip=$arr['1'];
	  $name=$arr['2'];
	  //$type="switch";
    $type="relay";
	  $tskname=$ip;
    $local_device	=	'ip_mqtt';
	  $local_type	=	$type;
	  if($power[1]=="ON") $local_val	=	1; elseif($power[1]=="OFF") $local_val	=	0; else $local_val	=	2; 
	  $local_name	=	$name;
	  $local_ip	=	$ip;
	  $local_tskname = $tskname;
	  $local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
      echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
      db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    }
   }
 
   
   }
   else {
	$ip=$arr['1'];
	$name=$arr['2'];
	$type=$arr['3'];
	$tskname=$arr['4'];
    
    $local_device	=	'ip_mqtt';
	$local_type	=	$type;
	$local_val	=	$output;
	$local_name	=	$name;
	$local_ip	=	$ip;
	$local_tskname = $tskname;
	$local_rom=$local_device."_".$local_name."_".$local_type."_".$local_tskname;
    echo $date." Rom:".$local_rom." Name: ".$local_name." Value: ".$output." IP: ".$local_ip." GPIO: ".$local_gpio."\n".$output."\n";
    db($local_rom,$local_val,$local_type,$local_device,$local_current,$local_ip,$local_gpio,$local_i2c,$local_usb,$local_name);
    
   
  }
}
?>

/var/www/nettemp/modules/relays/html/relays_controls.php

Kod: Zaznacz cały

<?php 
$user = isset($_SESSION["user"]) ? $_SESSION["user"] : '';
$perms = isset($_SESSION["perms"]) ? $_SESSION["perms"] : '';
if( $user == 'admin'){

?>

<?php
$db = new PDO('sqlite:dbf/nettemp.db');
$ip = isset($_POST['ip']) ? $_POST['ip'] : '';
$relay = isset($_POST['relay']) ? $_POST['relay'] : '';
$rom = isset($_POST['rom']) ? $_POST['rom'] : '';
$ronoff = isset($_POST['ronoff']) ? $_POST['ronoff'] : '';
if (($ronoff == "ronoff")){
    if ($relay == 'on' ){
		$ch = curl_init();
    if(strpos($rom,"tasmota") == true){
		$optArray = array(
			CURLOPT_URL => "$ip/cm?cmnd=Power%20On",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_CONNECTTIMEOUT => 1,
			CURLOPT_TIMEOUT => 3
		);
    }else{
		$optArray = array(
			CURLOPT_URL => "$ip/seton",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_CONNECTTIMEOUT => 1,
			CURLOPT_TIMEOUT => 3
		);    
    }
		curl_setopt_array($ch, $optArray);
		$res = curl_exec($ch);
		$dbf = new PDO("sqlite:db/$rom.sql");
		$dbf->exec("INSERT OR IGNORE INTO def (value) VALUES ('1')");
		$db->exec("UPDATE sensors SET tmp='1' WHERE rom='$rom'");

     } else { 
		$ch = curl_init();
    if(strpos($rom,"tasmota") == true){
		$optArray = array(
			CURLOPT_URL => "$ip/cm?cmnd=Power%20off",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_CONNECTTIMEOUT => 1,
			CURLOPT_TIMEOUT => 3
		);
    }else{
		$optArray = array(
			CURLOPT_URL => "$ip/setoff",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_CONNECTTIMEOUT => 1,
			CURLOPT_TIMEOUT => 3
		);    
    }    
		curl_setopt_array($ch, $optArray);
		$res = curl_exec($ch);
		$dbf = new PDO("sqlite:db/$rom.sql");
		$dbf->exec("INSERT OR IGNORE INTO def (value) VALUES ('0')");
		$db->exec("UPDATE sensors SET tmp='0' WHERE rom='$rom'");
	}
    header("location: " . $_SERVER['REQUEST_URI']);
    exit();
    }

$db = new PDO('sqlite:dbf/nettemp.db');
$sth2 = $db->prepare("SELECT * FROM sensors WHERE type='relay'");
$sth2->execute();
$result2 = $sth2->fetchAll();
foreach ( $result2 as $r) {
$ip=$r['ip'];

$ch = curl_init();
if(strpos($r['rom'],"tasmota") == true){
$optArray = array(
    CURLOPT_URL => "$ip/cm?cmnd=Power",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 1,
	CURLOPT_TIMEOUT => 3
);
}else{
$optArray = array(
    CURLOPT_URL => "$ip/showstatus",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 1,
	CURLOPT_TIMEOUT => 3
);
}
curl_setopt_array($ch, $optArray);
$res = curl_exec($ch);

if(strpos($r['rom'],"tasmota") == true){
preg_match('/":"(.*)"/msU', $res, $o1);
$obj = json_decode($res);
$o=strtolower(trim($obj->{'POWER'}));
}else{
$o1=str_replace('status', '', $res);
$o=strtolower(trim($o1));
}
?>


<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?php echo $r['name']; ?></h3>
</div>
<div class="panel-body">
    <form action="" method="post">
    <input type="checkbox"  data-toggle="toggle"  onchange="this.form.submit()" name="relay" value="<?php echo $o == 'on'  ? 'off' : 'on'; ?>" <?php echo $o == 'on' ? 'checked="checked"' : ''; ?>  />
    <input type="hidden" name="ip" value="<?php echo $r['ip']; ?>"/>
    <input type="hidden" name="rom" value="<?php echo $r['rom']; ?>"/>
    <input type="hidden" name="ronoff" value="ronoff" />
</form>
</div></div>
<?php
unset($i);
}

}
?>

/var/www/nettemp/status/controls.php

Kod: Zaznacz cały

<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
$root=$_SERVER["DOCUMENT_ROOT"];
if(isset($_SESSION['user'])){
	
//hide
	$hidegpio = isset($_POST['hidegpio']) ? $_POST['hidegpio'] : '';
	$hidegpiostate = isset($_POST['hidegpiostate']) ? $_POST['hidegpiostate'] : '';
	
	if (!empty($hidegpio) && $hidegpio == 'hidegpio'){
		if ($hidegpiostate == 'on') {$hidegpiostate = 'off';
		}elseif ($hidegpiostate == 'off') {$hidegpiostate = 'on';}
		
	$db = new PDO('sqlite:dbf/nettemp.db');
	$db->exec("UPDATE nt_settings SET value='$hidegpiostate' WHERE option='hide_gpio'") or die ($db->lastErrorMsg());
	header("location: " . $_SERVER['REQUEST_URI']);
	exit();
	 }	
	
$db = new PDO("sqlite:$root/dbf/nettemp.db");	
$hideg = $db->query("SELECT value FROM nt_settings WHERE option='hide_gpio'");
$hide_resg = $hideg->fetchAll();
foreach ($hide_resg as $hg) {
    $nts_hide_gpio=$hg['value'];
}	 
	
	
/* SWITCH EasyESP */
$db = new PDO("sqlite:$root/dbf/nettemp.db");
$sth = $db->prepare("SELECT ip,rom,gpio,name,tmp,status FROM sensors WHERE type='gpio' AND ch_group='gpio' ORDER BY position ASC");
$sth->execute();
$ip_gpio = $sth->fetchAll();

/* RELAYS  wireless, nodemcu */
$sth = $db->prepare("SELECT ip,rom,gpio,name,tmp FROM sensors WHERE type='relay' AND ch_group='relay'");
$sth->execute();
$sensors_relay = $sth->fetchAll();

/* Functions */

function label($status){
	if($status == 'on' || $status == 'ON' || $status=substr($status,0,2)== 'ON' )
	{	
		return 'label-success';
	} 
	elseif($status == 'off' || $status == 'OFF') 
	{
		return 'label-danger';
	}
	elseif($status == 'moment')
	{
		return 'label-default';
	}
	elseif($status == 'error')
	{
		return 'label-warning';
	}
	else 
	{
		return 'label-danger';
	}

}

      


function gpio_db($rom,$action){
	global $root;
	$dbf = new PDO("sqlite:$root/db/$rom.sql");
	$dbf->exec("INSERT OR IGNORE INTO def (value) VALUES ('$action')");
}

function gpio_status($rom,$tmp,$action,$gpio){
	global $root;
	$db = new PDO("sqlite:$root/dbf/nettemp.db");
	$db->exec("UPDATE sensors SET tmp='$tmp', status='$action' WHERE rom='$rom'");
	$db->exec("UPDATE gpio SET status='$action', simple='$action' WHERE rom='$rom'");
}

function gpio_curl_onoff($ip,$gpio,$rom,$action,$moment_time){
	
	if($action=='on') {
		$set='1';
		$tmp='1.0';
		$method='GPIO';
		$time='0';
	} elseif($action=='off') {
		$set='0';
		$tmp='0.0';
		$method='GPIO';
		$time='0';
	} elseif($action=='moment') {
		$method='Pulse';
		$set='1';
		$tmp='1.0';
	}
		
	$ch = curl_init();
	$optArray = array(
		CURLOPT_URL => "$ip/control?cmd=$method,$gpio,$set,$moment_time",
		CURLOPT_HEADER => true,
		CURLOPT_NOBODY => true,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_CONNECTTIMEOUT => 1,
		CURLOPT_TIMEOUT => 3
	);
	curl_setopt_array($ch, $optArray);
	$res = curl_exec($ch);
	
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
	if($httpcode == 200) {
		gpio_db($rom,$set);
		gpio_status($rom,$tmp,$action,$gpio);
	} else {
		gpio_status($rom,0,error,$gpio);
	}

}

function gpio_onoff($gpio,$rom,$action,$rev){
	
	if($action=='on'&&$rev=='on'){
		$set='0';
	} elseif ($action=='on'&&$rev==''){
		$set='1';
	} elseif ($action=='off'&&$rev=='on'){
		$set='1';
	} elseif ($action=='off'&&$rev==''){
		$set='0';
	}
	
	if ($gpio >= '100') {
		exec("/usr/local/bin/gpio -x mcp23017:$gpio:0x20 mode $gpio out");
		if ($rev == 'on') { 
			exec("/usr/local/bin/gpio -x mcp23017:$gpio:0x20 write $gpio $set");
		}
		else { 
			exec("/usr/local/bin/gpio -x mcp23017:$gpio:0x20 write $gpio $set");
		}
	} else {	
		exec("/usr/local/bin/gpio -g mode $gpio output");
		if ($rev == 'on') { 
			exec("/usr/local/bin/gpio -g write $gpio $set");	
		}
		else { 
			exec("/usr/local/bin/gpio -g write $gpio $set");	
		}
	}

	gpio_db($rom,$set);
	gpio_status($rom,$tmp,$action,$gpio);
}

function gpio_moment($gpio,$rom,$rev,$moment_time) {
	$tmp='';
	$action='moment';
	if ($rev == 'on') {
		$faction='0';
		$laction='1';
	} else {
		$faction='1';
		$laction='0';
	}
	$set='1';
	exec("/usr/local/bin/gpio -g mode $gpio output && /usr/local/bin/gpio -g write $gpio $faction && sleep $moment_time && /usr/local/bin/gpio -g write $gpio $laction");
	gpio_db($rom,$set);
	gpio_status($rom,$tmp,$action,$gpio);
}


/* END FUNCTIONS */

if(!empty($ip_gpio)||!empty($sensors_relay)) {
?>

<div class="grid-item swcon">
<div class="panel panel-default">
<div class="panel-heading">
<div class="pull-left"><a href="index.php?id=device&type=gpio" title="Go to GPIO settings" class="group-name" >GPIO</a></div>

<div class="pull-right">
		<div class="text-right">
			 <form action="" method="post" style="display:inline!important;">
					
					<input type="hidden" name="hidegpiostate" value="<?php echo $nts_hide_gpio; ?>" />
					<input type="hidden" name="hidegpio" value="hidegpio"/>
					<?php
					if($nts_hide_gpio =='off'){ ?>
					<button class="hidearrow"><span class="glyphicon glyphicon-triangle-top"></span> </button>
					<?php } elseif($nts_hide_gpio =='on'){?>
					<button class="hidearrow"><span class="glyphicon glyphicon-triangle-bottom"></span> </button>
					<?php } ?>
				</form>	
		</div>
  </div>
  <div class="clearfix"></div>

</div>
<table class="table table-hover table-condensed small" border="0">
	<tbody>
	<?php
if ($nts_hide_gpio == 'off') { 
	/* EasyESP */
	$numRows = count($ip_gpio);
	if ( $numRows > '0' ) { 
		/* FORMS */
		$ip_post = isset($_POST['ip']) ? $_POST['ip'] : '';
		$switch = isset($_POST['switch']) ? $_POST['switch'] : '';
		$rom_post = isset($_POST['rom']) ? $_POST['rom'] : '';
		$gpio_post = isset($_POST['gpio']) ? $_POST['gpio'] : '';
		$rev = isset($_POST['rev']) ? $_POST['rev'] : '';
		$onoff = isset($_POST['onoff']) ? $_POST['onoff'] : '';
		$moment_time = isset($_POST['moment_time']) ? $_POST['moment_time'] : '';
		$rom_lock = isset($_POST['rom_lock']) ? $_POST['rom_lock'] : '';
		$trun = isset($_POST['trun']) ? $_POST['trun'] : '';
		$time_offset = isset($_POST['time_offset']) ? $_POST['time_offset'] : '';
		
		
		/* SIMPLE IP */
		if ($onoff == "simpleip"){
			if ($switch == 'on' ){
				gpio_curl_onoff($ip_post,$gpio_post,$rom_post,'on','0');
			} else { 
				gpio_curl_onoff($ip_post,$gpio_post,$rom_post,'off','0');
			}
		$db->exec("UPDATE gpio SET locked='user' WHERE gpio='$gpio_post' AND rom='$rom_post'");
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
		/* MOMENT IP */
		if ($onoff == "momentip"){
			if($moment_time>10){
				$moment_time=10;
			}
			$moment_time=$moment_time*1000;	
			
			gpio_curl_onoff($ip_post,$gpio_post,$rom_post,'moment',$moment_time);
	
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
		
		/* TIME IP */
		if ($trun == "timerunip") {
			if ($switch == 'on' ){
				gpio_curl_onoff($ip_post,$gpio_post,$rom_post,'on','0');
				$date = new DateTime();
				$time_start=$date->getTimestamp();
				$db->exec("UPDATE gpio SET time_run='on', status='ON $time_offset min', time_offset='$time_offset',time_start='$time_start' WHERE gpio='$gpio_post' AND rom='$rom_post'") or die("exec error");
				header("location: " . $_SERVER['REQUEST_URI']);
				exit();
			} else {
				gpio_curl_onoff($ip_post,$gpio_post,$rom_post,'off','0');
				$date = new DateTime();
				$time_start=$date->getTimestamp();
				$db->exec("UPDATE gpio SET time_run='', time_start='', status='OFF' WHERE gpio='$gpio_post' AND rom='$rom_post'") or die("exec error");
				header("location: " . $_SERVER['REQUEST_URI']);
				exit();
			}
	
		}
		

		
        $value_update_from_status = isset($_POST['value_update_from_status']) ? $_POST['value_update_from_status'] : '';
		$id_value_update_from_status = isset($_POST['id_value_update_from_status']) ? $_POST['id_value_update_from_status'] : '';
		$update_from_status = isset($_POST['update_from_status']) ? $_POST['update_from_status'] : '';
		$time_update_from_status = isset($_POST['time_update_from_status']) ? $_POST['time_update_from_status'] : '';
		
		$lock_update_from_status = isset($_POST['lock_update_from_status']) ? $_POST['lock_update_from_status'] : '';
		$gpio_lock_update_from_status = isset($_POST['gpio_lock_update_from_status']) ? $_POST['gpio_lock_update_from_status'] : '';
		$rom_lock = isset($_POST['rom_lock']) ? $_POST['rom_lock'] : '';

		if($update_from_status=='switch_update_from_status') {
			$db->exec("UPDATE g_func SET value='$value_update_from_status' WHERE id='$id_value_update_from_status'");
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
		
		if($update_from_status=='lock_update_from_status') {
			$db->exec("UPDATE gpio SET locked='$lock_update_from_status' WHERE gpio='$gpio_lock_update_from_status' AND rom='$rom_lock'");
			header("location: " . $_SERVER['REQUEST_URI']);
			exit();
		}
		
		if($update_from_status=='time_update_from_status') {
			$db->exec("UPDATE gpio SET time_offset='$value_update_from_status' WHERE gpio='$gpio_post' AND rom='$rom_post'");
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
		
		/* MOMENT */
		if ($onoff == "bi")  {
			if($moment_time>10){
				$moment_time=10;
			}
			gpio_moment($gpio_post,$rom_post,$rev,$moment_time);
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
		/* SIMPLE */
		if ($onoff == "simple") {
			if ($switch == 'on' ){
				gpio_onoff($gpio_post,$rom_post,'on',$rev);
			} else {
				gpio_onoff($gpio_post,$rom_post,'off',$rev);
			}
		$db->exec("UPDATE gpio SET locked='user' WHERE gpio='$gpio_post' AND rom='$rom_post'");
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
		
		/* TIME */
		if ($trun == "timerun") {
			if ($switch == 'on' ){
				gpio_onoff($gpio_post,$rom_post,'on',$rev);
				$date = new DateTime();
				$time_start=$date->getTimestamp();
				$db->exec("UPDATE gpio SET time_run='on', status='ON $time_offset min', time_offset='$time_offset',time_start='$time_start' WHERE gpio='$gpio_post' AND rom='$rom_post'") or die("exec error");
				header("location: " . $_SERVER['REQUEST_URI']);
				exit();
			} else {
				gpio_onoff($gpio_post,$rom_post,'off',$rev);
				$date = new DateTime();
				$time_start=$date->getTimestamp();
				$db->exec("UPDATE gpio SET time_run='', time_start='', status='OFF' WHERE gpio='$gpio_post' AND rom='$rom_post'") or die("exec error");
				header("location: " . $_SERVER['REQUEST_URI']);
				exit();
			}
	
		}
  		    

		foreach ( $ip_gpio as $s) {
			?>
			

			<tr>
				<?php		
				/* GPIO */
				$sth = $db->prepare("SELECT * FROM gpio WHERE gpio='$s[gpio]' AND rom='$s[rom]' AND (mode='simple' OR mode='temp' OR mode='moment' OR mode='read' OR mode='day' OR mode='time') ");
				$sth->execute();
				$gpio = $sth->fetchAll();
				foreach ($gpio as $g) {
				?>
					<td class="col-md-1">
						<a href="index.php?id=device&type=gpio&gpios=<?php echo $s['gpio'] ?>&ip=<?php echo $s['ip'] ?>&roms=<?php echo $s['rom'] ?>"><img <?php if (!$s['ip']) {echo 'src="media/ico/switch-icon.png"';} else {echo 'src="media/ico/switchip-icon.png"';}  ?> alt="" title="<?php if(!empty($s['ip'])){echo "Last IP: ".$s['ip']." GPIO: ".$s['gpio']." Mode: ".$g['mode'];} else {echo "GPIO: ".$s['gpio']." Mode: ".$g['mode'];}?>" /></a>
					</td>
					<td class="col-md-1">
						<a href="index.php?id=view&type=gpio&max=day&single=<?php echo $s['name']?>" class="label <?php echo label($g['status']) ?>" title="Charts" ><?php echo str_replace("_", " ", $s['name'])?></a>
					</td>
				<?php
				/* SIMPLE AND DAY IP */
				if(($g['mode']=='simple'&&!empty($s['ip']))||($g['mode']=='temp'&&!empty($s['ip'])) ||($g['mode']=='day'&&!empty($s['ip']))) {
					?>
					<td class="col-md-1">
                   	<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input id="onoffstatus" type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="switch" value="<?php echo $s['tmp'] == '1.0'  ? 'off' : 'on'; ?>" <?php echo $s['tmp'] == '1.0' ? 'checked="checked"' : ''; ?>  />
                        <input type="hidden" name="ip" value="<?php echo $s['ip']; ?>"/>
                        <input type="hidden" name="rev" value="<?php echo $g['rev']; ?>"/>
                        <input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
                        <input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
                        <input type="hidden" name="onoff" value="simpleip" />
                    </form>
                    </td>  
					
                   <?php 
				   if($g['mode']!='temp' & $g['mode']=='simple') { echo '<td></td><td></td><td></td>';}
				   elseif($g['mode']!='temp' & $g['mode']=='day')                 {?>
					
					<?php
					$sth = $db->prepare("SELECT name,stime,etime FROM day_plan WHERE  active='on' AND rom='$s[rom]' ");
					$sth->execute();
					$activedp = $sth->fetchAll();
					$numRows = count($activedp);
					if($numRows > 0) {
					foreach ($activedp as $adp) {
						
						$activenamedp=$adp[name];
						$stime=$adp[stime];
						$etime=$adp[etime];
					?>
					<td class="col-md-2">
					<span class="label label-info"><?php echo $activenamedp; ?> </span> 
					<!--
					<td class="col-md-1">
					<span style=" display:inline!important" class="label label-warning"><?php echo $stime." ".$etime;?> </span>
					</td>
					-->
					</td>
					<?php
					 echo '<td class="col-md-1"></td>';
					}
					} else {echo '<td></td><td></td>';}
					?>

				   
				   <td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="hidden" name="gpio_lock_update_from_status" value="<?php echo $s['gpio']; ?>"/>
						<input id="lockstatus" type="checkbox"  data-toggle="toggle" data-size="mini" data-on="lock" data-off="lock" onchange="this.form.submit()" name="lock_update_from_status" value="<?php echo $g['locked'] == 'user'  ? '' : 'user'; ?>" <?php echo $g['locked'] == 'user' ? 'checked="checked"' : ''; ?>  />
						<input type="hidden" name="rom_lock" value="<?php echo $s['rom']; ?>"/>
						<input type="hidden" name="update_from_status" value="lock_update_from_status" />
					</form>
					</td>

					<?php } 

				   
				}
				/* SIMPLE AND DAY*/
				elseif(($g['mode']=='simple')||($g['mode']=='temp')||($g['mode']=='day')) {
				
					?>
					<td class="col-md-1">
					 <?php
						exec('/usr/local/bin/gpio -g read '.$g['gpio'], $state);
						$set=$state[0];
						if ($g['rev']=='on'){
							
							if ($set==1){$set=0;
							
							} else {$set=1;}
						}
					?>	
                   	<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input id="onoffstatus" type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="switch" value="on" <?php echo $set == '1' ? 'checked="checked"' : ''; ?>  />
                        <input type="hidden" name="rev" value="<?php echo $g['rev']; ?>"/>
                        <input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
                        <input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
                        <input type="hidden" name="onoff" value="simple" />
                    </form>
                    </td>
                    <?php 
                    unset($set);
                    unset($state);
                    
					if($g['mode']!='temp' & $g['mode']=='simple') { echo '<td class="col-md-1"></td><td></td><td></td>';}
                    elseif($g['mode']!='temp' & $g['mode']=='day') {?>
					
					
					
					<?php
					$sth = $db->prepare("SELECT name,stime,etime FROM day_plan WHERE  active='on' AND rom='$s[rom]' ");
					$sth->execute();
					$activedp = $sth->fetchAll();
					$numRows = count($activedp);
					if($numRows > 0) {
					foreach ($activedp as $adp) {
						
						$activenamedp=$adp[name];
						$stime=$adp[stime];
						$etime=$adp[etime];
					?>
					<td class="col-md-2">
					<span class="label label-info"><?php echo $activenamedp; ?> </span> 
					<!--
					<td class="col-md-1">
					<span style=" display:inline!important" class="label label-warning"><?php echo $stime." ".$etime;?> </span>
					</td>
					-->
					</td>
					<?php
					 echo '<td class="col-md-1"></td>';
					}
					} else {echo '<td></td><td></td>';}
					?>
					
					
					<td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="hidden" name="gpio_lock_update_from_status" value="<?php echo $s['gpio']; ?>"/>
						<input id="lockstatus" type="checkbox"  data-toggle="toggle" data-size="mini" data-on="lock" data-off="lock" onchange="this.form.submit()" name="lock_update_from_status" value="<?php echo $g['locked'] == 'user'  ? '' : 'user'; ?>" <?php echo $g['locked'] == 'user' ? 'checked="checked"' : ''; ?>  />
						<input type="hidden" name="rom_lock" value="<?php echo $s['rom']; ?>"/>
						<input type="hidden" name="update_from_status" value="lock_update_from_status" />
					</form>
					</td>

					<?php } 
	
				}
				
				/* TIME IP */
				elseif ($g['mode']=='time' && !empty($s['ip'])) {
				
					?>
					<td class="col-md-1">
					 <?php
						exec('/usr/local/bin/gpio -g read '.$g['gpio'], $state);
						$set=$state[0];
						if ($g['rev']=='on'){
							
							if ($set==1){$set=0;
							
							} else {$set=1;}
						}
					?>	
                   	<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input id="onoffstatus" type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="switch" value="<?php echo $s['tmp'] == '1.0'  ? 'off' : 'on'; ?>" <?php echo $s['tmp'] == '1.0' ? 'checked="checked"' : ''; ?>  />
                        <input type="hidden" name="ip" value="<?php echo $s['ip']; ?>"/>
						<input type="hidden" name="rev" value="<?php echo $g['rev']; ?>"/>
                        <input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
                        <input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
						<input type="hidden" name="time_offset" value="<?php echo $g['time_offset']; ?>"/>
                        <input type="hidden" name="trun" value="timerunip" />
                    </form>
                    </td>
                    <?php 
                    unset($set);
                    unset($state);
					?>
					
					<td style="vertical-align:middle">
						Mins:
					</td>
					
					<td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="number" style="width: 4em;" onchange="this.form.submit()" name="value_update_from_status" value="<?php echo $g['time_offset'] ?>" />
						<input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
                        <input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
						<input type="hidden" name="update_from_status" value="time_update_from_status" />
					</form>
					</td>
					
					<td>
					<?php
					if (substr($g['status'],0,2) == 'ON') {
					?>
						<span class="label <?php echo label($g['status']) ?>"> <?php echo str_replace("ON","",str_replace("min","",$g['status'])).min ?> </span>
					</td>
					<?php 
					}
				
				}
				/* TIME */
				elseif ($g['mode']=='time') {
				
					?>
					<td class="col-md-1">
					 <?php
						exec('/usr/local/bin/gpio -g read '.$g['gpio'], $state);
						$set=$state[0];
						if ($g['rev']=='on'){
							
							if ($set==1){$set=0;
							
							} else {$set=1;}
						}
					?>	
                   	<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input id="onoffstatus" type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="switch" value="on" <?php echo $set == '1' ? 'checked="checked"' : ''; ?>  />
                        <input type="hidden" name="rev" value="<?php echo $g['rev']; ?>"/>
                        <input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
                        <input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
						<input type="hidden" name="time_offset" value="<?php echo $g['time_offset']; ?>"/>
                        <input type="hidden" name="trun" value="timerun" />
                    </form>
                    </td>
                    <?php 
                    unset($set);
                    unset($state);
					?>
					<td style="vertical-align:middle">
						Mins:
					</td>
					<td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="number" style="width: 4em;" onchange="this.form.submit()" name="value_update_from_status" value="<?php echo $g['time_offset'] ?>" />
						<input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
                        <input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
						<input type="hidden" name="update_from_status" value="time_update_from_status" />
					</form>
					</td>
					
					<td>
					<?php
					if (substr($g['status'],0,2) == 'ON') {
					?>
						<span class="label <?php echo label($g['status']) ?>"> <?php echo str_replace("ON","",str_replace("min","",$g['status'])).min ?> </span>
					</td>
					<?php 
					}
												
					} 
	
				/* MOMENT IP*/
				elseif($g['mode']=='moment'&&!empty($s['ip'])) {
					?>
					<td class="col-md-1">
                   	<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input id="onoffstatus" data-onstyle="default" type="checkbox" data-size="mini" data-toggle="toggle" name="bi" value="on" onchange="this.form.submit()" title=""   onclick="this.form.submit()" />
						<input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
                        <input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
						<input type="hidden" name="rev" value="<?php echo $g['rev']; ?>"/>
						<input type="hidden" name="ip" value="<?php echo $s['ip']; ?>"/>
						<input type="hidden" name="onoff" value="momentip" />
						<input type="hidden" name="moment_time" value="<?php echo $g['moment_time']; ?>" />    
                    </form>
                    </td>
                    <td></td><td></td><td></td>
				<?php
				}
				/* MOMENT */
				elseif($g['mode']=='moment') {
					?>
					<td class="col-md-1">
                   	<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input id="onoffstatus" data-onstyle="default" type="checkbox" data-size="mini" data-toggle="toggle" name="bi" value="on" onchange="this.form.submit()" title=""   onclick="this.form.submit()" />
						<input type="hidden" name="gpio" value="<?php echo $s['gpio']; ?>"/>
						<input type="hidden" name="rom" value="<?php echo $s['rom']; ?>"/>
						<input type="hidden" name="rev" value="<?php echo $g['rev']; ?>"/>
						<input type="hidden" name="onoff" value="bi" />
						<input type="hidden" name="moment_time" value="<?php echo $g['moment_time']; ?>" />    
                    </form>
                    </td>
                    <td></td><td></td><td></td>
				<?php
				}
				/* READ */
				elseif($g['mode']=='read') {
					?>
					<td class="col-md-1">
                   <?php
						exec('/usr/local/bin/gpio -g read '.$g['gpio'], $state);
					?>
					<input type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="switch" value="" <?php echo $state[0] == '1' ? 'checked="checked"' : ''; unset($state); ?> disabled="disabled" />

                    </td>
                    <td></td><td></td><td></td>
				<?php
				}
				/*READ IP */
				elseif($g['mode']=='simple'&&!empty($ip)) {
					?>
					<td class="col-md-1">
						<input type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="switch" value="<?php echo $s['tmp'] == '1.0'  ? 'off' : 'on'; ?>" <?php echo $s['tmp'] == '1.0' ? 'checked="checked"' : ''; ?>  disabled="disabled" />
                    </td>
                    <td></td><td></td><td></td>
				<?php
				}
				/* TEMP */
				if($g['mode']=='temp') {
					/* TEMP */
					$sth = $db->prepare("SELECT id,value FROM g_func WHERE gpio='$s[gpio]' AND active='on' AND rom='$s[rom]' ORDER BY position ASC LIMIT 1 ");
					$sth->execute();
					$g_func = $sth->fetchAll();
					$numRows = count($g_func);
					$gpio_locked=$g['locked'];
			if ( $numRows > '0' ) {
					foreach ($g_func as $gf) {
					?>
					<td style="vertical-align:middle">
						Value:
					</td>
					<td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="number" style="width: 4em;" onchange="this.form.submit()" name="value_update_from_status" value="<?php echo $gf['value'] ?>" />
						<input type="hidden" name="id_value_update_from_status" value="<?php echo $gf['id']; ?>"/>
						<input type="hidden" name="update_from_status" value="switch_update_from_status" />
					</form>
					</td>
					
					<td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="hidden" name="gpio_lock_update_from_status" value="<?php echo $s['gpio']; ?>"/>
						<input id="lockstatus" type="checkbox"  data-toggle="toggle" data-size="mini" data-on="lock" data-off="lock" onchange="this.form.submit()" name="lock_update_from_status" value="<?php echo $g['locked'] == 'user'  ? '' : 'user'; ?>" <?php echo $g['locked'] == 'user' ? 'checked="checked"' : ''; ?>  />
						<input type="hidden" name="rom_lock" value="<?php echo $s['rom']; ?>"/>
						<input type="hidden" name="update_from_status" value="lock_update_from_status" />
					</form>
					</td>
					<?php
					} 
			}else {
					?>
					<td style="vertical-align:middle">
						Value:
					</td>
					<td class="col-md-2">
					<span class="label label-warning">NO DP</span>
					</td>
					
					<td>
					<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
						<input type="hidden" name="gpio_lock_update_from_status" value="<?php echo $s['gpio']; ?>"/>
						<input id="lockstatus" type="checkbox"  data-toggle="toggle" data-size="mini" data-on="lock" data-off="lock" onchange="this.form.submit()" name="lock_update_from_status" value="<?php echo $g['locked'] == 'user'  ? '' : 'user'; ?>" <?php echo $g['locked'] == 'user' ? 'checked="checked"' : ''; ?>  />
						<input type="hidden" name="rom_lock" value="<?php echo $s['rom']; ?>"/>
						<input type="hidden" name="update_from_status" value="lock_update_from_status" />
					</form>
					</td>
					
					<?php

			}
					?>
					</tr>	

					<?php
					
				}
			}
		}
	}


	/* RELAYS  wireless, nodemcu */
	$numRows = count($sensors_relay);
	if ( $numRows > '0' ) {
		$ip = isset($_POST['ip']) ? $_POST['ip'] : '';
		$relay = isset($_POST['relay']) ? $_POST['relay'] : '';
		$rom = isset($_POST['rom']) ? $_POST['rom'] : '';
		$ronoff = isset($_POST['ronoff']) ? $_POST['ronoff'] : '';
		if (($ronoff == "ronoff")){
			if ($relay == 'on' ){
				$ch = curl_init();
        if(strpos($rom,"tasmota") == true){
				$optArray = array(
					CURLOPT_URL => "$ip/cm?cmnd=Power%20on",
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_CONNECTTIMEOUT => 1,
					CURLOPT_TIMEOUT => 3
				);
        }else{
				$optArray = array(
					CURLOPT_URL => "$ip/seton",
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_CONNECTTIMEOUT => 1,
					CURLOPT_TIMEOUT => 3
				);
        }
				curl_setopt_array($ch, $optArray);
				$res = curl_exec($ch);
				$dbf = new PDO("sqlite:db/$rom.sql");
				$dbf->exec("INSERT OR IGNORE INTO def (value) VALUES ('1')");
				$db->exec("UPDATE sensors SET tmp='1' WHERE rom='$rom'");
		} else { 
			$ch = curl_init();
      if(strpos($rom,"tasmota") == true){
			$optArray = array(
				CURLOPT_URL => "$ip/cm?cmnd=Power%20off",
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_CONNECTTIMEOUT => 1,
				CURLOPT_TIMEOUT => 3
			);
      }else{
			$optArray = array(
				CURLOPT_URL => "$ip/setoff",
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_CONNECTTIMEOUT => 1,
				CURLOPT_TIMEOUT => 3
			);      
      }
			curl_setopt_array($ch, $optArray);
			$res = curl_exec($ch);
			$dbf = new PDO("sqlite:db/$rom.sql");
			$dbf->exec("INSERT OR IGNORE INTO def (value) VALUES ('0')");
			$db->exec("UPDATE sensors SET tmp='0' WHERE rom='$rom'");
		}
		header("location: " . $_SERVER['REQUEST_URI']);
		exit();
		}
         
		foreach ( $sensors_relay as $r) {
		$ip=$r['ip'];
		$ch = curl_init();
    
    if(strpos($r['rom'],"tasmota") == true){
		$optArray = array(
			CURLOPT_URL => "$ip/cm?cmnd=Power",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_CONNECTTIMEOUT => 1,
			CURLOPT_TIMEOUT => 3
		);
    }else{
		$optArray = array(
			CURLOPT_URL => "$ip/showstatus",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_CONNECTTIMEOUT => 1,
			CURLOPT_TIMEOUT => 3
		);    
    }
		curl_setopt_array($ch, $optArray);
		$res = curl_exec($ch);
    if(strpos($r['rom'],"tasmota") == true){
    preg_match('/":"(.*)"/msU', $res, $o1);
    $obj = json_decode($res);
    $o=strtolower(trim($obj->{'POWER'}));
    }else{
		$o1=str_replace('status', '', $res);
		$o=strtolower(trim($o1));
    }
		?>
		<tr>
					<td class="col-md-1">
						<a href="index.php?id=view&type=relay&max=day&single=<?php echo $r['name']?>"><img <?php if (!$s['ip']) {echo 'src="media/ico/switch-icon.png"';} else {echo 'src="media/ico/switchip-icon.png"';}  ?> alt="" title="<?php if(!empty($s['ip'])){echo "Last IP: ".$s['ip']." GPIO: ".$s['gpio']." Mode: ".$g['mode'];} else {echo "GPIO: ".$s['gpio']." Mode: ".$g['mode'];}?>" /></a>
					</td>
			<td>
				<a href="index.php?id=view&type=relay&max=day&single=<?php echo $r['name']?>" class="label <?php if($o=="on") echo "label-success"; else echo "label-danger"; ?>" title=""><?php echo $r['name']?></a>
			</td>
			<td>
				<form class="form-horizontal" action="" method="post" style=" display:inline!important;">
        <input id="onoffstatus" type="checkbox"  data-toggle="toggle" data-size="mini" onchange="this.form.submit()" name="relay" value="<?php echo $o == 'on'  ? 'off' : 'on'; ?>" <?php echo $o == 'on' ? 'checked="checked"' : ''; ?>  />
				<input type="hidden" name="ip" value="<?php echo $r['ip']; ?>"/>
				<input type="hidden" name="rom" value="<?php echo $r['rom']; ?>"/>
				<input type="hidden" name="gpio_post" value="<?php echo $r['gpio']; ?>"/>
				<input type="hidden" name="ronoff" value="ronoff" />
        </form>
			</td>
			<td></td><td></td><td></td>
		</tr>
		<?php
		unset($i);
		}
	}

	/* GPIO */
		
}//hide
?>
<!--END-->
	</tbody>
</table>

</div>
</div>
<?php
} /*user*/

} /* end check if empty */
?>



Awatar użytkownika

pasiekazj
Twórca oprogramowania
Posty: 227
Rejestracja: pn lip 01, 2019 12:08 pm
Otrzymał  piwo: 16 razy
Kontakt:

Re: Nettemp + ESP Tasmota + MQTT

#12

Post autor: pasiekazj » śr lut 26, 2020 1:42 pm

@rejf muszę znaleźć trochę czasu na postawienie ESP i tasmoty na nim.
Rpi 4 + SSD 240 GB + nettemp.
http://mmsite.pl
ODPOWIEDZ