RaspberryPi

Here are my notes on playing with the Raspberry Pi.

In general, the thing just works!! I was able to fairly quickly get it powered up and going. Here are a few quirks to note:

New Pi 3.0 Setup

I recently bought a new Raspberry Pi 3.0 version, and am finally getting a chance to set it up.

  • SD Card Setup

    • As mentioned below - Downloaded NOOBS 2.1.0 from the RaspberryPi.org website.

    • The zip file has to be downloaded to a laptop, then copied to a (8 or 16G) microSD card, then unzipped.

    • Note that when unzipping the file, set the destination to the TOP LEVEL of the SD card. By default, it will want to put it into a NOOBS folder - that won't work.

    • Once the file is unzipped, install in the Pi, plug in keyboard, mouse, HDMI, and power. It should boot up.

      • Set the keyboard language (English - US), then start the OS install.

      • It can take something like an hour or more to unpack and install the OS.

  • System Setup

    • Menu -> Preferences -> Raspberry Pi Configuration

      • Set the host name.

      • Change the password (default is "raspberry")

      • Under "Interfaces", I enabled:

        • SSH

        • VNC

        • Serial

        • Remote GPIO

      • Under "Localisation", set Country and Timezone (use "Chicago" for Austin), and WiFi Country Code (US).

  • Wireless Setup

    • Top tool bar - Wifi configuration is on Right hand side. Click Icon

    • Enable Wifi, then search for the correct connection, click - and enter password for the network.

  • VNC Setup

    • It appears that VNC Server is already installed on the Pi by default.

    • VNC server has UPDATED since the notes below were written... It now wants to "cloud connect". See this web page:

      • https://www.realvnc.com/en/connect/docs/raspberry-pi.html

      • On your Raspberry Pi, select "Licensing" from the VNC Server status menu, choose "Sign in to your RealVNC account", and enter your new account email and password.

      • Use VNCviewer on the laptop client to connect.

    • OLD notes for VNC:

      • It can be started by bringing up a command prompt and typing: vncserver :1 ## space between "vncserver" and ":1".

      • Then start the VNCserver client on the laptop. Use "pi" as the user, with the pi password to access.

  • sudo apt-get update

  • Install emacs:

    • sudo apt-get install emacs

  • Tried to update for the .aliases file - haven't gotten this to work yet. .aliases file isn't reading correctly

      • Edit .bashrc to include the following code for sourcing the .aliases file:

if [ -f ~/.aliases ]; then

source ~/.aliases

fi

Add a .aliases file with the following code:

# .aliases file

alias em='/usr/bin/emacs -nw '

  • Set default shell to tcsh:

    • sudo apt-get install tcsh

    • chsh -s /usr/bin/tcsh

    • Then you MUST reboot to get the update.

  • To set up .aliases:

    • First do the above to set up tcsh shell

    • emacs ~/.tcshrc

      • Inside that file, add the line:

source ~/.aliases

  • Set up VNC on startup:

$ cd /home/pi/.config

$ mkdir autostart

$ cd autostart

$ nano tightvnc.desktop (or use emacs...)

In that file, add the following text:

[Desktop Entry]

Type=Application

Name=TightVNC

Exec=vncserver :1

StartupNotify=false

After re-booting the Raspi, download and use VNC-Viewer on a PC to log into the Raspi using the IP address from 'hostname -I', and the password that was created when vncserver was manually run above.

  • Installed Apache Server!

    • sudo apt-get install apache2 -y

    • To test, to go a web browser on another computer, and enter the IP address of the Pi.

    • Default web page is at: /var/www/html/index.html

    • Install php:

      • sudo apt-get install php5 libapache2-mod-php5 -y

mv /var/www/html/index.html /var/www/html/orig.index.html

PHP Example is at: /var/www/html/index.php

  • Web Cam:

    • sudo apt-get install fswebcam

    • Short help: fswebcam -h

    • Man page: man fswebcam

    • Operation:

      • To find cameras, try: ls /dev/vid*

      • Simplest command to take a picture: fswebcam myfile.jpg

        • Defaults to first camera found

      • To specify video source (You can hook up multiple cameras!): fswebcam -v -d /dev/video1 testvid01.jpg

  • GPIO Pins:

    • Tested, and work just like GPIO section documentation below.

    • IMPORTANT NOTE - Pins are 3.3V!!!! Numbers are the GPIO numbers, NOT the pin number of the connector.

    • Easiest way to handle them is with command lines:

To INITIALIZE a port:

echo 11 > /sys/class/gpio/export

The above command sets up the file structure for GPIO port 11

To REMOVE a port:

echo 11 > /sys/class/gpio/unexport

To set the DIRECTION of a port:

echo out > /sys/class/gpio/gpio11/direction

echo in > /sys/class/gpio/gpio11/direction

To WRITE to a port:

echo 1 > /sys/class/gpio/gpio11/value

echo 0 > /sys/class/gpio/gpio11/value

To READ a port:

