In 2012 I’ve connected a TC74 temperature sensor with a Raspberry Pi and used a combination of a bash script and a web server to display the current temperature on a web page.

Today, I’ve updated this setup. Instead of using a bash script, I now make use of Apache and PHP.

How?

1
2
3
4
5
root@debianI5:/home/jan/RaspberryPi/images# dd if=2018-06-27-raspbian-stretch-lite.img of=/dev/mmcblk0
3637248+0 records in
3637248+0 records out
1862270976 bytes (1.9 GB, 1.7 GiB) copied, 447.756 s, 4.2 MB/s
root@debianI5:/home/jan/RaspberryPi/images#
  • Insert the SD Card in the Raspberry Pi and power up
  • Log in with user=pi and password=raspberry
  • Use raspi-config to enable ssh and I²C
    • sudo raspi-config
      • 5 interfacing options
        • P2 ssh enable
        • P5 I2C enable
    • FINISH - REBOOT
  • Use ifconfig to get the current IP address (don’t forget to connect the ethernet cable)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pi@raspberrypi:~ $ sudo ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.14  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2a02:578:85d9:500:f214:ea17:70e7:a677  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::19d7:c750:fd02:b653  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:01:90:d2  txqueuelen 1000  (Ethernet)
        RX packets 200  bytes 20106 (19.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 100  bytes 16746 (16.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • Use ssh to log in : ssh pi@192.168.1.14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
The authenticity of host '192.168.1.14 (192.168.1.14)' can't be established.
ECDSA key fingerprint is SHA256:b8kZmZnNozkAauDx4oNFx5sPYjNlz5sFvNvwGmI4Kxg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.14' (ECDSA) to the list of known hosts.
pi@192.168.1.14's password:
Linux raspberrypi 4.14.50+ #1122 Tue Jun 19 12:21:21 BST 2018 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jul 14 08:51:38 2018

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
  • Use sudo to become root : sudo su -
1
2
3
4
5
6
pi@raspberrypi:~ $ sudo su -

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

root@raspberrypi:~#
  • apt-get update
  • apt-get upgrade
  • Setup root so we can become root with a password
1
2
3
4
5
6
7
8
9
10
11
12
13
root@raspberrypi:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@raspberrypi:~# exit
logout
pi@raspberrypi:~ $ su -
Password:

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

root@raspberrypi:~#
  • Create a new user “jan”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@raspberrypi:~# adduser jan
Adding user `jan' ...
Adding new group `jan' (1001) ...
Adding new user `jan' (1001) with group `jan' ...
Creating home directory `/home/jan' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for jan
Enter the new value, or press ENTER for the default
        Full Name []: Jan Wagemakers
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
root@raspberrypi:~#
  • Remove the default user “pi”
1
2
3
root@raspberrypi:~# userdel -r pi
userdel: pi mail spool (/var/mail/pi) not found
root@raspberrypi:~#
  • Install i2c-tools
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
root@raspberrypi:~# apt-get install i2c-tools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  read-edid
Suggested packages:
  libi2c-dev python-smbus
The following NEW packages will be installed:
  i2c-tools read-edid
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 72.1 kB of archives.
After this operation, 241 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf read-edid armhf 3.0.2-1 [15.4 kB]
Get:2 http://mirror.as43289.net/raspbian/raspbian stretch/main armhf i2c-tools armhf 3.1.2-3 [56.7 kB]
Fetched 72.1 kB in 1s (61.6 kB/s)
Selecting previously unselected package read-edid.
(Reading database ... 34394 files and directories currently installed.)
Preparing to unpack .../read-edid_3.0.2-1_armhf.deb ...
Unpacking read-edid (3.0.2-1) ...
Selecting previously unselected package i2c-tools.
Preparing to unpack .../i2c-tools_3.1.2-3_armhf.deb ...
Unpacking i2c-tools (3.1.2-3) ...
Setting up read-edid (3.0.2-1) ...
Setting up i2c-tools (3.1.2-3) ...
Processing triggers for man-db (2.7.6.1-2) ...
root@raspberrypi:~#
  • Test if the TC74 can be found (don’t forget to connect it first)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@raspberrypi:~# i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@raspberrypi:~#
  • Test if it is possible to read the temperature
1
2
3
root@raspberrypi:~# i2cget -y 0 0x48 0 b
0x19
root@raspberrypi:~#
  • Make sure that normal user “jan” can read the temperature
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
jan@raspberrypi:~ $ i2cdetect 0
Error: Could not open file `/dev/i2c-0': Permission denied
Run as root?
jan@raspberrypi:~ $ su -
Password:
root@raspberrypi:~# usermod -a -G i2c jan
root@raspberrypi:~# exit
logout
jan@raspberrypi:~ $ i2cdetect 0
Error: Could not open file `/dev/i2c-0': Permission denied
Run as root?
jan@raspberrypi:~ $ exit
logout
Connection to 192.168.1.14 closed.
 pts/2  jan@LAPTOP ~/RaspberryPi/images$ ssh 192.168.1.14
jan@192.168.1.14's password:
Linux raspberrypi 4.14.50+ #1122 Tue Jun 19 12:21:21 BST 2018 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jul 14 09:03:52 2018 from 192.168.1.245
jan@raspberrypi:~ $ i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
jan@raspberrypi:~ $ i2cget -y 0 0x48 0 b
0x19
jan@raspberrypi:~ $
  • Ok, at this point a normal user can read the temperature by using the commandline.
  • Let’s install Apache
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
root@raspberrypi:~# apt-get install apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 ssl-cert
Suggested packages:
  www-browser apache2-doc apache2-suexec-pristine | apache2-suexec-custom openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,949 kB of archives.
After this operation, 6,266 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf libapr1 armhf 1.5.2-5 [79.8 kB]
Get:2 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf libaprutil1 armhf 1.5.4-3 [75.9 kB]
Get:3 http://mirror.de.leaseweb.net/raspbian/raspbian stretch/main armhf libaprutil1-dbd-sqlite3 armhf 1.5.4-3 [17.9 kB]
Get:4 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf libaprutil1-ldap armhf 1.5.4-3 [16.9 kB]
Get:5 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf liblua5.2-0 armhf 5.2.4-1.1 [82.8 kB]
Get:7 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf apache2-utils armhf 2.4.25-3+deb9u4 [218 kB]
Get:8 http://mirror.netcologne.de/raspbian/raspbian stretch/main armhf apache2-data all 2.4.25-3+deb9u4 [162 kB]
Get:10 http://mirror.de.leaseweb.net/raspbian/raspbian stretch/main armhf ssl-cert all 1.0.39 [20.8 kB]
Get:9 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf apache2 armhf 2.4.25-3+deb9u4 [236 kB]
Get:6 http://mirrors.fe.up.pt/raspbian/raspbian stretch/main armhf apache2-bin armhf 2.4.25-3+deb9u4 [1,038 kB]
Fetched 1,949 kB in 2s (730 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libapr1:armhf.
(Reading database ... 34426 files and directories currently installed.)
Preparing to unpack .../0-libapr1_1.5.2-5_armhf.deb ...
Unpacking libapr1:armhf (1.5.2-5) ...
Selecting previously unselected package libaprutil1:armhf.
Preparing to unpack .../1-libaprutil1_1.5.4-3_armhf.deb ...
Unpacking libaprutil1:armhf (1.5.4-3) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3:armhf.
Preparing to unpack .../2-libaprutil1-dbd-sqlite3_1.5.4-3_armhf.deb ...
Unpacking libaprutil1-dbd-sqlite3:armhf (1.5.4-3) ...
Selecting previously unselected package libaprutil1-ldap:armhf.
Preparing to unpack .../3-libaprutil1-ldap_1.5.4-3_armhf.deb ...
Unpacking libaprutil1-ldap:armhf (1.5.4-3) ...
Selecting previously unselected package liblua5.2-0:armhf.
Preparing to unpack .../4-liblua5.2-0_5.2.4-1.1_armhf.deb ...
Unpacking liblua5.2-0:armhf (5.2.4-1.1) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../5-apache2-bin_2.4.25-3+deb9u4_armhf.deb ...
Unpacking apache2-bin (2.4.25-3+deb9u4) ...
Selecting previously unselected package apache2-utils.
Preparing to unpack .../6-apache2-utils_2.4.25-3+deb9u4_armhf.deb ...
Unpacking apache2-utils (2.4.25-3+deb9u4) ...
Selecting previously unselected package apache2-data.
Preparing to unpack .../7-apache2-data_2.4.25-3+deb9u4_all.deb ...
Unpacking apache2-data (2.4.25-3+deb9u4) ...
Selecting previously unselected package apache2.
Preparing to unpack .../8-apache2_2.4.25-3+deb9u4_armhf.deb ...
Unpacking apache2 (2.4.25-3+deb9u4) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../9-ssl-cert_1.0.39_all.deb ...
Unpacking ssl-cert (1.0.39) ...
Setting up libapr1:armhf (1.5.2-5) ...
Setting up apache2-data (2.4.25-3+deb9u4) ...
Setting up ssl-cert (1.0.39) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up libaprutil1:armhf (1.5.4-3) ...
Processing triggers for systemd (232-25+deb9u2) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up liblua5.2-0:armhf (5.2.4-1.1) ...
Setting up libaprutil1-ldap:armhf (1.5.4-3) ...
Setting up libaprutil1-dbd-sqlite3:armhf (1.5.4-3) ...
Setting up apache2-utils (2.4.25-3+deb9u4) ...
Setting up apache2-bin (2.4.25-3+deb9u4) ...
Setting up apache2 (2.4.25-3+deb9u4) ...
Enabling module mpm_event.
Enabling module authz_core.
Enabling module authz_host.
Enabling module authn_core.
Enabling module auth_basic.
Enabling module access_compat.
Enabling module authn_file.
Enabling module authz_user.
Enabling module alias.
Enabling module dir.
Enabling module autoindex.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module filter.
Enabling module deflate.
Enabling module status.
Enabling module reqtimeout.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Processing triggers for systemd (232-25+deb9u2) ...
root@raspberrypi:~#
  • Let’s install 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
root@raspberrypi:~# apt-get install php libapache2-mod-php
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php7.0 php-common php7.0 php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php libapache2-mod-php7.0 php php-common php7.0 php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,695 kB/2,719 kB of archives.
After this operation, 11.1 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://ftp.fi.muni.cz/pub/linux/raspbian/raspbian stretch/main armhf php7.0-common armhf 7.0.30-0+deb9u1 [484 kB]
Get:5 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian stretch/main armhf php7.0-cli armhf 7.0.30-0+deb9u1 [1,052 kB]
Get:2 http://raspbian.mirror.uk.sargasso.net/raspbian stretch/main armhf php7.0-json armhf 7.0.30-0+deb9u1 [15.2 kB]
Get:3 http://ftp.fi.muni.cz/pub/linux/raspbian/raspbian stretch/main armhf php7.0-opcache armhf 7.0.30-0+deb9u1 [65.2 kB]
Get:6 http://mirror.de.leaseweb.net/raspbian/raspbian stretch/main armhf libapache2-mod-php7.0 armhf 7.0.30-0+deb9u1 [1,015 kB]
Get:4 http://ftp.fi.muni.cz/pub/linux/raspbian/raspbian stretch/main armhf php7.0-readline armhf 7.0.30-0+deb9u1 [10.8 kB]
Get:7 http://mirror.as43289.net/raspbian/raspbian stretch/main armhf php7.0 all 7.0.30-0+deb9u1 [53.4 kB]
Fetched 2,695 kB in 4s (591 kB/s)
Selecting previously unselected package php-common.
(Reading database ... 35137 files and directories currently installed.)
Preparing to unpack .../0-php-common_1%3a49_all.deb ...
Unpacking php-common (1:49) ...
Selecting previously unselected package php7.0-common.
Preparing to unpack .../1-php7.0-common_7.0.30-0+deb9u1_armhf.deb ...
Unpacking php7.0-common (7.0.30-0+deb9u1) ...
Selecting previously unselected package php7.0-json.
Preparing to unpack .../2-php7.0-json_7.0.30-0+deb9u1_armhf.deb ...
Unpacking php7.0-json (7.0.30-0+deb9u1) ...
Selecting previously unselected package php7.0-opcache.
Preparing to unpack .../3-php7.0-opcache_7.0.30-0+deb9u1_armhf.deb ...
Unpacking php7.0-opcache (7.0.30-0+deb9u1) ...
Selecting previously unselected package php7.0-readline.
Preparing to unpack .../4-php7.0-readline_7.0.30-0+deb9u1_armhf.deb ...
Unpacking php7.0-readline (7.0.30-0+deb9u1) ...
Selecting previously unselected package php7.0-cli.
Preparing to unpack .../5-php7.0-cli_7.0.30-0+deb9u1_armhf.deb ...
Unpacking php7.0-cli (7.0.30-0+deb9u1) ...
Selecting previously unselected package libapache2-mod-php7.0.
Preparing to unpack .../6-libapache2-mod-php7.0_7.0.30-0+deb9u1_armhf.deb ...
Unpacking libapache2-mod-php7.0 (7.0.30-0+deb9u1) ...
Selecting previously unselected package libapache2-mod-php.
Preparing to unpack .../7-libapache2-mod-php_1%3a7.0+49_all.deb ...
Unpacking libapache2-mod-php (1:7.0+49) ...
Selecting previously unselected package php7.0.
Preparing to unpack .../8-php7.0_7.0.30-0+deb9u1_all.deb ...
Unpacking php7.0 (7.0.30-0+deb9u1) ...
Selecting previously unselected package php.
Preparing to unpack .../9-php_1%3a7.0+49_all.deb ...
Unpacking php (1:7.0+49) ...
Setting up php-common (1:49) ...
Created symlink /etc/systemd/system/timers.target.wants/phpsessionclean.timer → /lib/systemd/system/phpsessionclean.timer.
Processing triggers for man-db (2.7.6.1-2) ...
Setting up php7.0-common (7.0.30-0+deb9u1) ...

Creating config file /etc/php/7.0/mods-available/calendar.ini with new version

Creating config file /etc/php/7.0/mods-available/ctype.ini with new version

Creating config file /etc/php/7.0/mods-available/exif.ini with new version

Creating config file /etc/php/7.0/mods-available/fileinfo.ini with new version

Creating config file /etc/php/7.0/mods-available/ftp.ini with new version
 
Creating config file /etc/php/7.0/mods-available/gettext.ini with new version

Creating config file /etc/php/7.0/mods-available/iconv.ini with new version

Creating config file /etc/php/7.0/mods-available/pdo.ini with new version

Creating config file /etc/php/7.0/mods-available/phar.ini with new version

Creating config file /etc/php/7.0/mods-available/posix.ini with new version

Creating config file /etc/php/7.0/mods-available/shmop.ini with new version

Creating config file /etc/php/7.0/mods-available/sockets.ini with new version

Creating config file /etc/php/7.0/mods-available/sysvmsg.ini with new version

Creating config file /etc/php/7.0/mods-available/sysvsem.ini with new version

Creating config file /etc/php/7.0/mods-available/sysvshm.ini with new version

Creating config file /etc/php/7.0/mods-available/tokenizer.ini with new version
Setting up php7.0-readline (7.0.30-0+deb9u1) ...

Creating config file /etc/php/7.0/mods-available/readline.ini with new version
Setting up php7.0-opcache (7.0.30-0+deb9u1) ...

Creating config file /etc/php/7.0/mods-available/opcache.ini with new version
Setting up php7.0-json (7.0.30-0+deb9u1) ...

Creating config file /etc/php/7.0/mods-available/json.ini with new version
Setting up php7.0-cli (7.0.30-0+deb9u1) ...
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar7.0 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar7.0 to provide /usr/bin/phar.phar (phar.phar) in auto mode

Creating config file /etc/php/7.0/cli/php.ini with new version
Setting up libapache2-mod-php7.0 (7.0.30-0+deb9u1) ...

Creating config file /etc/php/7.0/apache2/php.ini with new version
Module mpm_event disabled.
Enabling module mpm_prefork.
apache2_switch_mpm Switch to prefork
apache2_invoke: Enable module php7.0
Setting up libapache2-mod-php (1:7.0+49) ...
Setting up php7.0 (7.0.30-0+deb9u1) ...
Setting up php (1:7.0+49) ...
root@raspberrypi:~#
  • Use apt-get to install your favourite text editor : apt-get install vim
  • Create /var/www/html/index.php to test if apache/php works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@raspberrypi:/var/www/html# ls
index.html
root@raspberrypi:/var/www/html# rm index.html
root@raspberrypi:/var/www/html# vim index.php
root@raspberrypi:/var/www/html# cat index.php
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
 </body>
</html>
root@raspberrypi:/var/www/html#
  • With a webbrowser go to the IP address of the raspberry pi. It should display “Hello World”.
  • Change index.php so that PHP uses i2cget to read the temperature and convert it from hexadecimal to decimal.
1
2
3
4
5
6
7
8
9
10
11
12
13
root@raspberrypi:/var/www/html# cat index.php
<html>
 <head>
  <title>TC74 temperature</title>
 </head>
 <body>
  <?php
        $Thex = exec('/usr/sbin/i2cget -y 0 0x48 0 b');
        echo 'Temp = ' . hexdec($Thex) . '°C';
  ?>
 </body>
</html>
root@raspberrypi:/var/www/html#
  • At this point the PHP-page will display a temperature of 0°C. Question, why is it not working?
  • Answer, Permissions. Add “www-data” to the “i2c” group, just like we have done before for user “jan”.
1
2
3
4
5
6
7
8
9
10
11
root@raspberrypi:/var/www/html# ps aux | grep apache
root      6760  0.0  6.6 119768 12184 ?        Ss   09:42   0:00 /usr/sbin/apache2 -k start
www-data  6762  0.0  4.4 120064  8176 ?        S    09:42   0:00 /usr/sbin/apache2 -k start
www-data  6763  0.0  2.5 119808  4704 ?        S    09:42   0:00 /usr/sbin/apache2 -k start
www-data  6764  0.0  2.0 119792  3856 ?        S    09:42   0:00 /usr/sbin/apache2 -k start
www-data  6765  0.0  4.6 120064  8500 ?        S    09:42   0:00 /usr/sbin/apache2 -k start
www-data  6766  0.0  2.0 119792  3856 ?        S    09:42   0:00 /usr/sbin/apache2 -k start
www-data  7106  0.0  2.7 119792  4980 ?        S    09:51   0:00 /usr/sbin/apache2 -k start
root      7131  0.0  0.9   4364  1792 pts/0    S+   10:01   0:00 grep apache
root@raspberrypi:/var/www/html# usermod -a -G i2c www-data
root@raspberrypi:/var/www/html# systemctl restart apache2
  • At this point, it should be working :-)

The result

Webbrowser : Temp = 25°C