[Mooc]IoT Course 4 Interfacing with the Raspberry Pi
Week 1
Reading Material
Lesson 1
Lecture 1.1 Network
Connecting to a Network
- Wired connection using the built-in RJ45 connector
- Wi-Fi connection using a USB Wi-Fi adapter
Wi-Fi Config
- start Wifi config from the desktop
- Allows you to select a network and give a password
- Similar to Mac OSX or Windows interface
- Click Scan to find networks
- Select the network you want
- DHCP is needed to get an IP address
Using a Web Browser
- Epiphany browser is built-in
- Icon on the desktop
Firewall Warning
- Your network may have a firewall
- To use a networked application, firewall must be set to allow that application's traffic
- Web traffic may be allowed, but not World of Warcraft
- If you run a networked application and it doesn't work, check the firewall settings
- Or ask the network administrator
Lecture 1.2 Secure Shell
Secure Shell
- Secure shell(ssh) allows you to access a machine remotely
- Two machines involved
- ssh server - the machine you with to access remotely
- ssh client - the machine which you use to access the server
- Commands you type into the client are executed by the server
- Text printed by the server appears on the client screen
- Basic ssh provides only a terminal interface
- Not normally GUI
SSH Requirements
- Both machines must be on the Internet
- Must have an ssh client program on the client machine
- Default on Linux, Mac OSX
- The machine being controlled must be running an ssh server
- Default on Raspberry Pi
- ssh server must be started on other machines
- ssh server must be installed on Windows machines
- Must have an account on the server machine
- Must provide username/password
- Any firewall must allow ssh access
- Firewalls for client and server
Lecture 1.3 SSH Client/Server
Running the SSH Client
- Open a terminal(LXTerminal) and type the following command:
ssh
@ is your username on the server machine is the address of the server machine - For example, uci.edu
- First time, you will get a message that the address could not be authenticated
- Enter "yes" to continue, as long as you're sure of the address
- Enter password
- You will see the server's command-line prompt
SSH Client Screen Shot
Raspberry Pi SSH Server
- This is useful to access your Raspberry Pi without connecting it to a monitor/keyboard
- ssh server is running by default on your Raspberry Pi
- Should change the private host ID keys
- Otherwise, they have the same default values
sudo rm /etc/ssh/ssh_host_* && sudo dpkg-reconfigure openssh-server
Lesson 2
Lecture 2.1 SSH Server
Getting Your IP Address
- Your Raspberry Pi does not have a domain name to serve as an address for client
- Need to get your IP address instead
ifconfig
Accessing Raspberry Pi w/ SSH
- Using a Linux or Mac OSX machine with ssh pre-installed
- Using a terminal window
- Using the default "pi" account
Lecture 2.2 Network Programs
Programmatic Networking
- Accessing a network manually is easy
- We want to access to network via a program
- Enable interaction between the network and he world
- What IoT is about
Internet Structure
A hierarchy of local area networks(LANs) connected by routers
- Also switches and bridges
Internet Protocols
- Need to send data between incompatible networks
- A protocol is a set of rules defining
- how data should be transferred
- what data is contained in each packet
- Every machine in the network must implement a common set of protocols to communicate on the Internet
- Internet protocols do not interfere with LAN protocols
Lecture 2.3 Internet Protocols
Key Features of Internet Protocols
- Unique naming
- Defines a format for host addresses
- Each node gets one unique host address
- Message structure
- Defines a standard transfer unit, a packet
- Packet consists of header and payload
- Header: protocol information(i.e. destination address)
- Payload: contains data to be transmitted
Internet Protocol Family
- IP (Internet protocol)
- Host naming scheme
- Host-to-host connection, unreliable
- UDP (Unreliable Datagram Protocol)
- Process-to-process communication
- Process naming
- Also unreliable
- TCP (Transmission Control Protocol)
- Process-to-process communication and naming
- Process naming
- Reliable communication
TCP vs. UDP
- TCP and UDP are the transport layer protocols
- TCP is connection-oriented
- Reliable, will resend a lost or corrupted packet
- Packet sequencing supported
- Flow control, error detection/correction
- UDP is connectionless
- Unreliable, does not guarantee packet arrival
- Simpler, faster, smaller than TCP
Lesson 3
Lecture 3.1 IP Addresses
IP Addresses
- TCP/IP addresses a machine using an IP address
- A unique number for all machines on the internet
- IP version 4(IPv4) uses 32-bit addresses
- Four numbers (8-bits each) separated by periods -- 192.0.0.0
- Network Address: High bytes of IP address common across an entire LAN/WAN
- Host Address: Low bytes of IP address unique for each machine in a network
IPv6
- IPv4 uses 126-bit addresses compared to 32-bits with IPv4
- IPv4 only supports 2^32 addresses
- Internet will exceed this number
- IPv6 integrates security
- IPSec used to encrypt data in transit
- IPv6 incorporates several changes to the header
- Some fields become optional
- IPv6 is not widely adopted yet
Ports
- TCP and UDP use ports to address specific applications on a machine
- Port numbers are assigned to each networked application layer protocol
- Ex. HTTP uses port 80
- Web browser sends page requests on port 80
- Web server listens to port 80 for requests
- Ex. HTTP uses port 80
- Port number is 16-bit value in TCP and UDP headers
Lecture 3.2 Domain Names
Host Names & Domain Names
- Each host on the Internet has a unique IP address
- 128.2.203.179
- IP addresses are not easy for humans to memorize, so we use domain names
- ics.uci.edu, cnn.com
- Each domain name must be resolved to an IP address to send packets
Domain Naming System (DNS)
- DNS is a hierarchical naming system used to determine IP addresses from domain names
- Gigantic, distributed tables are accessed by performing a DNS Lookup
- IP addresses are stored in a local cache to save time
nslookup
Program that performs a DNS lookup and returns the IP address
Lecture 3.3 Client/Server
Client-Server Model
- Networked applications commonly use the client-server model
- Server manages a resource
- Server accepts requests, sends responses
Internet Connections
- Clients and servers communicate over connections
- A socket is an endpoint of a connection
- Client and server both have sockets
- A port is a 16-bit integer that identifies a process:
- TCP and UDP use ports to refer to processes
- Some ports are well-known and associated with an application(e.g., port 80 is associated with Web servers)
Ports and Servers
- Multiple clients and servers can exist on the same machine
- Server processes listen to their assigned ports
- Client processes send requests on their assigned ports
Week 2
Reading material
Lesson 1
Lecture 1.1 Sockets
Sockets Interface
- Programming interface to perform network communication
- Can be used in many languages
- We will use Python
- Based on client/server programming model
Sockets on the Client
Creating a generic network client:
- Create a socket
- Connect socket to server
- Send some data (a request)
- Receive some data (a response)
- Close the socket
Creating a Socket
import socket
mysock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- Need to import the
socket
package -
socket.socket()
creates the socket -
AF_INET
declares Address Family Internet -
SOCK_STREAM
indicates TCP (connection-based)
Lecture 1.2 Sending Data
Connect Socket to Server
host = socket.gethostbyname("www.google.com")
mysock.connect(host, 80)
- Need a host to connect to
- Host is an IP address, but you may only have the domain
-
gethostbyname()
performs DNS lookup -
connect()
creates the connection - Port is second argument, 80 is web traffic
Sending Data on a Socket
message = "GET / HHTTP/1.1\r\n\r\n"
mysock.sendall(message)
- Message string is an HTTP GET request
- could send any data
-
sendall()
send the data- Tries until it succeeds
Receiving Data on a Socket
data = mysock.recv(1000)
-
recv()
returns data on the socket- Blocking wait, by default
- Argument is the maximum number of bytes to receive
Closing a Socket
mysock.close()
- Close connection, free up port
Lecture 1.3 Exceptions
Errors During Execution
- Errors may happen when working with sockets
- Socket cannot be opened
- Data cannot be sent
- Errors occurring during execution are called Exceptions
- There are many kinds for each function
ZeroDivisionError
TypeError
Handling Exceptions
- When an error happens, you might want your code to do something appropriate
- Use try and except statements
- Place code with possible error inside the try clause
- Code in the except clause is executed when an exception occurs
try:
z = x / y
except ZeroDivisionError:
print("Divide by zero")
Socket Exceptions
- socket.error
- All socket errors
- gaierror
- getaddressinfo() error - no host found
- We will handle some of these to make the networking more robust to errors
Client Program
import socket
import sys
try:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create socket")
sys.exit()
try:
host = socket.gethostbyname("www.google.com")
except socket.gaierror:
print("Failed to get host")
sys.exit()
mysock.connect(host, 80)
message = "GET / HTTP/1.1\r\n\r\n"
try:
mysock.sendall(message)
except socket.error:
print("Failed to send")
sys.exit()
data = mysock.recv(1000)
print(data)
mysock.close()
Lesson 2
Lecture 2.1 Server Code
Sockets on the Server
Server needs to wait for requests
- Create a socket
- Bind the socket to an IP address and port
- Listen for a connection
- Accept the connection
- Receive the request
- Send the response
Creating and Binding a Socket
mysock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysock.bind("",1234)
-
bind()
binds the socket to a port - First argument is the host, it is empty
- Can receive from any host
- Port number is arbitrary and not well-known
Listening and Accepting a Connection
mysock.listen(5)
conn, addr = mysock.accept()
-
listen()
starts listening for aconnect()
- Argument is backlog, number of requests allowed to wait for service
-
accept()
accepts a connection request- Returns a connection (for sending/receiving) and an address (IP, port)
Lecture 2.2 Live Server
Sending and Receiving
data = conn.recv(1000)
conn.sendall(data)
conn.close()
mysock.close()
-
recv()
andsendall()
used to send and receive data - Connection and socket closed separately
A Live Server
while True:
conn, addr = mysock.accept()
data = conn.recv(1000)
if not data:
break
conn.sendall(data)
conn.close()
mysock.close()
- An infinite loop is typical
- Connection closed when no data is received
Full Server Program
import socket
import sys
mysock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
mysock.bind("",1234)
except socket.error:
print("Failed to bind")
sys.exit()
mysock.listen()
while True:
conn, addr = socket.accept()
data = conn.recv(1000)
if not data:
break
conn.sendall(data)
conn.close()
mysock.close()
Lecture 2.3 Internet Control
Control via the Internet
- Controlling a device over the Internet is interesting
- Home automation
- Tele-medicine
- Send commands to a Raspberry Pi
- RPi does something in the real world
- Sensor data can be sent remotely also
Internet Controlled LED
- LED turns off and on by remote commands
- Send "on" message to turn LED on
- Send "off" message to turn LED off
- Modify server program to control LED
Modified Server Program
while True
conn, addr = mysock.accept()
data = conn.recv(1000)
if not data:
break
if data == b'on':
GPIO.output(13,True)
if data == n'off':
GPIO.output(13,False)
- Receive data is compared to commands
- Data is a byte array, not a string
Lesson 3
Lecture 3.1 Python Client Demo
Lecture 3.2 Python Server Demo
Week 3
Reading Material 1
Reading Material 2
Lesson 1
Lecture 1.1 Network Libraries
Networking Libraries
- The Socket library is very low level
- Programmer composes the message content
- Must know details of the protocol
Protocol-Specific Libraries
- Other libraries are available for specific protocols
- Programmer does not need to know protocol details
- Library functions are provided to handle details
import http.client
conn = httplib.HTTPConnection("www.google.com")
conn.request("GET","/")
Interaction with Online Services
- Many services exits which you can use programmatically
- Google Maps
- YouTube
- Your program can use these services
- You may need to pay for access
Lecture 1.2 Web Services
Web-based Services
- Services "in the cloud"
- Runs on remote server, accepts requests from clients
- Interact using HTTP messages
Application Programming Interface
- An API defines the communication interface between programs
- Defines the format of Request/Response messages
- Defines legal message sequences
GET /maps/ HTTP/1.1
Software Development Kit(SDK)
- An SDK is set of tools to support the use of an API
- Typically library functions
- Programmer does not need to know the message details
- API and SDK may be used interchangeably
API:
GET /maps/ HTTP/1.1
SDK:
map = GetMap("UCI")
Lecture 1.3 Public APIs
Example API for Sentiment Analysis
- Sentiment analysis analyzes text to determine if it is positive or negative
- "I hate cats" score =-1
- "I love cats" score =+1
- AlchemyAPI (owned by IBM) does this
- Can download their SDK and use their API
- May not work on Raspberry Pi
AlchemyAPI SDK
- Want to can a web page and check its sentiment
- Webpage is written in HTML
from alchemyapi import AlchemyAPI
demo_html = 'AlchemyAPI works on HTML '
response = alchemyapi.sentiment('html' , demo_html)
if 'score' in
response['docSentiment']:
print('score: ',response['docSentiment']['score'])
Lesson 2
Lecture 2.1 Twitter's API
Twitter's API
Raspberry Pi can run several SDKs for Twitter's API
-
RPi can respond to tweets
- May look for a tag
Twython is the package we will use
-
Installing Twython
sudo apt-get update sudo apt-get install python-pip pip install twython
Pip is an installer for Python packages
Registering Your Twitter App
- Your app needs to be registered to access Twitter's API
- Need a twitter account first
- You will receive several keys needed for authentication
- Twython functions use keys to transmit messages
- Twitter servers authenticate if keys are correct
- Go to http://apps.twitter.com
- Click the Create New App button
Lecture 2.2 Twitter Registration
Create an application
- Any website is fine
- Click Create Twitter Application button at bottom
Application Details
- Details of app are shown
- Consumer Key will be needed
- Go to Keys and Access Tokens
Keys and Access Tokens
- Record Consumer Key and Consumer Secret
- Click Create my access token at bottom
Access Tokens
- Record Access Token and Access Token Secret
Lecture 2.3 Sending a Tweet
Sending a Tweet
-
Use the keys to create the Twython object
from twython import Twython C_KEY = "?????????" C_SECRET = "?????????" A_TOKEN = "?????????" A_SECRET = "?????????" api = Twython(C_KEY, C_SECRET, A_TOKEN, A_SECRET) api.update_status("Hi")
Searching for a Hashtag
- Maybe we want the Raspberry Pi to react to a hashtag
- When the hash tag appears, do something
- Need to scan the Twitter stream for the hashtag
Twython Streamer
A TwythonStreamer object allows you to examine public streams
TwythonStreamer has a method which searches for text in streams,
statuses.filter()
Pass as an argument the terms you want to track
-
i.e.
stream.statuses.filter(trach='Harris')
Lecture 2.4 Sending a Tweet(Demo)
Lesson 3
Lecture 3.1 Twython Callbacks
Using TwythonStreamer
- First, define a new class which extends TwythonStreamer
class MyStreamer(TwythonStreamer):
- This allows you to redefine some of the methods of TwythonStreamer
- Define a method
on_success()
inside your class -
on_success()
is a callback- Called when a tweet is found matching your criteria
on_success() callback
def on_success(self, data):
if 'text' in data:
print("Found it.")
- data is a dictionary passed to on_success()
- text field of data is the matching tweet
Lecture 3.2 Tweet Response
Filtering Twitter Streams
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
print("Found it.")
stream = MyStreamer(C_KEY, C_SECRET, A_TOKEN, A_SECRET)
stream.statuses.filter(track = "Harris")
- Create the MyStreamer class
- Callback prints a result
- Instantiate MyStreamer
- Filter the stream to find the text
Blinking in Response to a Tweet
def blink():
GPIO.output(13, GPIO.HIGH)
time.sleep(1)
GPIO.output(13, GPIO.LOW)
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
blink()
- Let's make an LED blink when a tweet is received
Lecture 3.3 Responding to a tweet(Demo)
Week 4
camera module setup
camera readme
camera python readme
Lesson 1
Lecture 1.1 Camera Module
Raspberry Pi Camera Module
- Camera are useful sensors
- There are many cameras that can be used with Raspberry Pis (USB)
- The Raspberry Pi camera module is useful
- Camera Serial Interface (CSI) to the processor
- Good libraries for control
Enabling the Camera
-
Use
raspi-config
to enable the CSI Interface
sudo raspi-config
Select
Enable Camera, Finish
Reboot the RPi
Lecture 1.2 picamera Library
python-picamera Library
- Install the library
sudo apt-get update
sudo apt-get install python3-picamera
- Using the library
import picamera
- Instantiate an object of the PiCamera class
camera = picamera.Picamera()
Camera Functions
- Take a picture
camera.capture("pict.jpg")
- Changing camera settings
- There are many
camera.hflip = True
camera.vflip = True
camera.brightness = 50
camera.sharpness = 0
- View video on RPi screen
camera.start_preview()
camera.stop_preview()
Video Recording
import picamera
import time
camera.start_recording("vid.h264")
time.sleep(10)
camera.stop_recording()
Lecture 1.3 Capturing Images
Sending an Image on the Network
- What if you want to a remote site?
- Home security system
- Capture an image, send it on a connection
mysocket = socket.socket()
mysocket.connect(('aserver', 8000))
conn = mysocket.makefile('wb')
camera.capture(conn,'jpeg')
- Need a file-like object to capture to
TImelapse Rhtography
-
camera.capture_connections()
takes photos over time - Takes one argument, a file name
-
{counter}
andtimestamp
can be substituted - ex.
picture{counter}.gpg
produces picture1.jpg, picture2.jpg,etc
Continuous Capture
camera = picamera.Picamera()
for filename in
camera.capture_continuous(img(counter),jpg)
time sleep(300)
- Iterate through all images
- Infinite loop
- 5 minute delay between images
Lesson 2
Lecture 2.1 Camera(Demo)
Lecture 2.2 PWM on RPi
Pulse Width Modulation
- Duty cycle is the percent of time the pulse is high
- Increasing duty cycle increases perceived voltage
Servo Motors
- Motors whose rotation angle can be precisely controlled
- Pulse width controls rotation angle
- 50Hz frequency is normal
- 1.0 ms width = 0 degrees
- 2.0 ms width = 180 degrees
- Same interface is used for remote control
- Receiver sends PWM to control interfaces
Lecture 2.3 Servo Control
Servo Motor Wiring
- Three wires:
- Power (Red)
- Ground (Black)
- Signal (White or Yellow)
- PWM applied to signal wire
- Power should supply sufficient current
Servo Motor Power
- External power supply should be used
- ~5 volts
- Raspberry Pi cannot supply enough current to drive typical motors
- ~1 Amp
- External supply has power and ground wires
- Wire external power wire to Servo power wire
- External ground wired to Raspberry Pi ground
Servo Wiring
- Only pins 12 and 24 (BOARD numbering) can produce PWM signals
- Resistor might be used to isolate pins from servo
Lesson 3
Lecture 3.1 Servo Code
Servo Scan Code Example
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 50)
pwm.start(0)
foreach i in range(100):
pwm.ChangeDutyCycle(i)
time.sleep(0.1)
foreach i in range(100, 0, -1):
pwm.ChangeDutyCycle(i)
time.sleep(0.1)