[Mooc]IoT Course 5 Interfacing w

2017-05-26  本文已影响0人  Vinchent

[Mooc]IoT Course 4 Interfacing with the Raspberry Pi

Week 1

Reading Material

Lesson 1

Lecture 1.1 Network

Connecting to a Network
Wi-Fi Config
Using a Web Browser
Firewall Warning

Lecture 1.2 Secure Shell

Secure Shell
SSH Requirements

Lecture 1.3 SSH Client/Server

Running the SSH Client
SSH Client Screen Shot
Raspberry Pi SSH Server
sudo rm /etc/ssh/ssh_host_* && sudo dpkg-reconfigure openssh-server

Lesson 2

Lecture 2.1 SSH Server

Getting Your IP Address
Accessing Raspberry Pi w/ SSH

Lecture 2.2 Network Programs

Programmatic Networking
Internet Structure

A hierarchy of local area networks(LANs) connected by routers

IoTM5W1L2.2.png
Internet Protocols

Lecture 2.3 Internet Protocols

Key Features of Internet Protocols
Internet Protocol Family
TCP vs. UDP

Lesson 3

Lecture 3.1 IP Addresses

IP Addresses
IPv6
Ports

Lecture 3.2 Domain Names

Host Names & Domain Names
Domain Naming System (DNS)
nslookup

Program that performs a DNS lookup and returns the IP address

Lecture 3.3 Client/Server

Client-Server Model
IoTM5W1L3.3.png
Internet Connections
Ports and Servers
IoTM5W1L3.3_2.png

Week 2

Reading material

Lesson 1

Lecture 1.1 Sockets

Sockets Interface
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)

Lecture 1.2 Sending Data

Connect Socket to Server
host = socket.gethostbyname("www.google.com")
mysock.connect(host, 80)
Sending Data on a Socket
message = "GET / HHTTP/1.1\r\n\r\n"

mysock.sendall(message)
Receiving Data on a Socket
data = mysock.recv(1000)
Closing a Socket
mysock.close()

Lecture 1.3 Exceptions

Errors During Execution
Handling Exceptions
try:
  z = x / y
except ZeroDivisionError:
  print("Divide by zero")
Socket Exceptions
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)
Listening and Accepting a Connection
mysock.listen(5)
conn, addr = mysock.accept()

Lecture 2.2 Live Server

Sending and Receiving
data = conn.recv(1000)
conn.sendall(data)

conn.close()
mysock.close()
A Live Server
while True:
    conn, addr = mysock.accept()
    data = conn.recv(1000)
    if not data:
        break
    conn.sendall(data)
    
conn.close()
mysock.close()
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
Internet Controlled 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)

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
Protocol-Specific Libraries
import http.client
conn = httplib.HTTPConnection("www.google.com")
conn.request("GET","/")
Interaction with Online Services

Lecture 1.2 Web Services

Web-based Services
Application Programming Interface
GET /maps/<place> HTTP/1.1
Software Development Kit(SDK)

API:

GET /maps/<place> HTTP/1.1

SDK:

map = GetMap("UCI")

Lecture 1.3 Public APIs

Example API for Sentiment Analysis
AlchemyAPI SDK
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
Registering Your Twitter App

Lecture 2.2 Twitter Registration

Create an application
Application Details
Keys and Access Tokens
Access Tokens

Lecture 2.3 Sending a Tweet

Sending a Tweet

Lecture 2.4 Sending a Tweet(Demo)

Lesson 3

Lecture 3.1 Twython Callbacks

Using TwythonStreamer
on_success() callback
def on_success(self, data):
    if 'text' in data:
        print("Found it.")

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")
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()

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
Enabling the Camera

Lecture 1.2 picamera Library

python-picamera Library
sudo apt-get update
sudo apt-get install python3-picamera
import picamera
camera = picamera.Picamera()
Camera Functions
camera.capture("pict.jpg")
camera.hflip = True
camera.vflip = True
camera.brightness = 50
camera.sharpness = 0
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
mysocket = socket.socket()
mysocket.connect(('aserver', 8000))
conn = mysocket.makefile('wb')
camera.capture(conn,'jpeg')
TImelapse Rhtography
Continuous Capture
camera = picamera.Picamera()
    for filename in
        camera.capture_continuous(img(counter),jpg)
            time sleep(300)

Lesson 2

Lecture 2.1 Camera(Demo)

Lecture 2.2 PWM on RPi

Pulse Width Modulation
Servo Motors

Lecture 2.3 Servo Control

Servo Motor Wiring
Servo Motor Power
Servo Wiring
IoTM5W4L2.3.png

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)

上一篇 下一篇

猜你喜欢

热点阅读