EENG 383 – Dev + RPi configuration

Instructions for getting the RPi configured for EENG 383 without using the HDMI port
  1. Go to Raspberry Pi, click on RASPBIAN, and download the RASPBIAN STRETCH LITE Zip file. The download will take a while. Unzip the file and store it somewhere safe.
  2. Go to Etcher and download and install.
  3. Insert the SD card into your computer.
  4. Launch Etcher and flash the Raspbian zip to the SD card.
  5. When complete, close Etcher and identify the drive letter associated with boot partition of the SD card.
  6. Edit wpa_supplicant.conf file using notepad
    • Change ssid to match your home network name – this is case sensitive
    • Change psk to math the password of your home network
    • Make changes to CSM configuration to match your userid and password
  7. Copy ssh and wpa_supplicant.conf to boot partition.
  8. Insert the SD card into the RPi. Optional - attached an Ethernet cable between the RPi d your router. Next power-up through micro-USB adapter using a wall adapter or USB power brick – do not power from your PC USB port as it will not be able to provide enough power.
  9. Log into your router and get the IP address assigned by your router to the RPi.
  10. Launch putty:
    • Host Name: 10.96.1.105 // IP address from previous step
    • Connection type: SSH
    • Open
  11. From the terminal interface:
    • User name: pi
    • Password: raspberry
In the instructions that follow, there are many commands that you will need to issue to the RPi. In all cases things that are printed by the RPi are shown in courier font and all the things that you are to type are shown in bold courier font.

Create new super user

EVERY Raspberry pi comes with this default password making it a HUGE security vulnerability. First things first, let’s add you as a new user and give you super user priviledge.

Start by creating a new userID - I would advise using your CSM user id. In the instructions below replace "coulston" with your CSM user id.
pi@raspberrypi:~ $ sudo adduser coulston
Use a reasonablly strong password. After this is complete, edit the superuser file by typing the following:
pi@raspberrypi:~ $ sudo visudo
Scroll through the file and file the following area. Its on or about the second page down. Add your username and associated text on the line below the "root" user as shown below.
# User privilege specification
root    ALL=(ALL:ALL) ALL
coulston ALL=NOPASSWD: ALL
To check that you typed everything correctly,
pi@raspberrypi:~$ sudo reboot
Login as your newly created superuser account and then check that you have superuser priviledges....
coulston@raspberrypi:~ $ nano /test
You should see the message " [ Directory '/' is not writable ]". Now try this same command
coulston@raspberrypi:~ $ sudo nano /test
Instead of the warning, in its place you should see " [ New File ]". This is confirmation that you have super user priviledge. The next step is to delete the "pi" account as follows:
coulston@raspberrypi:~ $ sudo userdel -r pi
You can confirm that the pi user has been delete by listing all the users on the computer as follows:
coulston@raspberrypi:~ $ ls /home
Your account should be the only one. Nice work.

Configure interfaces

The RPi Software Configuration Tool (raspi-config) allows you to configure some important system-level features.
	pi@raspberrypi:~$ sudo raspi-config
Use the up and down arrows to select the numbered items. Use the right and left keyboard arrows to select <select> and <finish>
select:   5 Interfacing Options		<Enter>
	P2 SSH				<Enter>
	<Yes>	 			<Enter>
	<Ok>	 			<Enter>
select:   5 Interfacing Options		<Enter>
	P3 VNC				<Enter>
	<Yes>	 			<Enter>
	Do you want to continue	Y (wait a few minutes for install to complete)
	<Ok>	 			<Enter>
select:   5 Interfacing Options		<Enter>
	P6 Serial			<Enter>
	Would you like a login shell to be accessible… 	<No>	 	<Enter>
	Would you like the serial port hardware to be…	<Yes>	 	<Enter>
	<Ok>	 			<Enter>
select:	7 Advanced Options		<Enter> 
	A1 Expand Filesystem		<Enter>
	<Ok>	 			<Enter>
