NTP server on Raspberry Pi

About to start following the post, http://www.satsignal.eu/ntp/Raspberry-Pi-NTP.html, by David Taylor. I emailed David (contact is on his page) thanking him for his post and he replied with a few pointers.  I’ll incorporate his suggestions into this blog post and any challenges.

Hardware purchased:

Hardware Assumed:

  • Keyboard and Mouse
  • Monitor with HDMI Support, (Makes setup of WiFi easier even if you plan to run headless)
  • microUSB power cord
  • Soldering Iron
  • Resistor (220 Ohm)

Raspberry Pi Setup for New Users:

http://www.raspberrypi.org/wp-content/uploads/2012/04/quick-start-guide-v2_1.pdf – Various videos on YouTube show you how to boot up your pi if you are using a NOOB card. The USB WiFi dongle states that the latest versions of Wheezy Raspbian distribution support this module out-of-the-box. Default username is pi and the password is raspberry. When booting your pi, make sure to enable ssh services. In addition, you may want to expand the root partition to fill the entire SD card.

If you are loading your own version, you can follow the steps below:

  1. Download an OS image for the SD card – 2012-09-18-wheezy-raspbian.zip
  2. Unzip the contents of the Zip archive to a .IMG file
  3. Download the SD card writer program: Win32DiskImager
  4. Use the Disk Imager to write the OS image to the SD card

Setting up for Wireless Communication:

Although my end goal is to use my raspberry pi as a direct access point, it’s easier to become familiar with the pi by connecting to it through my home router. My first step was to verify the Internet worked by directly connecting to my router by Ethernet. I switched into the raspbian GUI by typing startx on the command line. To test the Internet connection, I opened the the NetSurf Web browser. Following adafruit’s network setup tutorial, I used the WiFi Config application to connect to my wireless network. If you don’t see any network connections during a network scan, you may need to remove the WiFi dongle from your raspberry pi and connect it back. I should add that I struggled getting the WiFi connection to work and I switched to trying to a command line approach. I admit I’m not certain what sets were necessary in allowing me to finally connect to my network through the WiFi dongle. When I have both the Ethernet and WiFi dongle plugged in, I lost access to the WiFi. I then followed a forum post regarding the ifplugd that caused your pi to only use Ethernet when it was plugged in. To remove ifplugd enter sudo apt-get remove ifplugd on the command line, unplug your pi from the power source to reboot, and then WiFi will work when Ethernet is plugged in, but I wasn’t able to get it to work with both network interfaces. In retrospect, removing ifplugd was a dumb idea, apparently you can modify it’s configuration file to enable both. Go to “/etc/default/ifplugd” and change the file to

INTERFACES=""
HOTPLUG_INTERFACES=""
ARGS="-q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"

SSH and Headless Mode:

Since I don’t own a monitor with HDMI, I’ve been using a pico projector with an eye-straining 480×640 screen. SSH allows me to access my pi through my laptop’s command line. If you didn’t enable ssh when you first started your OS, you can enable ssh by typing sudo raspi-config. To get your pi’s IP address, type either ifconfig or access your router’s IP lookup table. Last, from the command line of another computer (my favorite is mobaxterm), type ssh pi@IPADDRESS with the password raspberry.

Raspberry Pi P1 Header and GPIO pins: 

Raspberry Pi P1 Header
Raspberry Pi P1 Header Described by Mosaic Industries

This is the 26 pin I/O P1 Header on the pi. Pin number 1 is closest to the SD card. You can locate the pin 1 copper square by looking at the mirror image on the underside of the raspberry pi.

My next step was to see if I could read an input and an output. Using my jumper cables, I connected Pin 11 to 12 with a 220 Ohm resistor between. (It may be safe, but I just wanted to be careful.)

I then wrote the following code using the command line, creating test.py using the following command:  nano test.py

import RPi.GPIO as GPIO
import time

def toggle(pin1, pin2, input):
GPIO.output(pin1,input)
val = GPIO.input(pin2)
print “%r” % (val)
return

GPIO.setmode(GPIO.BOARD)

outPin = 11
inPin = 12
GPIO.setup(outPin,GPIO.OUT)
GPIO.setup(inPin,GPIO.IN)
toggle(outPin,inPin, True)
toggle(outPin,inPin, False)

GPIO.cleanup()

I then saved the file by typing CTRL-O, then CTRL-X to quit.

From the command line, execute the program by typing sudo python test.py

Adafruit Ultimate GPS Breakout - 66 channel w/10 Hz updates

GPS Board:

