Raspberry Pi

MakeNTU workshop

What is Raspberry Pi?

raspberry pi logoreal rpi photo

v.s. Arduino?

R PiArduino
multiple taskssimple task
digital GPIO onlydigital & analog

Raspberry Pi

R Pi 3R Pi 2 B+
quad-core 1.2GHz CPUquad-core 900MHz CPU
1GB RAM1GB RAM
WifiNo
BLENo bluetooth

Operating System

RPi 一般使用基於 Linux kernel 的作業系統

  • Raspbian (based on debian)
  • Pidora (Fedora)
  • Arch linux ARM
  • RISC OS
  • ...

For more Information...

Go to RPi downloads page!

Enter the RPi World

What you will need

  • a RPi
  • a computer with an ethernet slot
  • ethernet cable
  • Android phone for USB tethering (optional)

Step by Step (Windows)

  • download pietty
  • setup internet sharing

Step by Step (Mac OSX)

  • (ssh is bulit-in)
  • setup internet sharing:
    System Preferences > Sharing > Internet Sharing

Step by Step (Linux)

Log in to RPi

  • pietty: use pi for "Log in as:"
  • ssh:
    ssh pi@192.168.137.10
  • password is raspberry

A Brief Guide to Linux

Directory Structure

fhs

Command Line

  • lscd
  • touchrmmvcpmkdirrmdir
  • catlessheadtail
  • man

Package Manager

  • sudo
  • apt-get install
check the usage usingman

Pipeline

串接前一個 stdout 到下一個 stdin

cat /etc/passwd | awk -F: '{print $1}' | less

Editor

  • vim
  • emacs
  • nano

vimrc

syntax on
filetype indent plugin on
colorscheme torte
set nu ai ar
set ts=4 sw=4 
set mouse=a
set bs=indent,eol,start ww+=<,>,[,]
Or you can use a configuration created by others.

I/O Ports on RPi

rpi circuit

GPIO

gpio ports

Limitation

It's not easy to do real-time control with RPi GPIO!!

Programming with GPIO

Languages

  • C/C++
  • Python
  • node.js
  • ... and more

Python

python

RPi.GPIO

import RPi.GPIO as GPIO
# GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BCM)

Input/Output

GPIO.setup(chan_in, GPIO.IN)
# this will return GPIO.LOW (0) or GPIO.HIGH (1)
GPIO.input(chan_in)
GPIO.setup(chan_out, GPIO.OUT)
GPIO.setup(chan_out, GPIO.OUT, initial=GPIO.HIGH)
# this will set the output state (GPIO.LOW or GPIO.HIGH)
GPIO.output(chan_out, state)
# chan_list is a list of channel number
GPIO.output(out_list, state)
# len(chan_list) == len(state_list)
GPIO.output(out_list, state_list)

Cleanup

GPIO.cleanup()

Docs

https://sourceforge.net/p/raspberry-gpio-python/wiki/Home/

Applications of RPi

Imagine It!

  • Sensor
  • Monitor
  • Controller
  • Server
  • Player
  • ...

Questions?

Project

Goal

Integrate an RFID sensor with an R Pi. The sensor can scan any easy card and do something cool.

yoyocard

Learning

  • Python
  • Networking
  • Event loop concept
  • Reading docs
  • Google-Fu

Structure

  • Use python to read card information
  • Use nodejs or python to serve websocket server
  • Use web app to get the information and do something cool

Hardware

材料

  • RPi * 1
  • RFID 模組
  • 杜邦線(母母) * 7

SPI

spi gpio pins

SPI

spi protocol

RFID

  • Each card has a UID (but may not be unique)
  • Small storage in the card
  • Needs a key to read/write
rfid data

Software

Dependencies

  • python (built-in)
  • SPI-Py
  • pi-rc522
  • websocket

Setup

git clone https://github.com/lthiery/SPI-Py
git clone https://github.com/NTUgEEk/pi-rc522
sudo pip3 install ./SPI-Py
sudo pip3 install ./pi-rc522

How to use

docs

Nodejs websocket

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt install nodejs

npm install --save websocket
Or, you can use python for the websocket server:
sudo pip3 install websockets

Websocket

Flow

  • Read card UID using python
  • Broadcast UID using websocket server
    format: 255.255.255.255 (string)

Event Loop Concept

import asyncio

@asyncio.coroutine
def compute(x, y):
    print("Compute %s + %s ..." % (x, y))
    yield from asyncio.sleep(1.0)
    return x + y

@asyncio.coroutine
def print_sum(x, y):
    result = yield from compute(x, y)
    print("%s + %s = %s" % (x, y, result))

loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

Hints

Nodejs

const spawn = require('child_process').spawn;