select:	<Finish>
	<Yes>	 			<Enter>
When you reboot your SSH session will be terminated. Just wait a few moments and re-login using the same IP address, userid pi, and your new password.

Network connection to your home network

The real power of a RPi is its ability to function as a standalone networked computer without any wired connections, a so-called headless configuration. To fully realize this, you will need to create a wireless network connection. Use the existing RPi setup and login to your pi and make the following changes to a key networking file.
pi@raspberrypi:~$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
In this file cut-and-paste the following contents.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
  ssid="myHomeNetworkName"
  psk=”pugpuppies”
  key_mgmt=WPA-PSK
  id_str="home"
  priority=1
}
Note that "myHomeNetworkName" should be replaced with your wireless network name. Note that "pugpuppies" should be replaced with your wireless password. "id_str" is the id_string for this network block (stuff between paraenthesis), and is needed because you will need to add the CSM wireless network later. The priority tells the wireless adapter to try to connect to this network first. Now to test that your wireless, reboot your RPi.
pi@raspberrypi:~$ sudo reboot now
You will loose your PuTTY connection. To restart it, wait a minues then right mouse click in the upper left corner of the PuTTY window (on the PC-to-PC lightening bolt) and select "restart connection". Then run
pi@raspberrypi:~ $ ifconfig