The Raspberry Pi outputs 3.3V on pin 1 and 5 V on pin 2 of the P1 Header, while the VIN of the GPS breakout board accepts between 3.3-5 V. In his tutorial, David Taylor connected his GPS board to pin 2, because he wanted to reduce the load on the raspberry pi 3.3 V power regulator.

We’ll be using pins 2, 10, 18, 20 on the raspberry pi to connect to the GPS breakout board.( I’m going to reference pin locations, because I’ve found different naming conventions for GPIOs.) In addition, to make the setup easier, I soldered the break away headers that came with the GPS module into the vias.

CABLE COLOR RASPBERRY PI PIN GPS MODULE PIN
Red 2 +5V 8 VIN
Black 20 Gnd 7 GND
Blue 10 UART RxD 5 TXD
Yellow 24 GPIO 8 9 PPS

I ran into issues trying to install the GPS Daemon, so I updated apt-get with sudo apt-get update && sudo apt-get upgrade

To setup the GPS I followed a tutorial from adafruit. I’ve copied their steps here, because web pages on the Internet are often ephemeral.

To install gpsd, run: sudo apt-get install gpsd gpsd-clients python-gps

Then enter sudo nano /boot/cmdline.txt

change:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

to:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Then: sudo nano /etc/inittab

Change:

#Spawn a getty on Raspberry Pi serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

to:

#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Reboot your Pi: sudo reboot

Point the gps daemon to HW UART: sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

Test the output with: cgps -s

You may have to wait several minutes to lock onto a GPS signal.

NTP:

David Taylor’s blog gives more of the insight into NTP setup, but I’ve summarized the key points here:

$ mkdir ntp
$ cd ntp
$ sudo apt-get install libcap-dev # once-off, required to prevent later file not found error
$ wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-dev/ntp-dev-4.2.7p368.tar.gz # development
$ tar xvfz ntp-dev-4.2.7p368.tar.gz
$ cd ntp-dev-4.2.7p368
$ ./configure –enable-linuxcaps # takes 11-15 minutes
$ make
$ sudo make install
$ sudo /etc/init.d/ntp stop
$ sudo cp /usr/local/bin/ntp* /usr/sbin/
$ sudo /etc/init.d/ntp start
$ ntpq -crv -pn # optional step to check for version and basic function
# To configure gpsd to auto-start, enter: $ sudo dpkg-reconfigure gpsd

Three windows will appear, answer with the following:
(1) Device the GPS is attached to: /dev/ttyAMA0
(2) Options to gpsd: -n
(3) Gpsd control socket path: /var/run/gpsd.sock

PPS:

David Taylor provides methods for both kernel-mode PPS and user-model PPS. In an email, he told me kernel-mode is only needed for synchronization below 3 microsecond average jitter. I’m providing his steps for user-model PPS.

$ wget http://vanheusden.com/time/rpi_gpio_ntp/rpi_gpio_ntp-0.3.tgz
$ tar xvfz rpi_gpio_ntp-0.3.tgz
$ cd rpi_gpio_ntp-0.3
$ sudo make install

#To test that you are receiving a PPS on pin 24, exit with CTRL-C
$ sudo rpi_gpio_ntp -g 8 -d

rpi_gpio_ntp v0.3, (C) 2013 by folkert@vanheusden.com

NTP unit: 0
GPIO pin: 8
Fudge : 0.000000000
“Fork into the background” disabled because of debug mode.
1384662324.539383140] poll() GPIO 8 interrupt occurred
1384662325.016712554] poll() GPIO 8 interrupt occurred

# To set NTP to read the PPS from pin 24 (GPIO 8)
$ sudo rpi_gpio_ntp -N 1 -g 8
# Modify the ntp.conf file to the shared memory for GPSD
sudo nano /etc/ntp.conf

Replace these lines below:
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst

To:

server 127.127.28.1 minpoll 4 prefer
fudge 127.127.28.1 refid UPPS
pool us.pool.ntp.org iburst # For US ntp pool

CTRL-o to save then CTRL-x

Reboot NTP:
$ sudo /etc/init.d/ntp stop
$ sudo /etc/init.d/ntp start

Check that UPPS now shows values in microseconds:
$ntpq -np
================================================================
*127.127.28.1 .UPPS. 0 l 9 16 77 0.000 4.412 8.684

$ntpq -c rv
Autostart:
$sudo nano /etc/rc.local

The line before “exit 0” enter: /usr/local/bin/rpi_gpio_ntp -N 1 -g 8

For the curious, the section under peers in documentation about ntpq explains what the fields are when sending the command ntpq -np

WiFi Direct Access Point:

As I explained earlier, my current setup requires the pi to communicate through a home router. However, for testing it would be beneficial to have the smallest network delay I can manage.

– Richard Latimer (LinkedIn)

