Slide #1.

Sensing the World with Raspberry Pi J Dean Brock and Rebecca Bruce CCSC:SE 2014 Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #2.

They’re back CCSC:SE 2013  Changing the World with Raspberry Pi • GPIO with the Raspberry Pi • Illustrating numeric algorithms using Python This year  Use of sensors to collect “environmental” information  Display of sensor data on the “web” Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #3.

Raspberry Pi in our courses Relevant course for today  CSCI/EGM Mechatronics Design (Fall 2013)  CSCI Operating Systems (Spring 2014)  CSCI/EGM Robotics Project (Fall 2014) Other course applications  CSCI Database Management Systems (Spring 2014) • Students set up database and web servers  CSCI Computer Organization (Fall 2014) Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #4.

Workshop Materials All workshop materials will be available on our own (256 Mbyte) RPi webserver: pinkie-pie.cs.unca.edu Mirrored on the UNCA CS webserver: www.cs.unca.edu/pinkie-pie Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #5.

Time to (try to) connect with VNC Server Step 1: On your PC, install the tightvnc viewer  From the Internet (preferred): http://www.tightvnc.com/download.php (download the Java viewer as a JAR file)  It’s also on the SD card, in case there is no internet Step 2: power up the RPi; the VNC server starts at boot; Power-up procedure:  Insert SD card  Connect cobbler ribbon cable  Connect power adapter Step 3: Get Pi’s IP as described on the slide Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #6.

What we are trying today Pi’s are configured to connect to our web server  Makes a query containing • “local” (possibly NATed) IP address • Ethernet address • Helps us to configure DHCP A CGI script stores this information a file  Remote IP is also stored File can be accessed from a browser This won’t be needed if we have monitors! Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #7.

Complete VNC connection Step 4: On PC, start viewer with IP for your RPi and port number 5901 RPi’s IP number 5901 Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #8.

Other Connection Options Direct connection to a monitor, key board, mouse (We would be looking at this slide if that is an option) Secure shell (SSH) connection  On Windows, you need MobaXterm or PuTTY to make an SSH connection; it also on the SD card Serial connection using FTDI cable  On Windows, you need MobaXterm or PuTTY for this connection; it also on the SD card Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #9.

A quick introduction Because you might have seen this at Furman last year Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #10.

Rapberry Pi Model B Picture from Wikimedia Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #11.

Raspberry Pi history A computer to inspire children  to put the fun back into learning computing  provide computers to the poor  guided by Raspberry Pi foundation Early support from academia and industry  University of Cambridge Computer Labo ratory  Broadcom Raspberry Pi model B launched in early Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #12.

Raspberry Pi Quick start From Raspberry Pi Quick start guide Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #13.

Raspberry Pi hardware Broadcom BCM2835 SoC (system on chip)  700 MHz ARM1176JZF-S CPU • ARM11 microarchitecture with ARMv6A ISA  Video Core IV GPU (1080p, 24GFLOPS)  Peripherals: UART, USB, I2C, GPIO LAN9512 USB hub and Ethernet controller 512 Mbytes RAM Connectors: USB, Ethernet, HDMI, Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #14.

Very detailed references Schematics for Rasberry Pi board BCM2835 ARM peripherals data sheet LAN9512 data sheet ARMv6 architecture reference manual  Available with ARM login id • Or search using google  Almost 400 pages Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #15.

How we name our Pi’s We give the Pi’s name and label them We put this information in our servers  DNS – Domain Name System • Name-to-IP and IP-to-Name  DHCP – Dynamic Host Configuration Protocol • Ethernet-to-IP Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #16.

Figuring out your IP address When at home, connect to your router  And try to find the Pi Elsewhere  Connect with a monitor • Look at the display • IP number will be at the end  Connect with serial connection from your PC • Run ip addr Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #17.

What we are trying today Pi’s are configured to connect to our web server  Makes a query containing • “local” (possibly NATed) IP address • Ethernet address • Helps us to configure DHCP A CGI script stores this information a file  Remote IP is also stored File can be accessed from a browser Obviously this needs a lot of work  Let’s hope we have monitors! Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #18.

Pi room last week Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #19.

Raspberry Pi GPIO A subset of the BCM2835 GPIO pins Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #20.

RPi General Purpose IO (GPIO) Pins • • • • • • 17 GPIO pins on the P1 header • most have alternated functions • two pins for UART; two for I2C; six for SPI All 17 pins can be GPIO (i.e., INPUT or OUTPUT) • all support interrupts • internal pull-ups & pull-downs for each pin • I2C pins have onboard pull-ups using forlike GPIO Pins •are 3.3Vthem not 5V onmay the not work Arduino They are connected directly to the Broadcom chip Sending 5V to a pin may kill the Pi Maximum permitted current draw from a 3.3V pin is 50mA Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #21.

The Bigger Picture Diagram includes BCM GPIO references (GPIO.BCM), common functions, WiringPi pin references, and Pin numbers (GPIO.BOARD) Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #22.

Using the GPIO Pins There are two methods to read or write these pins  File-type access in userspace • accessed through the device (/dev) interface  Write/read memory addresses allocated to the GPIO peripheral of the SoC • Memory locations can be found in the  datasheet for the BCM2835  Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #23.

Flash LEDs: Physical Connection Connect an LED to GPIO 17 (P1-11) The LED will initially be off because the GPIO pins are initialized as inputs at power-on (except for TXD). Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #24.

Flash LEDs: Software Solution 1 Using a file-type access  Run shell script ledBash.sh • cd ccscse2014/examples • sudo ./ledBash.sh #!/bin/sh echo 17 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio17/direction echo 1 > /sys/class/gpio/gpio17/value sleep 1 echo 0 > /sys/class/gpio/gpio17/value echo 17 > /sys/class/gpio/unexport Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #25.

Flash LEDs: Other Software Solutions  Solution 2: Write/read memory addresses allocated to the GPIO peripheral of the SoC using pointers  Memory locations can be found in the  datasheet for the BCM2835   RPi.GPIO • Comes with Raspbian  RPIO • Supports PWM & servos  WiringPi-python  Quick2Wire  The best way to access the GPIOs?  A Python program using RPi.GPIO to flash the LEDs:  Run Python script ledPython.py • cd ccscse2014/examples • sudo ./ledPython.py Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #26.

I2C – Inter-Integrated Circuit Invented by Phillips in 1982  I2C specification A tradeoff between speed and area  Less spaces devoted to wires  Fewer wires can decrease throughput A good idea for sensors  But not for disk drives Adopted by Intel for personal computers  Under the name SMBus  00:1f.3 SMBus: Intel Corporation 82801JD/DO (ICH10 Family) SMBus Controller (rev 02) Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #27.

What about SPI? Needs one line per device  O(n) vs O(1) wires Sensors aren’t packaged in DIPs  You must use breakout boards Adafruit  Tutorials  Libraries  Many useful choices • Search for “adafruit i2c images”  Limor Fried likes them Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #28.

IC 2 Understanding I2C  The physical I2C bus  Masters and Slaves  The physical protocol  I2C device addressing  The software protocol  I2C support in Linux kernel • And Windows and Arduino • And Microcontrollers and … Image credit: http://quick2wire.com/articles/i2c-and-spi/ A good Tutorial at Robot Electronics Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #29.

Adding physical I C devices 2 Adafruit breakout boards  BMP180  9-DOF: L3GD20 and LSM303 There’s just four connections on each  GND  3Vo (to 3V3 of T-Cobbler)  SDA  SCL Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #30.

It might look like this Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #31.

Masters and Slaves The devices are either masters or slaves Master device drives the clock Master device initiates the transfers Master device controls the transfer Typically only one master.  But multi-master mode is possible Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #32.

Protocol Wikimedia commons image • Start and stop sequences mark the beginning and end of a transaction • Transfer is initiated with SDA is pulled low while SCL (clock line) is high • During data transfer, SDA must not change while SCL is high • Data is transferred in sequences of 8 bits, followed by an acknowledge bit • Bits are sent with the MSB (Most Significant Bit) first. • The SCL line is pulsed high, then low for each bit • The standard clock (SCL) speed for I2C is up to 100KHz Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #33.

I C Device Addressing 2 Image credit: http://www.robot-electronics.co.uk/acatalog/I2C_Tutorial.html I2C addresses are 7 bits or sometimes 10 bits  Up to 128 devices on the usual I2C bus Devices has fixed addresses  But SMBus supports “address resolution” Addresses are still sent in 8 bits  Last bit is zero if writing  Last bit is one if reading Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #34.

I C Write Protocol 2 Send start sequence Send address of slave address with last bit 0 Send one byte register number or “command”  It’s a bit more complicated with an EEPROM Send the data byte If appropriate send more data  Register number will be incremented Send stop sequence  No length field! Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #35.

A great picture from http://mbed.org/users/okano/notebook/i2c-access-examples/ Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #36.

I C Read Protocol 2 Send start sequence Send address of slave address with last bit 0 Send one byte register number or “command” Send start sequence Send address of slave address with last bit 1 Read data  Use multiple ACK followed by STOP for multi-byte read A read is really a write followed by a read Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #37.

Another great picture from http://mbed.org/users/okano/notebook/i2c-access-examples/ Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #38.

Getting the Pi ready Stop the “blacklisting” of I2C  In /etc/modprobe.d/raspi-blacklist.conf • Comment out the blacklist of i2c-bcm2708 Add the appropriate modules  By adding the following lines to /etc/modules i2c-bcm2708 i2c-dev Install a couple of packages i2c-tools libi2c-dev Add user pi to group i2c Reboot Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #39.

Linux i2c device interface Device files are /dev/i2c-*  Major number is 89  Minor number is bus number Linux device documentation for “userspace”  The usual read and write are supported  but ioctl is the preferred interface  especially with smbus protocol macros Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #40.

The i2c utilities Before programming, try the i2c utilities  i2cdetect  i2cdump  i2cget  i2cset On Rev1 versions of the Raspberry Pi, the i2c devices on a bus 0 Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #41.

Using the I2C utilities finding the bus ls -l /dev/i2c-* lsmod | grep i2c id i2cdetect –l i2cdetect -y 1 Use 0 with Rev1 Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #42.

Using the I2C utilities finding the temperature i2cget -y 1 0x77 0xD0 b  Make sure you have a BMP180 i2cdump -r 0xAA-0xBF -y 1 0x77  Examine mysterious EEPROM registers i2cdump -y -r 0xF4-0xF7 1 0x77  Examine measurement register i2cset -y 1 0x77 0xF4 0x2E  Tell device to read temperature i2cdump -y -r 0xF4-0xF7 1 0x77 Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #43.

Programming examples Read the temperature on BMP180  C program using device interface  Python program using SMBus module • Which is really a Pythonized-C interface  Python program using AdaFruit library And use i2c utilities from the shell  Which really isn’t programming Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #44.

Trying out the device interface Take a look at the bmp180 program  Which should be in the examples directory Note the usual structures  Lots of register definitions  Routines to interpret the data  Actions • Open the device • Read and write to the device Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #45.

But what about the hard part? Where does this come from x1 = (ut-ac6)*ac5/32768 ; x2 = mc * 2048 / (x1 + md) ; b5 = x1 + x2 ; t = (b5 + 8) / 16 ; the BMP180 datasheet  See page 15 for the details • and don’t expect to understand why Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #46.

Secrets of the I2C sensor Read the datasheet Find an application note Find example programs Search for device drivers for the manufacturer  It should contain a lot of DEFINE Try I2Cdevlib Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #47.

Time to test it out Compile the programs Test it out  It should get warmer when touched  And colder when using an ice cube The program has no filtering  So sometimes odd results are returned Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #48.

