Telescope Control With a Raspberry Pi


Overview


The Idea


For several years now, I own a small telescope, that I regularily use to gaze at the stars. It is a Celestron NexStar 102 SLT, with an 102 mm opening and 660 mm focal length. It is a computerised GoTo-Telescope, that can be easily controlled by a small handset.

For identifying object in the night sky, I use the widely used tool Stellarium, which is not only a versatile star chart, but also very beautiful. So what do you do, if you have a computerised telescope and a star chart on the laptop PC? Right, connect them.
Marrying my NexStar with Stellarium should be easy enough: Stellarium comes with native support for Celestron telescopes, so no third party software is required.
Now I'm by far not the first to do this, but I wanted a solution, that fits my needs, and I wanted to try this for myself. So, I will not use a precompiled recipy, but play around with it for myself.

I will, however, make use of preexisting software; why reinvent the wheel, if it works?

Setup telescope



The Setup

The telescope will be setup with a Raspberry Pi computer as a remote controller, and the Raspberry Camera module will be used to take pictures,
Note-- This will not work for wireless dongles that use the RTL8192cu chipset. See details below!
and send them back to a PC. Since I don't want any cables to run from my Raspberry Pi to the Computer, we will setup the Raspberry as a Wifi-Access-Point, which will allow us, to communicate over the network.
The Rasperry is connected to the telescope via a USB-Serial Connector, which you can get for a few quid in any electronics store.

Hardware


To set things up, you need several parts:
Shows all parts of the project, that are in the list below


The Serial Cable


Ever since the RS232 port has come out of fashion, connector cables have been hard to get.
Celestron has a connector cable, and another option is offered by Baader-Planetarium. Both options work fine, but, as a downside, €45 for a simple RS232 cable is pretty expensive. It's a lot cheaper to build one yourself.

The NexStar's hand control's underside, showing the RJ-22 socket


I bought a 9-pin Sub-D connector for about €2 and a 2 meter handset cord for less than 50 cents. Be aware that most online stores market the cable as "RJ-11 cable for NexStar", the connector however, is, in fact, a RJ-22, with 4 instead of 6 pins! Since most landline phones, like those used in offices, use this jack, you're on the safe side, buying simple handset cord.

Soldering the cable is actually pretty easy. The NexStar only uses 3 of the four lines, Rx, Tx and GND. If you look at the jack from top, pin 1 is on the left. Since the jacks are transparent, you can see the different-colored wires at the end of each pin. The colour coding of these wires varies from country to country, but you need only to look at what colour is associated with each pin.

Now cut off the RJ-22 jack at one side of the handset cord, and solder the pins to the corresponding pins on the Sub-D connector.

Connect Pin 1 of the telephone cable to Pin 3 of the Sub-D connector, Pin 3 to Sub-D 5 and Pin 4 to Sub-D 2.
This should get your cable to work.

Setting up the Raspberry Pi

To setup your Raspberry Pi, you first need to install an operating system on the disk. Since i prefer to have a maximum of configuribility with my devices, I use Archlinux as my distribution of choice.
Everything in this tutotial can easily be transferred to use it with another distibution such as Rasbian. Archlinux has the advantage of being really compact and fast, and it doesn't come with loads of preinstalled stuff such as XOrg, that we won't need on a headless system.

Other than Rasbian, the bootloader of Archlinux does not automatically determine the architechture of your hardware. So you need to install the correct image, depending on what Raspberry Pi model you use.
The images are basically the same, but the kernel is compiled for the ARM-architecture of the respective model.

After you finished installing the system, the Raspberry Pi should boot up. If you're running headless, you should be able to SSH into the device with the user name root and password root.

After successfully logging into your Raspberry Pi, you should update the base system. To do so type:
pacman -Syu


Next, you should create a user, that you normally log into your system with. Logging in as root, especially over a network, is considered bad style, and insecure.
I will use the user ts (for telescope) for this example, but you can choose any username you like. Just replace the username ts with your choice
Use the command
useradd -m /home/ts ts


to create a new user.

Now set a password for the new user by typing
passwd ts


Enter your password twice to confirm it.

If you, like me, prefer the vim editor over the preinstalled nano, you can install it now with
pacman -S vim


Next, we need to allow our new user to get root privileges, when required. For this, we use sudo
pacman -S sudo


