Earlier in this thread I used xpra to make a window running Mathematica from the Zero appear directly among the local windows on the host computer. Since the version of xpra that ships with Raspbian is old, I installed a new version of xpra from source. While this looked great, it resulted in more computational overhead than when using RealVNC. I planned to try with xorgrdp. However, this is again a situation where an install from source is necessary for a valid comparison. Having other things to do, I've decided to skip that step and go with what is working now. Here is my final method for making a Mathematica USB dongle.
Download a copy of full Raspbian 2018-04-18-raspbian-stretch.zip unzip it and copy it to a new SD card. I used an 8GB card--larger ones are fine. Mount the DOS partition of the card, also known as the boot partition. To the end of config.txt add
dtoverlay=dwc2
framebuffer_width=1280
framebuffer_height=1024
Since the Zero will be running headless, we need to specify the default size of the video frame buffer, otherwise the resulting desktop will be very tiny. Since I have a 1920x1200 resolution monitor on my host computer, setting a size of 1280x1024 yields a reasonable window into the Zero later. As far as I can tell, many different dimensions will work, though it is possible that odd sizes may affect hardware acceleration later.
Next change cmdline.txt to
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether g_ether.host_addr=02:46:c0:a8:0d:02 g_ether.dev_addr=02:46:c0:a8:0d:01 quiet init=/usr/lib/raspi-config/init_resize.sh splash plymouth.ignore-serial-consoles
Note that the above file consists of only one very long line. Create an empty file in /boot called ssh to enable secure login
$ touch ssh
and further a file called wpa_supplicant.conf that will be automatically copied to the main partition upon booting that contains
Code: Select all
country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="your_real_wifi_ssid"
scan_ssid=1
key_mgmt=WPA-PSK
psk="your_real_password"
}
Unmount the SD card, place it in the Zero W and then boot it by plugging it in to the USB port of the PC which will serve as the host computer. Upon first boot, the Zero will expand the root filesystem to fill the SD card, create a swap file, enable secure login, switch the USB port to device mode, load the gadget driver and connect to WiFi. Although we will connect through USB momentarily it is easiest to log in first through WiFi and perform some additional configuration. To do this, check the DHCP lease table in your WiFi router. Mine looks like
which indicates that my Pi is on 192.168.46.138. Connect using any secure shell client such as putty on Windows or slogin on Mac or Linux. I'm using a Linux PC as the host computer so I'll type
$ slogin 192.168.46.138 -l pi
with the default password of raspberry to log in. The first thing to do is change the password with the command
pi@raspberrypi:~ $ passwd
note that this step should not be done using a public WiFi because the default password is well known and you will also need access to the router to figure out the actual IP number that was assigned to the Zero.
There is an alternative method to configure things in which first-time login is through the USB connection. This other method requires either editing files on partition two of the SD card which is Linux format using another Pi computer, or installing the Bonjour protocol on the host computer in order to make the initial connection to the Pi. I will not discuss these alternatives further here.
Procede by finishing the configuration of the USB gadget driver. First install some software with
pi@raspberrypi:~ $ sudo bash
root@raspberrypi:/home/pi# apt-get update
root@raspberrypi:/home/pi# apt-get install isc-dhcp-server
You will get a bunch of systemd startup failure messages. Failure seems to be the expected mode of operation for systemd and comes from the fact that we haven't actually had time to configure the dhcp server. Let's do that now first editing the file /etc/default/isc-dhcp-server and changing the INTERFACESv4 line to read
INTERFACESv4="usb0"
Then move the default /etc/dhcp/dhcpd.conf out of the way and create a new one which contains exactly the lines
Code: Select all
ddns-update-style none;
subnet 192.168.13.0 netmask 255.255.255.0 {
range 192.168.13.128 192.168.13.254;
}
host garden {
hardware ethernet 02:46:c0:a8:0d:02;
fixed-address 192.168.13.2;
}
The above file consists of two rules: The first rule assigns any computer at the other end of the USB cable an IP number in the 192.168.13.x subnet. The second rule specifically assigns the number 192.168.13.2 to MAC address 02:46:c0:a8:0d:02 which is supposed to be the other end of the USB cable. While the second rule should be enough, there appears to be an issue with USB gadget driver where it sometimes mixes up the MAC addresses. Having both rules allows things work in either case. If anyone has a solution that fixes this problem, I think many people would be interested.
Now configure the USB gadget to assign a static IP address to the Zero end of the cable. This is done by creating the file /etc/network/interfaces.d/usbgadget which contains the following
Code: Select all
allow-hotplug usb0
iface usb0 inet static
address 192.168.13.1
netmask 255.255.255.0
and then editing /etc/dhcpcd.conf to add the line
denyinterfaces usb0
at the top to prevent a race between the two network configuration tools. Be careful not to get the similarly named dhcpcd.conf and dhcpd.conf files mixed up. At this point we can reboot the Pi Zero and obtain a properly configured USB connection. Reboot using the command
root@raspberrypi:~# reboot
In less than a minute the Zero should be up and running. It will now be possible to log in through the direct USB connection. Check the host computer to see if a USB Ethernet device is present and whether it has been assigned an IP number. If it does not have an IP number, you may have to click with your mouse to enable DHCP on it. On my Linux PC I can type the command
Code: Select all
# dhclient usb0
# ifconfig usb0
usb0 Link encap:Ethernet HWaddr 02:46:c0:a8:0d:02
inet addr:192.168.13.2 Bcast:192.168.13.255 Mask:255.255.255.0
inet6 addr: fe80::46:c0ff:fea8:d02/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:31 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:656 (656.0 B) TX bytes:7627 (7.4 KiB)
to obtain a lease. Connect to the Pi using secure login with the address 192.168.13.1. For example
$ slogin 192.168.13.1 -l pi
Don't forget to use the new password that you created earlier. At this point it is reasonable to check that the hardware is working properly. For example, if you are using a bad cable or connecting though an old hub, you might end up with a full-speed 12Mbps connection rather than a high-speed 480Mbps one. To check the USB network speed install iperf3 on the Raspberry Pi and the host computer. On the Zero type
pi@raspberrypi:~ $ sudo apt-get install iperf3
pi@raspberrypi:~ $ iperf3 -s
On the host computer do something similar and finally type
$ iperf3 -c 192.168.13.1
Output from a misconfigured USB connection will look like
Code: Select all
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.00 sec 6.21 MBytes 5.21 Mbits/sec 0 sender
[ 4] 0.00-10.00 sec 6.19 MBytes 5.19 Mbits/sec receiver
which is super slow. If this is the case, shutdown the Pi using the command
pi@raspberrypi:~ $ sudo shutdown now
wait for the green light to blink four times, remain steady and then go out. Now plug your Pi Zero dongle into a high-speed port and try again. Note that if you had to start dhclient manually, you will have to stop and start it again to obtain a new DHCP lease after the Zero reboots. Most PCs offer continuous power to their USB ports while plugged in. It is therefore also possible to reboot the host computer while the Zero is running. If you are having trouble getting high speed this may help.
For my PC in particular, if the Zero is the only USB device connected and I reboot the Pi, then the USB port gets stuck in full speed mode. Rebooting the PC while leaving the Pi running immediately switches to the correct high speed mode. On the other hand, if there other high speed devices already connected then there are no problems either way.
Output from a correctly configured USB connection will look like
Code: Select all
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.00 sec 173 MBytes 145 Mbits/sec 0 sender
[ 4] 0.00-10.00 sec 173 MBytes 145 Mbits/sec receiver
The resulting bandwidth is greater than a 100 Mbit Ethernet connection but slower than the 1Gbit wired networking on the new Pi 3B+. As a point of comparison it is interesting to compare these results with WiFi. Since WiFi is a broadcast medium, its speed depends on how many other WiFi devices are active within range of the radio transmitter and receiver. In my case, testing the WiFi connection yields
Code: Select all
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.00 sec 5.83 MBytes 4.89 Mbits/sec 5 sender
[ 4] 0.00-10.00 sec 5.78 MBytes 4.85 Mbits/sec receiver
which is even slower than the incorrectly configured USB cable. Note also that there were 5 retransmissions during the test, which indicates a few packets were lost due to radio interference. As an aside, the above numbers indicate why building an
OctaPi using WiFi is less effective than building
one using wired networking. What remains is to set up RealVNC to allow running Mathematica in a window on the host computer.
From this point on we can follow the documentation from the
Raspberry Pi Foundation and from
RealVNC. RealVNC is already installed on the Zero; however, it needs to be enabled. Log into the Zero and type
pi@raspberrypi:~ $ sudo raspi-config
then select Interfacing Options -> VNC -> Yes. Then, exit the program.
RealVNC may not already be installed on the host computer. Fortunately, the needed
RealVNC viewer is a free download for Windows, Mac OS/X and Linux as well other commercial Unix systems and even cell phones. Download the correct version for your host computer. It is now possible to connect to the graphical desktop of the Zero through the USB cable.
Start vncviewer on the host computer. Click on File -> New Connection and fill out the form as indicated
click OK after which an icon labeled raspberrypi should appear in the VNC viewer window. It is fine to disable encryption because all communication is already private using a point-to-point connection over the USB cable. Now double-click on the icon in the viewer, click Continue, set the user name to pi, enter the password for the Zero and click OK. After a moment the full Raspbian desktop will appear exported from the Raspberry Pi to the screen of the host computer. Start Mathematica and you're done.