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