Then, we edit the sudoers file by uncommenting (remove the # character) the following line in /etc/sudoers:
# %wheel ALL=(ALL) ALL


Finally,. we add the new user to the group wheel by typing
gpasswd -a ts wheel


Now disconnect from your Raspberry Pi, and try to SSH into it, using the new user. You should now be able to get root privileges, by typing sudo before any command. Try this out, for example, by typing
sudo cat /etc/sudoers


This will ask you for your password, and then execute the cat command. If it works, open the file /etc/ssh/sshd_config in your editor, and replace the following line
# PermitRootLogin yes

with
PermitRootLogin no


This will disallow users to connect via SSH as user root. Technically, this is not neccessary, but it is considered best practice for safety.

Network setup

Since we want to comfortably control our telescope from a laptop, we need to connect to the Raspberry Pi in a convenient way. To achieve this goal, we will use the wireless connection as an access point.
Since the telescope opereration doesn't require internet access, out access point will not route traffic anywhere, but only allow access to the telescope itself.

The network setup has to meet the following requirements:
The first requirement is already met, because this is the default configuration for the ethernet port.

First, test if your wireless dongle works, and is recognised by the kernel. The command
lsusb

should be producing something like this:

Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub


I'm using the Edimax EW-7811Un dongle, which is very popular with the Raspberry Pi, because it's cheap and it works.
There a some problems, especially because the new iw tool doesn't recogize the RTL8188CUS chip, so you have to use the outdated wireless_tools for analysing, but the dongle itself works fine.

The kernel module should be installed and loaded by default. You can check this by typing
lsmod |grep 8192cu


If you don't see an output like this:
8192cu                530762  0

you need to install the module manually from the packages:
sudo pacman -S dkms-8192cu


Next we need to set up the interface wlan0. I use archlinux' very own netctl tool for this. Simply edit a new file /etc/netctl/wireless_ap (don't forget to call your editor with sudo!):
wireless_ap
Description='A basic static setup for a wireless AP connection'
Interface=wlan0
Connection=ethernet
IP=static
Address=('172.17.1.1/24')
#Routes=('192.168.0.0/24 via 192.168.1.2')
#Gateway='172.17.1.1'
DNS=('172.17.1.1')
SkipNoCarrier=yes

## For IPv6 autoconfiguration
#IP6=stateless

## For IPv6 static address configuration
#IP6=static
#Address6=('1234:5678:9abc:def::1/64' '1234:3456::123/96')
#Routes6=('abcd::1234')
#Gateway6='1234:0:123::abcd'


This will configure the interface wlan0 with address 172.17.1.1 in the network 172.17.1.0/24. Any private network will do here, but make sure, that you choose a network that differs from the one you are in!

Now you can bring up the interface with
sudo netctl  start wireless_ap


If everything worked,
ip addr show dev wlan0


should show something like this:
5: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
	link/ether 80:1f:02:a1:a2:44 brd ff:ff:ff:ff:ff:ff
	inet 172.17.1.1/24 brd 172.17.1.255 scope global wlan0
	   valid_lft forever preferred_lft forever
	inet6 fe80::821f:2ff:fea1:a244/64 scope link 
	   valid_lft forever preferred_lft forever


Next we need to make sure the interface comes up at boot time:
sudo netctl enable wireless_ap


This will make systemd create a service entry for the configured interface, and run it at boot.

Now that our interface works, we need to make it an access point. This can easily be done in Linux with software, by installing the hostapd daemon. You can install it with
sudo pacman -S hostapd


If you are using an RTL8188cus based wireless dongle, this sadly will not do, because it doesn't support the 8192cu chipset. Lucky for us, there is a patched version of the hostapd daemon in the AUR.
The AUR us the Archlinux User Repository, and contains thousands of packages, that are not part of the official package-tree. To install from the AUR, you need the base-devel group packages to be installed:

sudo pacman -S --needed base-devel


Now you can create a local repository, where you store the aur files and the built packages. It's good practise to have a local folder for this in your home folder:
mkdir ~/aur


You can now download and install the package you need.
cd ~/aur
curl -o hostapd-8192cu.tar.gz https://aur.archlinux.org/packages/ho/hostapd-8192cu/hostapd-8192cu.tar.gz
tar -zxvf hostapd-8192cu.tar.gz
cd ./hostapd-8192cu
makepkg -s
sudo pacman -U hostapd-8192cu.tar.xz