AutoIt, Macro Programming, and Why You Should Learn

If I were the head of a Department of Education, I would make AutoIt a mandatory tool learned in every high school. In terms of computer literacy, I’d rank it the most useful tool after Microsoft Office. In high school, I taught myself how to program using AutoIt and then at Rice University I taught a course, MURT 102, on how to use it.

The vision of the AutoIt authors is to make a simple scripting language designed for automating general computer tasks. Introduced in 1998, AutoIt simplified the complicated code and advanced programming skills needed by a user to configuring thousands of PCs to perform automated tasks for a variety of advance applications. Despite the sophisticated features, the BASIC-like programming syntax makes it a wonderful tool for both beginners and advanced programmers.

AutoIt programmers can simulate key presses, mouse movements, and window commands to automate daily tasks. These scripts have access to some of the low-level aspects of hardware such as the CD ejection tray or automated shutdown of the computer. In addition to automation scripting, AutoIt provides the ability to connect to common applications, such as Excel and FireFox, in addition to more advanced tools like SQL. AutoIt takes advantage of the Window’s operating system internal library commands (DLL) to perform these tasks. As an additional feature, programmed scripts can be compiled into a stand-alone executable for computers without an AutoIt interpreter, permitting programs to be instantly used on any computer and run by clicking a custom icon (very useful if you want to create a one-click button to empower your grandmother when she needs to compose a blank email to her best friend). Beyond simple programming syntax, AutoIt has excellent documentation with numerous examples. This documentation is complemented by the helped of the AutoIt community, which is very vibrant, encompassing tens of thousands of users.  By learning the ability to create automation scripts, individuals can be more effective with their time when performing tasks that are manual and tedious.

After learning about AutoIt, I added additional tools to my normal computer routines, which has enabled me to perform certain manual tasks probably 100x faster.  I’ve used it to automate program commands during my internships. I used this language to examine and create logic from text appearing in shipping software, by pixel searching for the text and asking for user-input at a critical decision moment, and then subsequently sending a string of keyboard inputs. I used the language to search 10,000 contacts from different Excel files and merge them together, to a reduced list of 3,000. In addition, this involved Regular Expressions to check for abbreviations and misspellings. I wrote a program that would constantly check the Rice University website for when a student dropped a course and text me so that I could register to enroll. I think these different experiences have provided me with the insight into the important features about AutoIt and why it has gained such as strong and supportive community. Although I own a Mac, I’ve programmed extensively in AutoIt, because the performance I gain from scripting is often too convenient. I’ll continue to buy future versions of the Windows operating system, because AutoIt is a killer application. You won’t know the full power of your PC unless you’ve used AutoIt.

Below I’ve attached the Power Point presentations from my course MURT 102. Although a few computer science students enrolled in my course, my goal was to target students of non-technical majors in a light 1-credit class to teach them programming concepts that would continue to be applicable in a student’s future because the tools learned complemented practical, everyday computer situations. The features I taught students was how one could control mouse movements, open applications, download files, send emails, and manipulate Excel. I hope my course material will provide students with instant gratification and inspire them to dabble in some of the lower-level languages.

Topics Learned:

  1. Week1 – Setup / SciTE Editor – Keyboard Commands: Students will learn to manipulate Excel from the front-end by simulating keyboard commands using the SciTE Editor.
  2. Week2 – For Loops – Mouse Movements: Students will learn to combine mouse movements to read text from the screen and display it in a Msgbox. Students will learn about for-loops and how this enables one to move the mouse in patterns. In addition, students will become familiar with the AutoIT Information Window.
  3. Week3 – Functions and HotKeys: Students will learn how keyboard keys can be tied to commands in a function. In addition, students will learn how to write custom functions used by their programs.
  4. Week4 – If Statements – Opening Applications and Window Manipulation: Students will learn how to request from the operating system different applications and instances of application windows. They will learn to use logic to check if the correct windows were discovered.
  5. Week5 – Debugging, Console Writing, PixelSearching: Due to the complexity of the code, students will learn how to debug their programs using the SciTE editor, Msgbox, and Tabtoolbars. In addition, students will learn a fast way of searching for text that does not involve mouse movements by pixel searching. They will use pixel searching to play an online game.
  6. Week6 – Arrays -String Manipulation: Students will learn how to process text content with a combination of string functions.
  7. Week7 – Using the Forum – Regular Expressions – UDFs and Downloading Web Content: Students will be introduced to the AutoIt community forum and the more advanced topic of string manipulation through Regular Expressions. Students will be introduced to User Defined Functions and the UDF needed for downloading web content.

A list of relevant links for learning more about AutoIt:

– Richard Latimer (LinkedIn)