IoT System Development with Raspberry Pi and Django 此页可以删除
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi 2 Raspberry Pi connects sensors 3 The Web Application
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi (1) Models (2) Parts (3) Electronic Components 3 (4) Operating system (5) Start Up
Raspberry Pi Raspberry Pi: a tiny computer integrated on a single circuit board with linux-based operating system. Size: like a credit card. Function: all the basic functions of a PC.
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi (1) Models (2) Parts (3) Electronic Components 3 (4) Operating system (5) Start Up
Cheaper, Lower CPU and memory, Fewer ports and accessories Models There are many different models of Raspberry Pi: Model A, A+, B, B+, 2B, 3B, 3B+....... At present, the latest models have two: Raspberry Pi 3B Raspberry Pi zero (including zero w) Cheaper, Lower CPU and memory, Fewer ports and accessories
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi (1) Models (2) Parts (3) Electronic Components 3 (4) Operating system (5) Start Up
Parts Power supply Micro SD card Displayer Keyboard and mouse
Power Supply To connect to a power socket, all Raspberry Pi models have a USB port (the same found on many mobile phones):
Micro SD Card Raspberry Pi doesn’t have a hard drive. A Micro SD card is used to store all its files.
Displayer A TV or computer screen You need a screen, and a cable to link the screen and the Pi. There is a HDMI port on Raspberry Pi.
Keyboard and Mouse Raspberry PI built-in bluetooth, both USB or bluetooth wireless keyboard and mouse can be used
Other optional extras Adapter A case Headphones or speakers Many computer monitors may also have DVI or VGA ports. A case This is not essential, but you can put your Raspberry Pi in a case to protect it. Headphones or speakers The large Raspberry Pi models (but not Pi Zero/Zero W) have a standard audio port like the one on your smart phone or MP3 players. An Ethernet cable The large Raspberry Pi models (but not Pi Zero/Zero W) have a standard Ethernet port to connect them to the Internet. Raspberry Pi 4, 3, and Pi Zero W can also be wirelessly connected to the Internet.
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi (1) Models (2) Parts (3) Electronic Components 3 (4) Operating system (5) Start Up
Bread Plate By inserting the components into the hole, the circuit and components can be tested without welding or manual wiring
Connection Male and female connection
LED Diode and Resistor
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi (1) Models (2) Parts (3) Electronic Components 3 (4) Operating system (5) Start Up
Operating System The official operating system is Raspbian, a customized version of Debian. There is an official installer, NOOBS, that allows you to install Raspbian in a relatively simple way.
Introduction To Raspberry Pi 目录 Contents 1 Introduction To Raspberry Pi (1) Models (2) Parts (3) Electronic Components 3 (4) Operating system (5) Start Up
Connection It’s important to do this in the right order, so that all your components are safe.
Right Order Insert the SD card with Raspbian. Connect the mouse to a USB port on Raspberry Pi. Connect the keyboard in the same way. Plug the screen into a wall socket and switched on. Connect the screen to Raspberry Pi’s HDMI port.
Start Up Raspberry Pi doesn’t have a power switch: as soon as you connect it to a power outlet, it will turn on. As it starts up (booting), a raspberry will appear in the top left- hand corner of the screen.
Start up After a few seconds the Raspbian Desktop will appear.
Raspbian Desktop
Raspberry Pi connects sensors 目录 Contents 2 Raspberry Pi connects sensors (1) Sensors (2) GPIO & Pins (3) Temperature and Humidity Sensor 3 (4) Smoke Sensor
Raspberry Pi connects sensors 目录 Contents 2 Raspberry Pi connects sensors (1) Sensors (2) GPIO & Pins (3) Temperature and Humidity Sensor 3 (4) Smoke Sensor
Sensors Definition: A device that senses specified measurements and converts them into usable signals according to certain rules (mathematical function rules.) Constitute: A sensor usually consists of a sensitive element and a conversion element. Characteristic: Miniaturization, digitalization, intelligentization, multi-function, systematization and networking
Different Sensors
Different Sensors Temperature and humidity sensor Photosensitive resistance sensor Acceleration sensor Flame sensor Tilt sensor Hall sensor Sound sensor Smoke sensor
Raspberry Pi connects sensors 目录 Contents 2 Raspberry Pi connects sensors (1) Sensors (2) GPIO & Pins (3) Temperature and Humidity Sensor 3 (4) Smoke Sensor
Raspberry Pi Pin Chart
Raspberry Pi Pin Chart There are three different names for the pin number of raspberry pi: wiringPi, BCM GPIO and HEADER GPIO.setmode(GPIO.BCM)
BCM
Raspberry Pi connects sensors 目录 Contents 2 Raspberry Pi connects sensors (1) Sensors (2) GPIO & Pins (3) Temperature and Humidity Sensor 3 (4) Smoke Sensor
DHT11 Application field: DHT11 is a compound temperature and humidity sensor with calibrated digital signal output. Application field: Air conditioning testing and testing equipment Dehumidifier Automobile data recorder Automatic control of consumer goods Medical humidity regulator
DHT11
Connect to the Pi
A library that encapsulates related functions Get the digital values Step 1 Update package sudo apt-get update sudo apt-get install build-essential python-dev Step 2 Get Adafruit from GitHub sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT A library that encapsulates related functions
Represent the DHT11 and data pin, respectively. Get the digital values Step 3 Install this library to both python2 and python3 sudo python setup.py install sudo python3 setup.py install Step 4 Run the sample program cd examples python AdafruitDHT.py 11 17 Represent the DHT11 and data pin, respectively.
AdafruitDHT.py import sys import Adafruit_DHT sensor_args = { '11': Adafruit_DHT.DHT11,'22': Adafruit_DHT.DHT22, '2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>') print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4') sys.exit(1)
AdafruitDHT.py results humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)) else: print('Failed to get reading. Try again!') sys.exit(1) results
Use it in other programs Import Adafruit_DHT sensor = Adafruit_DHT.DHT11 gpio = 17 humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
Raspberry Pi connects sensors 目录 Contents 2 Raspberry Pi connects sensors (1) Sensors (2) GPIO & Pins (3) Temperature and Humidity Sensor 3 (4) Smoke Sensor
Smoke Sensor
Smoke Sensor Import RPi.GPIO as GPID Import time CHANNEL = 7 #the data out is connected to pin 7 GPIO.setmode(GPIO.BOARD) # set pin numbering mode GPIO.setup (CHANNEL, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # The initial level of the input port is set to low def action(pin): print’Sensor detected action!’
Smoke Sensor GPIO.add_event_detect(CHANNEL, GPIO.RISING) # monitor the change of input port GPIO.add_event_callback(CHANNEL, action) #if change, execute action function try: while True: print’alive’ time.sleep(0.5) except KeyboardInterrupt: GPIO.cleanup()
The Web Application Web Architecture Web Communication 目录 Contents 3 The Web Application (1) Web Architecture (2) Web Communication (3) Environment Setup & Configuration 3 (4) Demo
The Web Application Web Architecture Web Communication 目录 Contents 3 The Web Application (1) Web Architecture (2) Web Communication (3) Environment Setup & Configuration 3 (4) Demo
Web Architecture Django V.S. Java Frameworks: Spring Framework, Servlet, JSP…… Here some comparison of Python/Django stack over Java/Spring stack: Python/Django feels quicker and lighter than Java/Spring stack which result in productivity boost. Django admin is great! Java technologies servlet, beans, XML configurations etc have more mental overhead which I do not like。 Easier !
Django A view of Django MTV framework
The Web Application Web Architecture Web Communication 目录 Contents 3 The Web Application (1) Web Architecture (2) Web Communication (3) Environment Setup & Configuration 3 (4) Demo
Web Communication HTTP Connection HTTP connection is a protocol that runs on a socket. HTTP connection is a higher-level abstraction of a network connection. Socket Connection With Socket connection you need to take care of all the lower-level details of a TCP/IP connection.
GET vs. POST The following table compares the two HTTP methods: GET and POST.
The Web Application Web Architecture Web Communication 目录 Contents 3 The Web Application (1) Web Architecture (2) Web Communication (3) Environment Setup & Configuration 3 (4) Demo
Install the configuration According to prepare.docx. We use Django+uwsgi+nginx as our framework. Main Thought: Client 1 request return Connect Client 2 Sensor Host Client 3
The Web Application Web Architecture Web Communication 目录 Contents 3 The Web Application (1) Web Architecture (2) Web Communication (3) Environment Setup & Configuration 3 (4) Demo
Create Django project 1. create Django project called helloworld: django-admin.py startproject helloworld 2.create application called raspberrypistate: cd /helloworld python manage.py startapp raspberrypistate Edit settings.py & urls.py
Create Django project Edit raspberrypistate’s urls.py cd ../raspberrypistate Edit raspberrypistate’s views.py
Create Django project Structure helloworld raspberrypistate settings.py urls.py views.py Django Project Project main settings Application By url, you can call related function realized in vews,py Write your functions here templates Html files
Create Django project cd /home/pi/helloworld python manage.py migrate python manage.py makemigrations raspberrypistate sudo systemctl restart emperor.uwsgi.service Type the url on Raspberry Pi--http://127.0.0.1/raspberrypistate In this way, a Django project is created.
First target—Read Pi’s temperature of CPU The way to get the temperature: Then your can write this on views.py:
Read Pi’s temperature of CPU sudo systemctl restart emperor.uwsgi.service Type the url on Raspberry Pi--http://127.0.0.1/raspberrypistate You only need edit views.py. CPU: 55.3℃
Second target—Read DHT11’s value DHT11 -- Humidity & Temperature Sensor
Read DHT11’s value View the pin diagram gpio readall
Read DHT11’s temperature The way to get DHT11’s value Then your can write this on views.py:
Read DHT11’s temperature sudo systemctl restart emperor.uwsgi.service Type the url on Raspberry Pi--http://127.0.0.1/raspberrypistate You only need edit views.py. Temperature:29℃ humidity: 69%
The way to provide service The host needs to provide service. Clients in Local Area Network need get responses from the host. That is easy— You only need to edit settings.py: Use ifconfig –a find your Raspberry Pi’s IP address Then edit settings.py In this way, clients 192.168.2.121/raspberrypistate
Different Types of Echarts
Echarts
Reference https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up https://projects.raspberrypi.org/en/projects/raspberry-pi-using