Saturday, October 26, 2013

Clojure on Raspberry Pi, Emacs and NFS

*markdown-output*

Making Clojure on Raspberry Pi workable

I'm building a weather station on my Raspberry Pi and coding it in Clojure. The main trouble is that it takes a few minutes to fire up a repl on the Pi. I've found what I think is a tolerable setup using NFS and connecting to a remote repl using emacs as I'll illustrate below. This setup moves most of the heavy lifting off of the pi and makes the development cycle quite responsive.

My weather station is called cows.

The raspberry pi is assigned 10.0.1.3 and the Mac I'm using to connect to the Pi is assigned 10.0.1.2.

Install the Java 8 preview with Hard Float.

  • Download the java.tar.gz from oracle and transfer it to the pi.
  • sudo tar xzvf java.xxx.tar.gz -C /usr/lib/jvm For the the code to have access to the low level input-output capabilities of the pi it must be run as root. This necessitates a few complications.
  • add /usr/lib/jvm/java.xxx/bin to sudo's secure_path with sudo visudo. For me sudo visudo looks like this:
Defaults        env_keep +="JAVA_TOOL_OPTIONS LEIN_ROOT"
Defaults        mail_badpass
Defaults        secure_path="/usr/lib/jvm/jdk1.8.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Install Leiningen as described here

put it in /usr/local/bin so that it can be used by su

Create a leiningen app

  • lein new app cows
  • Edit the project.clj to include :repl-options {:timeout 300000}. Yeah it takes a really long time!

Install tmux

  • sudo apt-get install tmux

Mount the Pi over NFS with a mac client

I had some trouble with this. Finally following these suggestions I had success.

  • sudo apt-get purge nfs-common rpcbind
  • sudo apt-get install nfs-common rpcbind
  • sudo apt-get install nfs-server
  • nano /etc/exports and add /home/pi/cows *(rw,insecure,all_squash,anonuid=1000,anongid=1000) This will allow all IPs to connect, since my pi is behind a router, this should be OK. /home/pi/cows is my leiningen app folder. 1000 is the uid of the 'pi' user on the Pi.
  • sudo /etc/init.d/nfs-kernel-server restart
  • then from within osx finder. Go => Connect to Server. Enter nfs://10.0.1.3/home/pi/cows and it connects
  • now reboot the pi and wait for it to come back up
  • Connecting through the finder still works.

Start a headless repl on the Pi

On the Pi type:

  • tmux
  • sudo lein repl :headless :host 10.0.1.3 :port 4567. 10.0.1.3 is the IP address of my pi.
  • Go and get a cup of coffee!

Connect to the Pi Repl from the mac

On the mac type: LEIN_REPL_PORT=4567 lein repl :connect 10.0.1.3:4567/repl. This should give you a repl connected to the pi.

Connect to the pi repl from emacs

Assuming you have a working emacs nrepl.el setup.

  • On the mac, go to the Pi's NFS mount and edit a file in in the pi's leiningen app folder. For me this is /Volumes/cows/src/cows/core.clj
  • M-x nrepl. Enter the IP and port of the Pi's repl from above. You should be in! Edit the file and execute code as if you are working with clojure code on the local machine.

1 comment:

  1. Hey I'm setting up an Intel Edison using the same approach and found this helpful. Thanks!

    ReplyDelete