cat /sys/class/gpio/gpio11/value

  • External Flashdrive:

    • Seems to mount automatically!

    • /media/pi/<volume_name_of_flash_drive>

  • Analog-Digital Conversion - See web page for ADC_MCP3008

  • Cool feature: SD Card Copy Feature!!! I'm hopeful this will allow me to make a full backup of the SD card after I have everything configured and installed. Looking forward to trying this out! Menu -> Accessories -> SD Card Copier

    • This feature doesn't seem to work???

    • Also see Menu -> Accessories -> Image viewer (and PDF viewer).

New Pi Setup:

    • SD Card Setup:

      • One option is to buy a version of NOOBS pre-installed on a SD / MicroSD card. Otherwise, I have had some success just downloading and unzipping NOOBS onto a clean SD / MicroSD card. If that doesn't work, try formatting as described below:

      • Downloaded the SDFormatter from: https://www.sdcard.org/downloads/formatter_4/eula_windows/index.html

      • Note - used SDFormatter to reformat and extend the size of the card. Watch options carefully!

        • Original card had FAT32, but was only 1.6G of a 16G card!

        • NOTE2: Even if SD card is set for the FAT32 partition to be the full size, the Pi will RESIZE it....

      • IMPORTANT >>>> Copy the NOOBS zip file to the microSD card and UNZIP / extract it TO THE CARD. DO NOT TRY to just copy the extracted files from the laptop!!

      • NOTE: UNZIP the files to the TOP LEVEL of the microSD card - zip utility will try to unzip to a folder by default.

    • NOOBS VERSION: Current Version is: 1.5.0, updated 11-21-2015

        • Update 12/3/2016 - CURRENT Version is 2.1.0

      • NOTE: New version will come up in desktop.

      • Go to the MENU (upper left of taskbar), then PREFERENCES => Raspberry Pi Configuration

        • Set Hostname on main menu

        • Keep "Set boot to desktop" selected.

        • Click Localisation

          • Set Language (English), Country (US), Don't touch Character set.

        • Click "Set Timezone", select Area -> America, select Location -> Chicago

        • Leave Keyboard alone.

        • Select "Interfaces"

          • My guess here is that it is best to enable SPI and SSH.

            • Definitely enable SSH - this is required for VNC.

            • SPI and i2c can both be disabled unless desired.

          • DISABLE the "Serial" option - this disables dumping the console output to the serial port that would be used for serial communication with other devices. If bluetooth is used, this wouldn't be important.

    • POWER: Use the mini-usb connector to apply (just) power. (This is not a usable USB port - it's just for power.)

    • Use Raspi-config (on initial power-up from NOOBS) to set the time-zone, keyboard preferences, etc.

      • Set the configuration to always start in desktop mode.

      • Timezone should be set to: 'America/Chicago'

      • May need to set the keyboard options.

    • With the wifi dongle plugged into a USB port, from the desktop, goto the menu (may be under "Preferences" and find the WiFi Configuration tool. Enter the tool, find the "scan" option to locate available networks. Enter the WPA password. Once the wireless network is set up, it will auto-enable on powerup each time.

    • The Raspberry Pi seems to come up expecting a BASH shell environment.

      • Edit .bashrc to include the following code for sourcing the .aliases file:

if [ -f ~/.aliases ]; then

source ~/.aliases

fi

      • Add a .aliases file with the following code:

# .aliases file

alias em='/usr/bin/emacs -nw '

    • Set up the VNC - run the following commands

sudo apt-get update

sudo apt-get install tightvncserver

The vncserver can be started with the command:

$ vncserver :1

Run the vncserver command manually the first time to set the password.

    • Run "hostname -I" to get the local IP address for the Raspi on the local wifi server.

    • Set up the VNC to start automatically on power-up:

$ cd /home/pi/.config

$ mkdir autostart

$ cd autostart

$ nano tightvnc.desktop (or use emacs...)

In that file, add the following text:

[Desktop Entry]

Type=Application

Name=TightVNC

Exec=vncserver :1

StartupNotify=false

After re-booting the Raspi, download and use VNC-Viewer on a PC to log into the Raspi using the IP address from 'hostname -I', and the password that was created when vncserver was manually run above.

Reboot

In order to enable the changes you have made, you will need to reboot the Raspberry Pi

sudo shutdown -r now

Editors:

There are options for editors on the Raspberry Pi.

Editors that come with the default package:

vi

nano Help at this url: http://www.raspberrypi-spy.co.uk/2013/11/quick-guide-to-nano-text-editor-on-the-raspberry-pi/

Also, help at: http://mintaka.sdsu.edu/reu/nano.html Good short reference.

You can also install the Emacs editor with:

sudo apt-get install emacs

Note - run emacs "-nw" to get it to run in the existing terminal.

Running in another display will complain if using VNC to a non-native display (need to set $DISPLAY?)

Will also have to go through startup emacs menu to set .emacs file.

Set it so that it does not default to the initial screen.

Setting the TimeZone

Use "tzselect" to walk through figuring out what the code is to set the timezone.

When it finishes, it will give you the line to add to your .profile config.

Appended the following line to the ~pi/.profile file:

TZ='America/Chicago'; export TZ

Languages:

Perl and Python are available with the base install.

Power

I guess I got distracted and was thinking I needed to power the board through the standard USB-A connectors. That works, but if you do it, and you're using a powered hub, then you need to realize this:

The powered hubs tend to have a "primary" port that comes from the host computer. This port MUST be cabled to the Raspberry Pi in order for the devices on the hub to communicate. IMPORTANT: That port does NOT include a power connection - the "powered" part of the hub only applies to the other usb ports on the hub. So... You have to plug the primary cable from the hub into the Pi, and you ALSO have to include a second cable to provide power.

The BEST way to provide power is to use the mini-USB connector to just provide power. Otherwise, it will work to use a SECOND cable to go from one of the (powered, secondary) usb ports on the hub to your second usb port on the PI - but this takes up hub and PI ports.

Initial Bringup:

Note: I initially had keyboard mapping issues. Had to use raspi-config to set up the keyboard to work correctly.

Set up SSH to be enabled on power-up

Done - followed instructions in book(s), no issues.

Set up wifi

On the OLD Raspbian, the wifi stuff was under "Preferences" in the menu.

IN VERSION 1.5, WIFI is under the icon on the far right side of the desktop taskbar. Click, select which Wifi network, then enter the password.

See above instructions for starting VNC, and enabling VNC on startup.

Startup Scripts:

Scripts to be run at power-on can be added to the /etc/rc.local file.

I have written a script called "machine_details" that runs in background on startup. It records the machine name, user, datestamp, and IP address, then sends this to the DanRoberts email account. This tells me 2 things: 1) it will let me know if there was a power glitch causing the machine to re-boot, and 2) it will let me know the IP address - which is helpful, since the Pi will typically be running headless, and is currently letting DHCP pick the IP address.

