| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,160 +0,0 @@ |
| 1 |
-<?php |
|
| 2 |
-//include_once '/var/www/html/www.clickhome.ro/inc/session.php'; |
|
| 3 |
-$db = @mysql_connect("localhost", "iot", "Clickiotdbhome8219", TRUE, 128) or die ("Nu ma pot conecta la serverul mysql!");
|
|
| 4 |
-mysql_select_db("clickhome", $db) or die ("Nu pot selecta baza de date!");
|
|
| 5 |
- |
|
| 6 |
-//$coeficient=0.25; |
|
| 7 |
-$ora=date('H:i:s');
|
|
| 8 |
-$dayofweek = date('w');
|
|
| 9 |
-if ($dayofweek==0) $dayofweek=7; |
|
| 10 |
-$q=mysql_query("SELECT * from centrala where day='$dayofweek'");
|
|
| 11 |
-// citesc datale din tabela centrala |
|
| 12 |
-while ($c=mysql_fetch_array($q)){
|
|
| 13 |
- $coeficient=$c['delta']; |
|
| 14 |
- $offline=0; |
|
| 15 |
- $user=$c['user']; |
|
| 16 |
- $senzor=$c['senzor']; |
|
| 17 |
- $releu=$c['releu']; |
|
| 18 |
- // verific ca toti senzorii sa fie ONLINE ca sa pot continua |
|
| 19 |
- $q1=mysql_query("select valoare from getdevinfo where devid in ($senzor, $releu) and user='$user' and camp='LWT'");
|
|
| 20 |
- if (mysql_num_rows($q1)<2){
|
|
| 21 |
- $offline=1; |
|
| 22 |
- } |
|
| 23 |
- else{
|
|
| 24 |
- while ($c1=mysql_fetch_array($q1)){
|
|
| 25 |
- if ($c1[0]=='Offline'){
|
|
| 26 |
- $offline=1; |
|
| 27 |
- } |
|
| 28 |
- } |
|
| 29 |
- } |
|
| 30 |
- if (!$offline){
|
|
| 31 |
- echo "User:$user | senzor:$senzor | releu: $releu\n"; |
|
| 32 |
- $flag=$c['flag']; |
|
| 33 |
- // verific sa nu fiu in modul manual |
|
| 34 |
- if ($flag=='stop'){
|
|
| 35 |
- echo "Centrala e pe modul manual. Fara actiune!\n"; |
|
| 36 |
- } |
|
| 37 |
- else{
|
|
| 38 |
- $topic_senzor=$user."-".$senzor; |
|
| 39 |
- $topic_releu=$user."-".$releu; |
|
| 40 |
- $realTemp=getRealTemp($topic_senzor); |
|
| 41 |
- if (time()<strtotime($c['i2'])){
|
|
| 42 |
- $configTemp=$c['t1']; |
|
| 43 |
- } |
|
| 44 |
- elseif (time()<strtotime($c['i3'])){
|
|
| 45 |
- $configTemp=$c['t2']; |
|
| 46 |
- } |
|
| 47 |
- elseif (time()<strtotime($c['i4'])){
|
|
| 48 |
- $configTemp=$c['t3']; |
|
| 49 |
- } |
|
| 50 |
- elseif (time()<strtotime($c['i5'])){
|
|
| 51 |
- $configTemp=$c['t4']; |
|
| 52 |
- } |
|
| 53 |
- else {
|
|
| 54 |
- $configTemp=$c['t5']; |
|
| 55 |
- } |
|
| 56 |
- $min=$configTemp; |
|
| 57 |
- $max=$configTemp+$coeficient; |
|
| 58 |
- if (isset($realTemp)){
|
|
| 59 |
- if (($min<=$realTemp)&&($realTemp<=$max)){
|
|
| 60 |
- echo "Temperatura $realTemp este in intervalul $min - $max; Fara actiune!\n"; |
|
| 61 |
- } |
|
| 62 |
- elseif (($realTemp<$min)&&($flag=="min")){
|
|
| 63 |
- echo "Temperatura $realTemp este sub pragul minim de $min insa centrala e deja pornita; Fara actiune!\n"; |
|
| 64 |
- } |
|
| 65 |
- elseif (($realTemp>$max)&&($flag=="max")){
|
|
| 66 |
- echo "Temperatura $realTemp este peste pragul maxim de $max insa centrala e deja oprita; Fara actiune!\n"; |
|
| 67 |
- } |
|
| 68 |
- elseif (($realTemp>$max)&&($flag!="max")){
|
|
| 69 |
- echo "Temperatura $realTemp este peste pragul maxim de $max; Opresc centrala!\n"; |
|
| 70 |
- if (isSonoffRelay($user,$releu)){
|
|
| 71 |
- pub_mqtt_topic("cmnd/$topic_releu/POWER","OFF");
|
|
| 72 |
- } |
|
| 73 |
- else {
|
|
| 74 |
- pub_mqtt_topic("cmnd/$topic_releu/ESP","OFF");
|
|
| 75 |
- } |
|
| 76 |
- @mysql_query("insert into log_centrala values (DEFAULT, '$user', '$senzor', '$releu', 'OFF', '$realTemp', '$configTemp',now(), 'AUTO')");
|
|
| 77 |
- @mysql_query("update centrala set flag='max' where user='$user' and senzor='$senzor' and releu='$releu'");
|
|
| 78 |
- } |
|
| 79 |
- elseif (($realTemp<$min)&&($flag!="min")){
|
|
| 80 |
- echo "Temperatura $realTemp este sub pragul minim de $min; Pornesc centrala!\n"; |
|
| 81 |
- if (isSonoffRelay($user,$releu)){
|
|
| 82 |
- pub_mqtt_topic("cmnd/$topic_releu/POWER","ON");
|
|
| 83 |
- } |
|
| 84 |
- else {
|
|
| 85 |
- pub_mqtt_topic("cmnd/$topic_releu/ESP","ON");
|
|
| 86 |
- } |
|
| 87 |
- @mysql_query("insert into log_centrala values (DEFAULT, '$user', '$senzor', '$releu', 'ON', '$realTemp', '$configTemp', now(), 'AUTO')");
|
|
| 88 |
- @mysql_query("update centrala set flag='min' where user='$user' and senzor='$senzor' and releu='$releu'");
|
|
| 89 |
- } |
|
| 90 |
- else {
|
|
| 91 |
- echo "N-ar fi trebuit sa mai ajung aici!\n"; |
|
| 92 |
- } |
|
| 93 |
- } |
|
| 94 |
- else {
|
|
| 95 |
- echo "N-am putut citi temperatura reala. Fara actiune!\n"; |
|
| 96 |
- } |
|
| 97 |
- } |
|
| 98 |
- } |
|
| 99 |
- else {
|
|
| 100 |
- echo "User:$user | senzor:$senzor | releu: $releu\n"; |
|
| 101 |
- echo "Centrala offline. Fara actiune!\n"; |
|
| 102 |
- } |
|
| 103 |
-} |
|
| 104 |
-function isSonoffRelay ($u, $d){
|
|
| 105 |
- $q=mysql_query("SELECT tip from devices where user='$u' and devid='$d'");
|
|
| 106 |
- while ($c=mysql_fetch_array($q)){
|
|
| 107 |
- if ($c['tip']=='12'){
|
|
| 108 |
- return true; |
|
| 109 |
- } |
|
| 110 |
- else {
|
|
| 111 |
- return false; |
|
| 112 |
- } |
|
| 113 |
- } |
|
| 114 |
-} |
|
| 115 |
- |
|
| 116 |
-function getRealTemp($topic){
|
|
| 117 |
- $mqtt=sub_mqtt_topic("cmnd/$topic/ESP","temp","stat/$topic/TEMP");
|
|
| 118 |
- $j=json_decode($mqtt,true); |
|
| 119 |
- return $j['Temp']; |
|
| 120 |
-} |
|
| 121 |
- |
|
| 122 |
-function sub_mqtt_topic($pub_channel,$pub_msg,$sub_channel){
|
|
| 123 |
- $timeout=3; // daca nu primeste raspuns in 3 secunde sa renunte |
|
| 124 |
- $data_initiala=time(); |
|
| 125 |
- $client = new \Mosquitto\Client(uniqid()); |
|
| 126 |
- $client->onMessage('message');
|
|
| 127 |
- $client->setCredentials('admin', 'Clickmqhome8219');
|
|
| 128 |
- $client->connect("localhost", 1883, 5);
|
|
| 129 |
- $client->setTlsCertificates('/etc/ssl/certs/ca-bundle.crt');
|
|
| 130 |
- $client->subscribe($sub_channel, 1); |
|
| 131 |
- usleep(100000); //100 ms |
|
| 132 |
- $client->publish($pub_channel, $pub_msg); |
|
| 133 |
- |
|
| 134 |
- $GLOBALS['mqtt_message'] = ''; |
|
| 135 |
- while (true) {
|
|
| 136 |
- $client->loop(); |
|
| 137 |
- usleep(100000); //100 ms de asteptare sa apuce sa execute comenzile |
|
| 138 |
- $data_acum = time(); |
|
| 139 |
- if (($data_acum - $data_initiala) > $timeout) break; |
|
| 140 |
- if(!empty($GLOBALS['mqtt_message'])) break; |
|
| 141 |
- } |
|
| 142 |
- $client->disconnect(); |
|
| 143 |
- unset($client); |
|
| 144 |
- return ($GLOBALS['mqtt_message']); |
|
| 145 |
-} |
|
| 146 |
- |
|
| 147 |
-function message($message){
|
|
| 148 |
- $GLOBALS['mqtt_message'] = $message->payload; |
|
| 149 |
-} |
|
| 150 |
- |
|
| 151 |
-function pub_mqtt_topic($topic,$msg){
|
|
| 152 |
- $client = new \Mosquitto\Client("phpMQTT-pub-centrala");
|
|
| 153 |
- $client->setCredentials('admin', 'Clickmqhome8219');
|
|
| 154 |
- $client->connect("localhost", 1883, 5); //Change Accordingly. TCP/IP connection is supported by mqtt.
|
|
| 155 |
- $client->setTlsCertificates('/etc/ssl/certs/ca-bundle.crt');
|
|
| 156 |
- $client->publish($topic, $msg); |
|
| 157 |
- $client->disconnect(); |
|
| 158 |
- unset($client); |
|
| 159 |
-} |
|
| 160 |
-?> |