Temperature.php does not work

Hi!
I am trying to get a php-script running on my RPI.
I did all like in the tutorial here:
https://raspberry.tips/raspberrypi-tutorials/visualisierung-von-temperaturdaten-mit-emoncms/
It should get the temperature from my ds18b20 sensor and send it to the emoncms.
But instead I get the error:

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

Line 8:

$EmonCMSHost = “localhost”;<a href="/wp-content/uploads/2014/12/temperatur.php_.txt">temperatur.php</a>

This is certainly incorrect. There are two logical statements there:

$EmonCMSHost = “localhost”;

This is OK, but the second one

<a href="/wp-content/uploads/2014/12/temperatur.php_.txt">temperatur.php</a>

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:

$EmonCMSHost = “localhost”;
?>
<a href="/wp-content/uploads/2014/12/temperatur.php_.txt">temperatur.php</a>
<?php

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.

Hi! Thanks for your answer!
I tried to comment the second statement out.
Now I have this:

PHP Notice: Undefined offset: 1 in /home/pi/temperatur.php on line 23
PHP Notice: Undefined offset: 1 in /home/pi/temperatur.php on line 24

I don’t get it…
Any Idea?

Have you looked at the PHP manual here and here? That will tell you what the functions on lines 23 & 24 do.

preg_match("/t=(.+)/", preg_split("/n/", $thermometerReadings)[1], $matches);
$temperature = $matches[1];

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 $matches should 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”?

Johannes - when you typed in the commands in the instructions, what did you get returned for these two commands?
 

My DS18B20 works. I get this:

~$ cat /sys/bus/w1/devices/28-041635d60cff/w1_slave
30 01 4b 46 3f ff 3f 10 91 : crc=91 YES
30 01 4b 46 3f ff 3f 10 91 t=19000

Have you put 28-041635d60cff into $SENSORID = … in Line 4?

Yes I have my sensor ID copied.
the code is:


<?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?

Thanks for your help!

there is a great resource for PHP and PHP Commands at php.net.

So since it states offset of 1, then like Robert mention it is array related. Lookup the preg_split function:
http://php.net/manual/en/function.preg-split.php

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.
 

EDIT: A better way may be

preg_split("/(\r\n|\n|\r)/",$thermometerReadings);

and then it will look for a Return and a Newline.

Thank you!

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 tried this and the result is a correct print:

48 01 4b 46 3f ff 3f 10 0e : crc=0e YES
48 01 4b 46 3f ff 3f 10 0e t=20500
20500

the 20500 is now the correct “splitted” value from the temperature sensor.

But unfortunately the “upload” to my emoncms does not work.
The example in the tutorial is:

$url = ‘http:’ . $EmonCMSHost . ‘/emoncms/api/post.json?node=’ . $ecmsSENSORID . ‘&csv=’ . $temperature .’&apikey=’ . $EmonCMSApiKey . ‘’;

I tried several “urls” …but no success. There is no temperature value under

http://emonpi/emoncms/input/view

there is no error and for my understanding the php script should work.
Any idea

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?)

If using a http(s) request direct to emoncms, then emonhub isn’t part of the chain.

@johannes1989

Can you add a line after the $url = line to print it? Something like should work (corrections welcome my php isn’t so hot)

print $url;

so that we can see the full url as it is sent. Please fudge any sensitive info like domain name or apikey, but let us see the rest.

As Robert says that should probably be http://and I do not think that /api should be in there either. it should be input. Try

$url = ‘http://’ . $EmonCMSHost . ‘/emoncms/input/post.json?node=’ . $ecmsSENSORID . ‘&csv=’ . $temperature .’&apikey=’ . $EmonCMSApiKey . ‘’;

and that can also be shortened further to

$url = ‘http://’ . $EmonCMSHost . ‘/emoncms/input/post?node=’ . $ecmsSENSORID . ‘&csv=’ . $temperature .’&apikey=’ . $EmonCMSApiKey . ‘’;

I think the .json api’s are being supported less now.

see http://emoncms.org/site/api#input for emoncms.org api help or http://yourserver/emoncms/input/api for help with self hosted emoncms api, although they should be similar, there are subtle differences, in the api and the documentation.

Now It works !!

The correct url is like Paul posted:

$url = ‘http://’ . $EmonCMSHost . ‘/emoncms/input/post.json?node=’ . $ecmsSENSORID . ‘&csv=’ . $temperature .’&apikey=’ . $EmonCMSApiKey . ‘’;

or

$url = ‘http://’ . $EmonCMSHost . ‘/emoncms/input/post?node=’ . $ecmsSENSORID . ‘&csv=’ . $temperature .’&apikey=’ . $EmonCMSApiKey . ‘’;

Thank you guys!

1 Like

Can you post the complete and correct script (but with a false APIKey) so that anyone reading this can have all the corrections in one place?

1 Like
<?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);
?>
1 Like