Next we need to configure the hostapd. Edit the file /etc/hostapd/hostapd.conf
hostapd.conf
# sets the ssid of the access point, I use the name of my telescope
ssid=CelestronNexStar
# sets the interface to bind to
interface=wlan0
# I'm using channel 7, any other channel is also fine. Carful: check what channels are legal in your country
channel=7
# driver to use; I use the edimax 7811un, most others need nl80211
driver=rtl871xdrv
# sets the wifi mode. '
g' usually is safe, because it enables backwards compatibility, if the connecting device uses older standard
hw_mode=g
# don'
t log
logger_stdout=-1
logger_stdout_level=2
# The maximum clients, that can connect at a time
max_num_sta=5
# setting this to 1 will disable the broadcasting of ssid
ignore_broadcast_ssid=0

#Sets authentication algorithm
#1 - only open system authentication
#2 - both open system authentication and shared key authentication
auth_algs=1

#wpa option sets which wpa implementation to use
#1 - wpa only
#2 - wpa2 only
#3 - both
# you should only use wpa2, for wpa is considered unsafe
# works without, but since we're going to pipe the telescope control through,
# we don'
t want anyone to mess with our connection.
wpa=2

# set a passphrase
wpa_passphrase=<MyPassphrase>
# the wpa key-management
wpa_key_mgmt=WPA-PSK
#sets encryption used by WPA
rsn_pairwise=TKIP
#sets encryption used by WPA2
rsn_pairwise=CCMP


Using WPA security might seem uneccessary, since the device will not be connected to the internet, but it is a good idea, because your telescope server will run on this connection, and you don't want anyone to mess with your telescope, while you are stargazing!

Now you can test run the server
sudo hostapd /etc/hostapd/hostapd.conf


Now you should be able to see the SSID CelestronNexStar on your client, when you scan for networks.
End the running server by pressing Ctrl+C, and configure the service:
sudo systemctl enable hostapd.service
sudo systemctl start hostapd.service


To be able to log into our wireless network, we still need to be able to setup the client and feed it an ip address.
For simplicities sake, I use dnsmasq for this task:
sudo packman -S dnsmasq


Edit the configuration file /etc/dnsmasq.conf and append the following lines:
dnsmasq.conf
# Interface to bind to
interface=wlan0
# Specify starting_range,end_range,lease_time
dhcp-range=172.17.1.3,172.17.1.20,12h


Keep in mind, that the interface line is of the utmost importance! If you do not use it, the dhcp-server will also wait for connections on the ethernet interface, which can seriously confuse your network, if there are two concurring dhcp-servers at work.

Start and enable the dhcp-server with
sudo systemctl enable dnsmasq.service
sudo systemctl start dnsmasq.service


That's it! Now you should be able to connect to your Raspberry Pi over wireless, like with any other access point.

The Telescope Server

Since we want to control the telescope on a different machine than the one Stellarium runs on, we need a means of communication between the two devices. Luckily, Stellarium allows us to communicate witth any server, that supports TCP/IP, as long as the telescope plugin supports the protocol.

The server will run on the Pi, and listen on a specific port for connections from Stellarium. I'm using Johannes Gajdosik's server, which supports the dummy telescope server, the Meade LX200 and all telescopes that are NexStar compatible.
The server needs to be built, so you need to install the build-essentials, of you have not already done so earlier.

sudo pacman -S --needed base-devel


There is no AUR-package, so we need to download from the subversion repository. In order to check this out, we install the subversion client:

sudo pacman -S subversion


Then we can check out the repo:

svn co https://svn.code.sf.net/p/stellarium/code/trunk/telescope_server


Now we an build the servers:

cd telescope_server/src
make


On the Raspberry Pi, this might take a while.

The make process should have created three executables. Copy them to /usr/local/bin

sudo cp TelescopeServer* /usr/local/bin


Now we can begin with our first test:

The usb-to-serial converter will appear on your raspberry when you type
ls -l /dev/tty*


Usually a USB-to-serieal device will appear as /dev/ttyUSB0, but some devices have other names, like
/dev/ttyACM0.

Now we can start our telescope server. The device is usually owned by root, so we need to sudo it.
sudo TelescopeServerNexStar 10001 /dev/ttyUSB0


This will make the telescope server listen on port 10001 for connections.

Now we connect our computer to the telescope network and connect
Stellarium to it.
Now you can click the Connect button to test if your telescope can be controlled.
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki