[Mooc]IoT Course 5 Interfacing with the Raspberry Pi

[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 <username>@<domainname>
  • <username> is your username on the server machine
  • <domainename> 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
IoTM5W1L2.2.png
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
  • 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
IoTM5W1L3.3.png
  • 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
IoTM5W1L3.3_2.png
  • 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:

  1. Create a socket
  2. Connect socket to server
  3. Send some data (a request)
  4. Receive some data (a response)
  5. 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

  1. Create a socket
  2. Bind the socket to an IP address and port
  3. Listen for a connection
  4. Accept the connection
  5. Receive the request
  6. 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 a connect()
    • 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() and sendall() 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
    • Twitter
    • YouTube
    • Facebook
  • 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/<place> 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/<place> 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 = '<html><body>AlchemyAPI works on HTML </body></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}and timestamp 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
IoTM5W4L2.3.png
  • 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)

Lecture 3.2 Servo (Demo)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,907评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,546评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,705评论 0 238
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,624评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,940评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,371评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,672评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,396评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,069评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,350评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,876评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,243评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,847评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,004评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,755评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,378评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,266评论 2 259

推荐阅读更多精彩内容