skip to Main Content

I tried to read my DS18B20 sensor temperature with my raspberry pi adresse, I wrote this code in /var/www/html/index.php

<?php
      // Fichier à lire
      $file = "/sys/bus/w1/devices/28-80000026ddb1/w1_slave";
      // Lecture ligne par ligne
      $lines = file($file);
      // Recupere la 2nd ligne
      $temp = explode(’=’, $lines[1]);
      // Formatage de la temperature
      $temp = number_format($temp[1]/1000,2, ’.’, ’’);
      // On affiche la temperature
      echo $temp;echo" degrés Celius";
?>

What’s wrong with it? It shows me the following:

enter image description here

2

Answers


  1. You are seeing the root page of your webserver. Your PHP code is not in the root page, you need to browse to the page index.php .

    Hit in the browser bar the url that show Apache2 Debian Default Page, followed by:

    /index.php
    

    instead of

    /index.html
    

    For example:

    [ip_address]/index.php
    

    If the apache configuration file is the default one, should not require other settings to browse your page. Further configurations are possible to change the root page to yours. See about this: How do I change the default index page in Apache?

    Yes, PHP should be installed in order to run PHP code.

    Login or Signup to reply.
  2. You need to install PHP, link it to your apache installation, then tell apache that the root page is “index.php” instead that “index.html” so that when you request “/” it can execute the index.php script.

    1- install PHP engine, for example as apache SAPI module:

    apt install libapache2-mod-php7.0
    

    2- put this one inside your virtualhost or in your /etc/apache2/apache2.conf file:

    DirectoryIndex index.php index.html
    

    3- restart apache

    You should now be able to execute PHP code with apache httpd

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search