Why the JSON? Pretty to easy to read Has a formal schema for validation Works well with web applications  JavaScript  HTML5 Will please the New Media students in CSCI 185 Supported in all common programming languages http://pinkie-pie.cs.unca.edu is running on a Raspberry Pi Model B Rev1 -- 256 MBtyes of RAM  Once it could tell you how hot it is over the web Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #49.

What about those interrupts? Some I2C sensors do have interrupt lines To “handle” an interrupt  Connect the interrupt line to a GPIO pin  Write to /sys/class/gpio/N/edge • none, rising, falling, both  Use poll on /sys/class/gpio/N/value • Waits for I/O activity with optional time-out A hard system call to use  But there are many examples because of its frequent use in networking applications Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #50.

WebIOPi: Install Install WebIOPi on your Raspberry Pi  Download the tar archive file from Sourceforge: • Already done for you---change directories to ccscse2014 & look for WebIOPi-0.7.0.tar.gz  Uncompress: • tar xvfz WebIOPi-0.7.0.tar.gz  Change directory to new WebIOPi folder: • cd WebIOPi-0.7.0  Run setup shell script: • sudo ./setup.sh Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #51.

WebIOPi: Default Page Try it:  On the Pi, start the web server: sudo service webiopi start  On the Pi, open the WebIOPi default page in Midori: • use the URL: localhost:8000  On your PC, open the WebIOPi default page in a browser: • On your Pi, use the ifconfig command to determine your pi's IP • For example, it might be: 192.168.0.146 • On you PC, open Chrome and open the URL: IP:8000 • For example the URL might be: 192.168.0.146:8000 Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #52.

WebIOPi: What's happening All of the source code is installed on your Pi in the WebIOPi-0.7.0 directory but it's easier to view on the Source tab of the WebIOPi site WebIOPi is a REST API containing many things: a HTTP server (written in python), a library of javascript library, python library... The default WebIOPi display is created using:  the index.html file in the htdocs directory  The GPIO configuration as defined by the files in the python directory Both the GPIO configuration and the browser display can be customized Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #53.

WebIOPi: Configuration A configuration file is used to setup the WebIOPi server  The syntax is the same as windows INI files • Several sections containing key=value pairs  The webiopi service use /etc/webiopi/config as a configuration file • That can be changed Extend the webiopi server using a Python script  Edit the [SCRIPTS] section of the config file to designate location of the python script Customize the browser display using Javascript  Edit the [HTTP] section of the config file to designate location of the index.html file Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #54.

WebIOPi: Customizing I Example 1: the script and index file in ccscse2014/WebIOIPi-0.7.0/examples/LED  Make the following changes to /etc/webiopi/config: sudo emacs (or nano) /etc/webiopi/config  In the [HTTP] section, change doc-root to be:  doc-root=/home/pi/ccsce2014/WebIOPi0.7.0/examples/LED  In the [SCRIPTS] section, change myscript to be:  myscript =/home/pi/ccsce2014/WebIOPi0.7.0/examples/LED/script.py  In the [DEVICES] section, uncomment pwm0 = PCA9685 Restart the server: Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this


Slide #55.

WebIOPi: Customizing II Example 2: the script and index file in ccscse2014/WebIOIPi-0.7.0/examples/BMP  Make the following changes to /etc/webiopi/config: sudo emacs (or nano) /etc/webiopi/config  In the [HTTP] section, change doc-root to be:  doc-root=/home/pi/ccsce2014/WebIOPi0.7.0/examples/BMP  In the [SCRIPTS] section, change myscript to be:  myscript =/home/pi/ccsce2014/WebIOPi0.7.0/examples/BMP/script.py  In the [DEVICES] section, uncomment the BMP device Restart the server: sudo service webiopi restart Dean Brock, Rebecca Bruce, and Marietta Cameron COMPUTER SCIENCE
More slides like this