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.
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:~#exitlogout
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:~#
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
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:~#exitlogout
jan@raspberrypi:~ $i2cdetect 0
Error: Could not open file `/dev/i2c-0': Permission denied
Run as root?
jan@raspberrypi:~ $exitlogout
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.
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