Thursday, 28 May 2015

Setting the time on a Raspberry Pi over HTTP/without NTP

One of my Raspberry Pis is installed in a location with a restrictive firewall, which means it is not possible to get the time and date using the NTP service, and I am not aware of a local NTP server.

I found an alternative which was to update the time over HTTP by using www.timeapi.org to request the time and date, in the correct format, and then use this to set the time. To set the time, I copied the code posted to the Raspberry Pi forums by ytsentas however with a modification.

The problem was that the URL originally specified the "I" flag for the format, which requests the number of hours in a 12 hour format. So changing this to "H" gives a 24 hour number. More information about the formating string can be found here (original website is no longer available).

The following batch script can be used to update the time and date.
#!/bin/bash
# Visit www.timeapi.org to find the correct url for your timezone. Then replace the url in the first line

time=$(wget http://www.timeapi.org/utc/in+one+hours?format=%25d%20%25b%20%25Y%20%25H:%25M:%25S -q -O -)
echo "Time set to:"
sudo date -s "`echo $time`"

The script needs to be saved as filename.sh, and then run the command
chmod 755 filename.sh
Which will allow the script to be executed. To execute the script from the same directory, enter:
./filename.sh

Wednesday, 22 October 2014

Python Find Serial Ports - Windows and Raspberry Pi

I started with code taken from stack overflow at the following page: http://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python with the code posted by user "Thomas" (code has since been modified). I tested the code on Windows 7 and a Raspberry Pi. The original 'unix' code did not work for me, so I found a method that did.
import sys
import glob
import serial
def ScanPorts():
    """
    Returns a generator for all available serial ports
    """
    if os.name == 'nt':
        # windows
        for i in range(256):
            try:
                s = serial.Serial(i)
                s.close()
                yield 'COM' + str(i + 1)
            except serial.SerialException:
                pass
    else:
        # unix
        ports = glob.glob('/dev/tty[A-Za-z]*')
       
        for port in ports:
            yield port
To run this on the Raspberry Pi, first the python serial package must be installed by running:
sudo apt-get install python-serial
Also the software package "Device Monitoring Studio" found online here: http://freeserialanalyzer.com/ was useful for peeking into what messages were being sent and received from another program. The free version has a 22 minute time limit.

"yarn" in R

Whilst trying to follow the PCA tutorial from the following page http://nir-quimiometria.blogspot.de/2012/02/pca-for-nir-spectrapart-001-plotting.html I couldn't find the "yarn" dataset, and couldn't find any information about where it was from, whether it was bundled with the R source etc. Trying to evaluate yarn simply gave the following:
Error: object 'yarn' not found
After some searching I stumbled across the documentation for the pls library. It turns out the data is bundled with the pls library. Running the following allowed me to use the dataset:
install.packages("pls")
library("pls")
Now on with the rest of the tutorial!