Setting the Hand Controller Location on Boot

A project log for Raspberry Pi Driven Telescope Mount

full telescope control over WiFi using INDI on a RPi Zero W

Dane GardnerDane Gardner 11/26/2018 at 00:160 Comments

After using network time protocol (NTP) to set the hand controller's time on start, I was lamenting that I still had to set the location manually.  I could easily use the same script as a starting point to set the location using my network's IP address geolocation, so why don't I?

There are plenty of free services on the web to get an IP address' latitude and longitude.  I chose `` because it allows a few requests per day without a license, and it was easiest to extract the location using a CLI JSON parser called `jq`.  At the command line you can get your location like this:

$ curl -s | jq -r '.loc'

According to the Celestron protocol documentation for location setting, we need it in a degrees/minutes/seconds format with an extra byte for hemisphere.

The format of the location commands is: ABCDEFGH, where:
A is the number of degrees of latitude.
B is the number of minutes of latitude.
C is the number of seconds of latitude.
D is 0 for north and 1 for south.
E is the number of degrees of longitude.
F is the number of minutes of longitude.
G is the number of seconds of longitude.
H is 0 for east and 1 for west.

I was disappointed that I couldn't find any easy way of converting floating point degrees to DMS format at the command line in Raspbian.  As a rule I don't do math in Bash, so I had to write my own in Python.

def dms(deg):
    m = (abs(deg) - int(abs(deg))) * 60.0
    s = (abs(m) - int(abs(m))) * 60.0
    return (int(deg), int(m), int(s))

def celestron(deg):
    c = dms(deg)
    return "{0:d} {1:d} {2:d} {3:d}".format(abs(c[0]), c[1], c[2], 0 if(c[0] >= 0) else 1)

print "{0} {1}".format(celestron(lat), celestron(lon))

Combining these two concepts with the previous script for setting the time, I came up with a couple scripts, and kicked them off with the `/etc/rc.local` script so it runs at boot.  This works surprisingly well from my home network, which is a little scary from a privacy perspective.