www/getonewire.php

Thu, 25 Apr 2024 14:26:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 25 Apr 2024 14:26:47 +0200
changeset 708
13555c27b592
parent 696
fe042f9484ac
permissions
-rw-r--r--

Version 0.9.19a6. Fixes after a short trial on the production controller. Fixed json for alternate beer termperature sensor. Fixed division by 1000 for the room temperature and humidity values. The dropdown list for devices shows the address instead of description in the list.

696
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 <?php
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 function open_socket()
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 if (!($sock === false)) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 if (socket_connect($sock, "localhost", 6554)) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0));
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 } else {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 socket_close($sock);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 return $sock;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 /**
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * @param string $command to send to the server.
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 * @return string with the complete reply from the
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 * server. This can be a multiline reply.
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 */
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 function send_cmd($command)
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 $sock = open_socket();
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 if ($sock == false) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 return "";
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 socket_write($sock, $command . "\r\n", 4096);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 $answer = "";
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 while (1) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 $line = socket_read($sock, 4096);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 if ($line === '')
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 break;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 $answer .= $line;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 socket_close($sock);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 return $answer;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 function startsWith($haystack, $needle)
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 return !strncmp($haystack, $needle, strlen($needle));
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 $answer = send_cmd("ONEWIRE JSON");
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 header("Content-type: application/json");
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 $arr = explode("\r\n", $answer);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 if (startsWith($arr[0], "212")) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 echo $arr[1];
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 } else {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 echo '{}';
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60

mercurial