Warning: include(highlight.inc.php): failed to open stream: No such file or directory in /usr/share/nginx/html/drucken.php on line 12

Warning: include(): Failed opening 'highlight.inc.php' for inclusion (include_path='.:/usr/local/lib/php') in /usr/share/nginx/html/drucken.php on line 12


The source code of all examples can be downloaded from here.


NTP Time Synchronization


When the Linux operating system of the Raspberry Pi is booting, it sets the current date and time to what was saved at the last run. But what you need is the current date/time and the Linux tries to get this information from one of the Internet time servers using the Network Time Protocol (NTP). There are hundreds of NTP servers around the world providing time services to hundred of millions of systems. At boot time a service is started and the Raspberry Pi tries to connect to one of the NTP servers configured in the file /etc/ntp.conf file. If successful, the system clock is automatically set to the current Universal Time (UTC). Provided that your time zone is correctly selected (see below), the date command returns the local time. Of course, if the Raspberry Pi is not connected to the Internet at boot time, the time is erroneous.

In order to set up NTP time synchronization open a terminal and type

sudo nano /etc/ntp.conf

Then replace the lines like

server 0.dk.pool.ntp.org iburst
server 1.dk.pool.ntp.org iburst
server 2.dk.pool.ntp.org iburst
server 3.dk.pool.ntp.org iburst

with the time servers for your region that you find here. For instace in Germany and Switzerland:

server 0.de.pool.ntp.org iburst
server 1.de.pool.ntp.org iburst
server 2.de.pool.ntp.org iburst
server 3.de.pool.ntp.org iburst

Comment out (by adding a leading # in the lines):

restrict ::1

Save the file and set the correct time zone by running

sudo cp /usr/share/zoneinfo/<zone> /etc/localtime

Replace <zone> with your time zone. Check about the possible values, by looking in /usr/share/zoneinfo. For instance for Switzerland type:

sudo cp /usr/share/zoneinfo/Europe/Zurich /etc/localtime

Restart the time synchronization service (ntp) by typing:

sudo /etc/init.d/ntp restart



to check if the time is correct. If this is not the case, force the synchronization by typing:

sudo ntpd -gq
sudo /etc/init.d/ntp restart



Real-time Clock Based On The DS1307 Chip


In many real-time applications the Raspberry Pi cannot access the Internet at startup. Like in any PC a battery powered on board clock should be used to deliver the current time. A small Realtime Clock Module (RTC) based on the DS1307 chip using the I2C protocol can do the job. RTC modules are available from different sources found on the Internet. Some of them are breakout boards that are inserted directly into the GPIO header. But you may also build your RTC from the Maxim DS1307 chip on a breadboard.

Schematic from binerry.de

If you inspect the datasheet of DS1307 you realize that the data exchange is extremely simple. 8 byte registers at 0x00 to 0x06 holds the seconds, minutes, hours, day_of_week, day, month, year of the current date/time in Binary-Coded-Decimal (BCD) format (two decimals coded as binary 0000 to 1001, one in the lower nibble, one in the higher nibble, e.g. decimal 41 is binary 01000001). The clock is set to a specific date/time by writing into the registers. As long as the chip is powered by the battery, the current date/time can be requested by reading the registers. Register at address 0x07 is a control register. It is not used in normal operation.

As usual we abstract the clock module in software by a simple class called RTC in the module RTC_DS1307.py that you can download from here. Consult the Python documentation for more information.

After installation, you should setup the RTC to the current date/time. The following program will do it.


# SetDate.py

from RTC_DS1307 import RTC

rtc = RTC()
rtc.setDate(seconds = 0, minutes = 47, hours = 9, dow = 3,
            day = 8, month = 6, year = 16)
print "Date set to:", rtc.getDateStr()
Highlight program code (Ctrl+C copy, Ctrl+V paste)

Whenever you need the current date/time you can use getDate() or getDateStr().


# GetDate.py

from RTC_DS1307 import RTC

rtc = RTC()
print "Got date:", rtc.getDateStr()

Highlight program code (Ctrl+C copy, Ctrl+V paste)

This program will not synchronize the Linux based clock; its left to you to add some additional lines of code for this task.