PHP Parse error: syntax error, unexpected ‘<’ in /home/pi/temperatur.php on line 8
I know that the turtorial says to change the script if necessary… but I don’t know how to get it work. I tried to change several things, but this caused only other errors.
I’ve never done any php programming (only a litte python).
Where is the bug?
Or, Does anyone have another way to get the data to the cms?
Thanks!
johannes
is HTML, not PHP. It is actually a link on a web page that goes to a text file “temperatur.php”
Comment that second statement out (with " // "), and see what happens. The worst is the web page will be missing the link for you to click on.
I cannot decide what the line should be. It might be that the HTML should be included, in that case you must escape from PHP before that statement and return after it, but that is not consistent with the rest of the program:
or it might be that this second part should be assigned to a string - but I cannot guess the name of the string; or it might just be a typing or copy & paste error and should not be there at all, so you must experiment.
My favourite is it should not be there at all, “/wp-content/uploads/2014” is a directory for WordPress.
In this case, preg_split("/n/", $thermometerReadings) returns an array and the 1st element should be filled with the result of the split, to feed into preg_match. Then $matchesshould be filled with the result of the search. The error says “Undefined offset”, and that means it cannot find element 1 in each case.
Your first move will be to find what $thermometerReadings contains - I think it will be empty.
The main thing I notice with this program is, there is no error handling. What happens if the file cannot be opened in line 16? It should stop there, but there’s nothing to detect an error so it carries on. That is bad programming, and when you have learned some PHP, you should add this so that it fails nicely and tells you what the fault is.
What you must do is check every step, and make sure each variable has the correct value (or as a minimum, it looks sensible).
I cannot run this program for you and debug it because I do not have the hardware, so you must step through the program and check everything. The first is, do you have a DS18B20 at “/sys/bus/w1/devices/28-000005c68110/w1_slave”?
<?php
//================ Config ===========================
//Replace your DS18B20 serial here!
$SENSORID = "28-041635d60cff";
//Set the emoncms API Key, the Hostname or IP and the internal Sensor ID (Numeric only)
$EmonCMSApiKey = "****************************************";
$EmonCMSHost = "192.168.178.41";//<a href="/wp-content/uploads/2014/12/temperatur.php_.txt">temperatur.php</a>
$ecmsSENSORID = "1";
//==================================================
//BuildSensor Path
$SensorPath = '/sys/bus/w1/devices/'. $SENSORID .'/w1_slave';
// Open resource file for thermometer
$thermometer = fopen($SensorPath, "r");
// Get the contents of the resource
$thermometerReadings = fread($thermometer, filesize($SensorPath));
print $thermometerReadings;
// Close resource file for thermometer
fclose($thermometer);
// We're only interested in the 2nd line, and the value after the t= on the 2nd line
preg_match("/t=(.+)/", preg_split("/n/", $thermometerReadings)[1], $matches);
//print $matches[1];
$temperature = $matches[1];
print $temperature;
//Write to emoncms - Example
// You may want to add other parsed values
$url = 'http:' . $EmonCMSHost . '/emoncms/api/post.json?node=' . $ecmsSENSORID . '&csv=' . $temperature .'&apikey=' . $EmonCMSApiKey . '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
?>
this is the output for:
sudo php /home/pi/temperatur.php
30 01 4b 46 3f ff 3f 10 91 : crc=91 YES
30 01 4b 46 3f ff 3f 10 91 t=19000
PHP Notice: Undefined offset: 1 in /home/pi/temperatur.php on line 25
PHP Notice: Undefined offset: 1 in /home/pi/temperatur.php on line 27
the print shows that my sensor is working correct.
But I don’t get closer to a solution…
Do you know any other script that works for the DS18B20?
In python I could write a script to get the value for the sensor as well, but how to transmit it to EmonCMS?
It looks like preg_split is splitting one string (the info printed via print $thermometerReadings :
“30 01 4b 46 3f ff 3f 10 91 : crc=91 YES EOL 01 4b 46 3f ff 3f 10 91 t=19000” <----- EOL is End Of Line.
into an array with two parts. It is split when it finds the end of first line. So the first line goes into [0] and the second line goes into [1].
[0] = 30 01 4b 46 3f ff 3f 10 91 : crc=91 YES
[1] = 30 01 4b 46 3f ff 3f 10 91 t=19000
I am guessing that is not happening because the pattern looks wrong. “/n/” should be “/\n/”. This is a regular expression. But that is only a guess since I haven’t tried this.
I am guessing that is not happening because the pattern looks wrong. “/n/” should be “/\n/”. This is a regular expression. But that is only a guess since I haven’t tried this.
Should it be http:// ?
Do you have a Node set up in emonhub.conf to receive the data? Note that you need datacode = 0
[zero = no decoding as the data is readable text]
Unfortunately, the input API documentation no longer specifies the input format, but I think it is otherwise correct. (@pb66 - can you confirm?)
<?php
//================ Config ===========================
//Replace your DS18B20 serial here!
$SENSORID = "28-041635d60cff";
//Set the emoncms API Key, the Hostname or IP and the internal Sensor ID (Numeric only)
$EmonCMSApiKey = "*********************************";
$EmonCMSHost = "192.168.178.41";//<a href="/wp-content/uploads/2014/12/temperatur.php_.txt">temperatur.php</a>
$ecmsSENSORID = "1";
//==================================================
//BuildSensor Path
$SensorPath = '/sys/bus/w1/devices/'. $SENSORID .'/w1_slave';
// Open resource file for thermometer
$thermometer = fopen($SensorPath, "r");
// Get the contents of the resource
$thermometerReadings = fread($thermometer, filesize($SensorPath));
//print $thermometerReadings;
// Close resource file for thermometer
fclose($thermometer);
// We're only interested in the 2nd line, and the value after the t= on the 2nd line
preg_match("/t=(.+)/", preg_split("/\n/", $thermometerReadings)[1], $matches);
//print $matches[1];
$temperature = $matches[1];
//print $temperature;
//Write to emoncms - Example
// You may want to add other parsed values
$http = 'http://';
$url = $http . $EmonCMSHost . '/emoncms/input/post?node=' . $ecmsSENSORID . '&csv=' . $temperature .'&apikey=' . $EmonCMSApiKey;
print $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
?>