NOTE: There is also a "@reboot" option in the cron tab, and that works on the Pi version.

Note - can get SSID from "iwconfig" command.

Test Flashdrive

IMPORTANT NOTE: There are several different formats for flashdrives. The "best" format to use so that data can be passed between Pi and Windows machines is the FAT32 format. Before using a flashdrive:

    • Access the flashdrive from Windows Explorer.

    • Right click on the flashdrive, and select "format"

    • Format the flash drive for FAT32.

Done. Note - The Raspberry Pi User Guide set up the flashdrive with the name "externaldrive". However, the mount command complained about the length of the name. It seems to be working, but a shorter name might be a good thing.

Currently, the flash drive is mounted at: /media/externaldrive

Steps to connect a flashdrive (from Pi User Guide):

  • Connect the flashdrive to the USB hub.

  • Type: sudo fdisk -l << gets the listing of devices connected. Will list the storage devices by size.

  • The device name will be something like: /dev/sdXN, where X is the drive letter and N is the partition number. The first device should be /dev/sda1

  • Create a mount by typing: sudo mkdir /media/extdrive

  • Make the drive accessible with the commands:

    • sudo chgrp - R users /media/extdrive && sudo chmod -R g+w /media/extdrive

  • Mount the drive: sudo mount /dev/sdXN /media/extdrive -o -rw

Test GPIO

GPIO_Ports

Test power from GPIO -

Tested, and verified that the RaspPi will run fine when powered from the GPIO port.

PiSerialPort

RaspiArduino - Interface protocol for Raspi to Arduino

Set up ADC

Crontab

Set up a simple crontab to run the raspi2ard script collecting analog data from the Arduino and logging it to the flash drive.

Edited the crontab file at ~pi/CRONTAB_pi

Working - no problems.

Note: Need to set the email in the crontab file with:

MAILTO=''

or

MAILTO='joe.user@gmail.com'

If not set, the cronjob will attempt to email the results of any cron jobs to pi@raspberrypi, which will obviously fail.

The mailto value can be set to a valid email address, and results will be forwarded correctly.

The mailto value can be set to an empty field to avoid email.

NOTE2: After the mailto, you may also want to include:

TZ='America/Chicago'

That sets the time zone for the crontab so that the "date" function will report the time as Central, instead of UTC

NOTE3: The *cron* tab timing will be based on UTC - NOT whatever timezone you set in the cron for local time. Curious - but seems to be the way it is. Won't matter for hourly or daily, but will matter any time a specific HOUR is desired. Subtract 6 hours from local time to get UTC.

NOTE4: Can also use "@reboot" instead of the time spec to have crons launched at each reboot. This can be done instead adding scripts to /etc/rc.local

SERVING WEB PAGES

Following the examples listed in at least two of the books, I stared with "FLASK" for serving web pages. It installs and works easily on the Pi.

sudo apt-get install python-pip

sudo pip install flask

NOTE: Python FLASK routines have to be run as ROOT.

To call the hello-flask.py script, use: sudo python2 hello-flask.py

FLASK

Web pages created by Flask (or other software packages are visible on your local network. To be seen on the OUTSIDE internet, you will have to "port forward" or otherwise expose the web page port to the outside world.

Information about using the ngrok service for port forwarding. This allows the Raspberry Pi connected to a hot spot to be visible from the rest of the internet.

Histograms