eth0: flags=4163  mtu 1500
        inet 10.96.1.105  netmask 255.255.255.0  broadcast 10.96.1.255
        inet6 2601:282:702:409:166:a36b:b6f7:3b50  prefixlen 64  scopeid 0x0
        inet6 fe80::f614:41e2:bae:5331  prefixlen 64  scopeid 0x20
        ether b8:27:eb:25:ed:ed  txqueuelen 1000  (Ethernet)
        RX packets 190  bytes 27840 (27.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 143  bytes 28846 (28.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163  mtu 1500
        inet 10.96.1.104  netmask 255.255.255.0  broadcast 10.96.1.255
        inet6 2601:282:702:409:a9e5:51c6:759f:1428  prefixlen 64  scopeid 0x0
        inet6 fe80::fdf0:ca7a:d3a8:2754  prefixlen 64  scopeid 0x20
        ether b8:27:eb:70:b8:b8  txqueuelen 1000  (Ethernet)
        RX packets 86  bytes 23780 (23.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 66  bytes 11171 (10.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Look at the eth0 section and you will see "inet 10.96.1.105". This is the IP address assigned to the RPi through the CAT-5 ethernet cable. Look at the wlan0 section and you will see "inet 10.96.1.104". This is the IP address assigned to the RPi through its wireless connection. Your IP addresses will most likely be different, but you need to have a valid wlan0 IP address in order to connect to the network.

Since it’s a bad idea to store passwords in plaintext, you should encrypt them whenever possible. Fortunately, my home network runs wpa-psk which allows me to encrypt my network password (pugpuppies). To do this, save and exit the file and then encrypt the psk passphrase as follows:
pi@raspberrypi:~$ wpa_passphrase myHomeNetworkName pugpuppies
network={
        ssid="myHomeNetworkName"
        #psk="pugpuppies"
        psk=7ecb80e7f6c847701458e71eda22b542757cad99ff38bb444149d202a2bfb7f8
}
Now copy the entire “psk=7ecb80e7…” string and edit the wpa_supplicant file. Replace the “psk=”pugpuppies” line with the “psk=7ecb80e7…”” line as shown the in following examples:
network={
  ssid="myHomeNetworkName"
  psk=7ecb80e7f6c847701458e71eda22b542757cad99ff38bb444149d202a2bfb7f8
  key_mgmt=WPA-PSK
}
After you make these changes reboot your Rpi with the ethernet cable unplugged and login using the wlan0 IP address.

Network connection to Mines network

If you followed the previous instructions, you will already be able to connect to your home wireless network. Let’s get you on the CSM wireless network. This is super easy. Before you start, you will need some basic network information from the ifconfig command, run as follows:
pi@raspberrypi:~$ ifconfig
eth0: flags=4163  mtu 1500
        inet 10.96.1.105  netmask 255.255.255.0  broadcast 10.96.1.255
        inet6 fe80::4464:81bd:549a:535  prefixlen 64  scopeid 0x20
        inet6 2601:282:702:409:dcad:bc4e:4dee:a281  prefixlen 64  scopeid 0x0
        ether b8:27:eb:25:ed:ed  txqueuelen 1000  (Ethernet)
        RX packets 8609  bytes 12506445 (11.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4558  bytes 447380 (436.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4099  mtu 1500
        ether b8:27:eb:70:b8:b8  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Make sure to record the MAC address (called “ether” by ifconfig) of the wlan0 adapter. In the example above this is b8:27:eb:70:b8:b8 and use it to register your RPi with the CSM Network Registration page Agree to the network policies and then complete the form using the MAC address of your wlan0 adapter. Once you complete this, login to your RPi and edit the wpa_supplicant.
pi@raspberrypi:~$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Then add the "school" block as shown below. Make sure to add it after your home network.
network={
  ssid="myHomeNetworkName"
  psk=7ecb80e7f6c847701458e71eda22b542757cad99ff38bb444149d202a2bfb7f8
  key_mgmt=WPA-PSK
  id_str="home"
  priority=1
}

network={
   ssid="CSMwireless"
   key_mgmt=NONE
   id_str="school"
   priority=2
}
You should be able to connect to the CSMwireless. The big problem that I am working on is how to determine the IP address assigned to you without having to connect the RPi to a console using the HDMI port. Fortunatly, the wireless IP address is pretty stable, meaning that it will not change that often.

Finding IP Address on Bootup

Because the way CSM has configured the wifi, the IP Addresses are not static and expire afer 30 minutes of inactivity. Not knowing the IP address makes working with a headless Raspberry Pi impossible because you cannot connect to it. One solution to this problem is to add a script at boot-up that will send you an email with your IP address. After you boot-up your Raspbery Pi (headless), open the email sent to you by your Raspberry Pi, get the IP address, then use SSH to connect with your Rasberry Pi. To do this follow along with these steps.
  1. Back on your personal computer, download pifinder.py Move this file to your Desktop. Open with Notepad, not MS Word, and edit in your email information. Your fromaddr will be your personal email (or a dummy email you can set up as well on gmail so you don't have your password to your real email on your Pi), the toaddr will be any email you can access, and the username and password will be your username and password to the “fromaddr” you entered. Note: I have not tried this with our MyMail accounts. I would recommend making a dummy Gmail account. The “fromaddr” MUST be a GMAIL account.
  2. Save the file and upload it to the /home/pi/ directory on your Raspberry Pi.
  3. Once the file is on your Raspberry Pi you need to make it executable by typing
    pi@raspberrypi:~$ sudo chmod +x /home/pi/pifind.py
    pi@raspberrypi:~$ /usr/bin/python /home/pi/pifind.py
    
    You should get an email.
  4. Now we need to make it run on boot up.
    pi@raspberrypi:~$ cd /etc/ 
    pi@raspberrypi:~$ sudo chmod 777 rc.local
    pi@raspberrypi:~$ nano rc.local
    
    Edit this script so that it looks like the one below by adding:
    sleep 60 
    /usr/bin/python /home/pi/pifind.py & 
    exit 0
    



    The sleep command causes this script to be inactive for 60 seconds after boot-up. The assumption is that the during these 60 seconds, the Raspberry Pi is able to connect to the CSM wireless network. The second command starts the email script. The "&" character runs the email script in the background. This is important, because if the email script gets caught in an infinite loop for some reason, it would hang the Raspberry Pi if the script was not running in the background. The exit 0 command tells the caller, that this script ran correctly.
  5. Check that everything is in order by rebooting the Raspberry Pi. You can do this by typing: pi@raspberrypi:~$ sudo reboot now
    You will have to wait a minute to get the email (sleep 60). Under the wlan0 category, use the inetaddr as your IP address when you ssh to your Raspberry Pi.

    RPi Serial interface

    Check that your serial port is working by connecting an oscilloscope probe to pin 8 of the RPi header (USART TX) and a ground lead to pin 39. The following command should produce activity on the TX pin.
    pi@raspberrypi:~$ echo “a” > /dev/ttyS0
    
    Keep the oscilloscope connected, we will want to test the python serial connection in a moment. Now, it’s time to configure the serial port to work with our development board by disabling the RPi from using the serial port to pump out diagonisitic message during boot-up as follows:
    pi@raspberrypi:~$  sudo systemctl disable serial-getty@ttyS0.service
    
    Next install Python's serial library.
    pi@raspberrypi:~$ sudo apt-get install python-serial
    
    Go to minterm and copy the python script into the buffer. On the RPi paste this code into a file called mini.py. Now run minterm to open a terminal interface to /dev/ttyS0
    pi@raspberrypi:~$ python mini.py /dev/ttyS0
    Enter port name: /dev/ttyS0
    --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
    --- Quit: Ctrl+]  |  Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
    
    Try sending some characters – you should see them appear in the USART TX pin on the RPi header. Note that miniterm is a terminal interface that runs on the RPi, allowing you to interact with the development board, in exactly the same way that PuTTY is a terminal interface that runs on a PC allowing you to interact with the development board. The PIC18F26K22 has two serial ports, one pointed to the PC through the FT232 chip (EUSART 2) and the other through the header at the top of the development board (via EUSART 1). If you are using the RPi, you will need to configure your programs to use EUSART 1 instead of EUSART 2 using the MCC. You will need to keep this change in mind – where you used to call “EUSART2_DataReady”, you will need to call “EUSART1_DataReady”. In order to test the interface between the serial connection, download lab9.hex from (http://ecse.bd.psu.edu/cmpen352/lab/lab9/lab9.hex) on to your development board through the PICKit3, solder in the 40 pin header, run miniterm and you should be able to execute the development board test program except through the RPi. You may find that you need to reset the PIC in order to get things working.
    pi@raspberrypi:~$ sudo chmod 660 /dev/ttyS0
    

    NGINX

    NGINX is a lightweight web server that will allow you to write embedded web apps. Start by installing NGINX. Before you start, you will need to know the IP address of your RPi as assigned by either your home network or the one assigned by CSM.
    pi@raspberrypi:~$ sudo apt-get install nginx
    
    Test your install by bringing NGINX up using the following command:
    pi@raspberrypi:~$ sudo /etc/init.d/nginx start 
    
    Now point your browser to the IP address of your RPi by typing in the IP address in the address bar of the browser. You should get a “Welcome to NGINX” web page. You will need PHP to interface tour development board to the RPi. Install using the following:
    pi@raspberrypi:~$ sudo apt-get install php7.0-fpm  
    
    Now point your browser to the IP address of your RPi by typing in the IP address in the address bar of the browser. You should get a “Welcome to NGINX” web page. You will need PHP to interface tour development board to the RPi. Install using the following:
    pi@raspberrypi:~$ sudo nano /etc/nginx/sites-enabled/default  
    
    Add index.php to the line index index.html index.htm; yielding index index.php index.html index.htm; Remove the “#” symbols from in front of the following – comment out the last line!
           location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    # With php-fpm (or other unix sockets):
                    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                    # With php-cgi (or other tcp sockets):
                    # fastcgi_pass 127.0.0.1:9000;
            }
    
    Remove the file /var/www/html/index.html and add a file index.php in its place containing the single line: <?php echo phpinfo(); ?> Point your browser at the IP address of your RPi and you should get a complex page with lots of PHP information.