-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalk.php
155 lines (136 loc) · 6 KB
/
talk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
// ------------------------------------------------------------
// (c) 2021/2022 by tmastersmart winnfreenet.com all rights recerved
// Permission granted to install and use wuith hubitat for free
// https://github.com/tmastersmart/pi-talk
//
// Pi talk,Chime, Siren,media,button
// v3.1 9- 2-2022 GPIO fix
// v3.0 4-10-2021 Default varables fixed
// v2.9 9-18-2021 Internal log Rotation
// v2.8 9-17-2021
// v2.7 9-16-2021 server.txt file added
// v2.6 9-15-2021
// v2.5 9/13/2021 GPIO added
// v2.4 9/11/2021
// v2.3 9/11/2021
// v2.2 9/10/2021
// v2.1 9/10/2021
// v2.0 9/09/2021
//
//
//
// ---------------------------------------------------
// you need to install these files on your pi
// place php files in /var/www/html/
//
// talk.php <-- this reveives commands from HUB
// temp.php <-- this file post to HUB
// temp-rotate.php <--- run daily by chron
// input-scan.php <-- Safe loading of get and post
// talk.sh <-- this runs in a loop to take action
// temp-chart.sh <-- Draws a png temp chart in /images
//
// install-talk.sh <-- Installs extra programs you need
//
//https://github.com/tmastersmart/pi-talk
//----------------------------------------------------
$server ="/var/www/html/server.txt"; // This holds the server info sent to the hub
$log ="/var/www/html/talk.log";
$backup= "/var/www/html/talk-1.log";
$cmd1 ="/var/www/html/talk1.txt"; if(file_exists($cmd1)) { unlink ($cmd1);}
$cmd2 ="/var/www/html/talk2.txt"; if(file_exists($cmd2)) { unlink ($cmd2);}
$cmd3 ="/var/www/html/chime.txt"; if(file_exists($cmd3)) { unlink ($cmd3);}
$logSize= 30000;
// set defaults for newer PHP to stop errors
$talk="";$device="";$code="";$voice="";$play="";$gpio="";$switch="";$button="";$ok="";
$lang="-ven-us";// english us 'espeak --voices' for list
$voice="+f4";// f4 works better than F1
$return_var =""; $ok= false; $header = true;// set 404 error
include "input-scan.php";
for ($i=0; $i < sizeof($fieldNames); $i++) {
if ($fieldNames[$i] == 'talk') { $talk= $fieldValues[$i]; }
if ($fieldNames[$i] == 'device') {$device= $fieldValues[$i]; }
if ($fieldNames[$i] == 'code') { $code= $fieldValues[$i]; }
if ($fieldNames[$i] == 'voice') { $voice= $fieldValues[$i]; }
if ($fieldNames[$i] == 'lang') { $lang= $fieldValues[$i]; }
if ($fieldNames[$i] == 'play') { $play= $fieldValues[$i]; }
if ($fieldNames[$i] == 'gpio') { $gpio= $fieldValues[$i]; }
if ($fieldNames[$i] == 'switch') {$switch= $fieldValues[$i]; }
if ($fieldNames[$i] == 'button') {$button= $fieldValues[$i]; }
}
// We create a file with what we want to say and the background program will play it
if($talk){
$fileOUT = fopen($cmd1, "w") ;flock( $fileOUT, LOCK_EX );fwrite ($fileOUT, "$talk") ;flock( $fileOUT, LOCK_UN );fclose ($fileOUT);
$fileOUT = fopen($cmd2, "w") ;flock( $fileOUT, LOCK_EX );fwrite ($fileOUT, "$lang$voice") ;flock( $fileOUT, LOCK_UN );fclose ($fileOUT);
$header= false;
}
// Play a .mp3 file same as above write the filename into a text file and the monitor will play it
if($play){
if(file_exists("/home/pi/$play.wav")) { $ok=true; $play="/home/pi/$play.wav";}
if(file_exists("/home/pi/$play.mp3")) { $ok=true; $play="/home/pi/$play.mp3";}
if(file_exists("/home/pi/Music/$play.wav")) { $ok=true; $play="/home/pi/Music/$play.wav";}
if(file_exists("/home/pi/Music/$play.mp3")) { $ok=true; $play="/home/pi/Music/$play.mp3";}
$talk="(Play $play)";
if($ok){
$fileOUT = fopen($cmd3, "w") ;flock( $fileOUT, LOCK_EX );fwrite ($fileOUT, "$play") ;flock( $fileOUT, LOCK_UN );fclose ($fileOUT);
$header=false;
}
}
if($gpio){
$header=true;$ok=false;
// at this time I dont have a safe list of GPIOs
// https://elinux.org/RPi_Low-level_peripherals
if($gpio == "7") { $ok=true; }//
if($gpio == "8") { $ok=true; }//
if($gpio == "9") { $ok=true; }//
if($gpio == "10"){ $ok=true; } //
if($gpio == "11"){ $ok=true; } //
if($gpio == "14"){ $ok=true; } //
if($gpio == "15"){ $ok=true; } //
if($gpio == "17"){ $ok=true; }//
if($gpio == "18"){ $ok=true; }//
if($gpio == "22"){ $ok=true; }//
if($gpio == "23"){ $ok=true; }//
if($gpio == "24"){ $ok=true; }//
if($gpio == "25"){ $ok=true; }//
$talk="GPIO $gpio $switch"; // tell the log what we are doing
// 1on 2off 3press
// This calls the gpio-g external program may not work with latest os
if($ok){
$send="gpio -g mode $gpio out"; exec($send, $output, $return_var );
if ($switch==0){$send="gpio -g write $gpio 0";exec($send, $output, $return_var );}
if ($switch==1){$send="gpio -g write $gpio 1";exec($send, $output, $return_var );}
if ($switch==2){
$send="gpio -g write $gpio 1";exec($send, $output, $return_var );
sleep(3);
$send="gpio -g write $gpio 0";exec($send, $output, $return_var );
}
// $return_var returning 127 after post
$header= false; $return_var = "Button = $button";
}
}
// Send a header so the status can be read
if ($header){
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
$return_var= "404 Not Found $return_var";
}
// Log rotation does not like the permisions in WWW directory so doing it by php
$size= filesize($log);
if($size >= $logSize){
if (file_exists($backup)) {unlink ($backup);}if (file_exists($backup)){$return_var="$return_var Error Del $backup";}
rename ($log, $backup); if (file_exists($log)){$return_var="$return_var Error Renam $log";}
if (!file_exists($log)){ $size=0;$return_var="$return_var Log Rotated";}
}
// Save the log
$datum = date('[Y-m-d H:i:s]');
$status = "$datum : Message:$talk From:$code $device status: $return_var Size:$size";
print $status;
$fileOUT = fopen($log, "a") ;flock( $fileOUT, LOCK_EX );fwrite ($fileOUT, "$status\n");flock( $fileOUT, LOCK_UN );fclose ($fileOUT);
// save server info gpio must be installed
if(!file_exists($server)) {
$send="gpio -v >>$server";exec($send, $output, $return_var );
$send="gpio readall >>$server";exec($send, $output, $return_var );
$send="gpio allreadall >>$server";exec($send, $output, $return_var );
}