...menustart
- Raspberry Pi II
...menuend
- wired connection using the built-in RJ45 connector
- Wi-Fi connection using a USB Wi-Fi adapter
- start Wifi Config from the desktop
- Epiphany browse is builti-in
- Should change the private host ID keys
- Otherwise , they have the same default values.
One you install raspbian on your Pi, you get these default keys ,which are the same as everybody else's default keys.
sudo rm /etc/ssh/ssh_host_* && sudo dpkg-reconfigure openssh-server
- IP (Internet protocal)
- Host naming scheme , define IP address
- Host-to-host connection, unreliable
- UDP (Unreliable Datagram Protocal)
- Process-to-process communication
- Process naming
- Also unreliable
- TCP (Transmission Control Protocal)
- Process-to-process communication and naming
- Process naming
- Reliable communication
IP addresses are not easy for humans to memorize, so we use domain names. eg. cc.com
Each domain name must be resolved to an IP address to send packets.
So ,for that we use what's called 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
When you visit cnn.com, your web browser sends a message to a DNS server and says and look, what is the address of cnn.com? And if DNS server knows that address, it sends the IP address back to you. If it doesn't , then it goes and asks another DNS server higher up. The ip address will store in a local cache to save time.
nslookup baidu.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
Name: baidu.com
Address: 180.149.132.47
Name: baidu.com
Address: 220.181.57.217
Name: baidu.com
Address: 111.13.101.208
Name: baidu.com
Address: 123.125.114.144
- #53 is the port
- 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
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
import socket
mysock = socket.socket( socket.AF_INET , socket.SOCK_STREAM )
- AF_INET declares Address Family Internet
- SOCK_STREAM idicates TCP (connection-based)
>>> host = socket.gethostbyname( 'www.baidu.com' ) #不解析也可以
>>> PORT = 80
>>> mysock.connect( host, PORT ) # mysock.connect( (host, PORT) ) for 2.7
message = "GET / HTTP/1.1\r\n\r\n"
mysock.sendall(message)
- sendall() send the data , and tries until it succeeds.
data = mysock.recv(1024)
- recv() returns the data on the socket
- Blocking wait, by default , you can change that
- python will resize the data buffer for you1
mysock.close() # free up port
- Errors may happen when working with sockets
- Socket cannot be opened
- Data cannot be sent
- socket.error
- All socket errors
- gaierror
- getaddressinfo() - no host found
- 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
mysock = socket.socket( socket.AF_INET , socket.SOCK_STREAM )
mysock.bind( "",1234 ) # mysock.bind( ("",1234) ) 2.7
- bind() : the 1st argument it the host that you want to sssociate it with
- "" like a wildcard , it allows it to receive from any host
mysock.listen(5)
conn , addr = mysock.accept()
- listen() starts listening for a connect()
- argument is called backlog , it's the number of requests allowed to wait for service.
- accept() accepts a connection request
- return a connection (for sending/receiving) and an address (IP, port)
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()
- Use raspi-config to enable the CSI interface
- sudo raspi-config
- Select Enable Camera , done
-
sudo apt-get update sudo apt-get install python3-picamera
-
import picamera camera = picamera.PiCamera()
- 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()
-
camera.start_recording( "vid.h264" ) time.sleep(10) camera.stop_recording()
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
camera.capture_continuous()
- takes photos over time
- Takes one argument , a file name
- {counter} and {timestamp} can be substituted
- ex. "picture{counter}.jpg" produces picture1.jpg, picture2.jpg, etc.
camera = picamera.PiCamera()
for filename in
camera.capture_continuous('img{counter}.jpg'):
time.sleep(300) # 不知道什么用意
- Iterate through all images, infinite loop
- 5 minute delya between images