README.md 7.66 KB
Newer Older
frankie's avatar
frankie committed
1
2
# Objets connectés / Raspbery Pi + Python

frankie's avatar
frankie committed
3
4
[![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/)

frankie's avatar
minor    
frankie committed
5
6
![banner](https://legacy.imal.org/sites/default/files/nodeuploads/2019/06/rj45-hand_obrazek3_duzy_tuned.png)

frankie's avatar
frankie committed
7
8
9
imal, 30 juillet - 2 août, https://legacy.imal.org/summerworkshops2019/raspberrypi#python

scripts and notes of the workshop
frankie's avatar
frankie committed
10

frankie's avatar
frankie committed
11
12
13
## scripts

### web
frankie's avatar
frankie committed
14
15
16

**web00_server.py** : basic file server (start it in the *html* folder with `python ../web00_server.py` to make visible the base root stuff)

frankie's avatar
frankie committed
17
#### GET requests
18

frankie's avatar
frankie committed
19
**web01_server.py** : parsing GET requests, and responding to the browser
frankie's avatar
frankie committed
20

frankie's avatar
frankie committed
21
**web02_server.py** : changing returned message depending on the get parameters
frankie's avatar
frankie committed
22

23
24
**web03_server.py** : adapting header to send images, big piece of code, go smoothly!

frankie's avatar
frankie committed
25
26
27
**webserver.py** : script developped during the workshop

#### POST requests
28

29
**web04_server.py** : parsing POST requests and saving files on drive
30

31
**web05_server.py** : listing files uploaded & providing links to files
32

frankie's avatar
frankie committed
33
34
35
**webpubliser.py** : script developped during the workshop

#### scrapping
frankie's avatar
frankie committed
36
37
38
39
40

**beautifulsoup00.py** : simple example on how to extract content from a webpage

requires the installation of python-bs4, on linux:

frankie's avatar
frankie committed
41
```bash
frankie's avatar
frankie committed
42
sudo apt install python-bs4
frankie's avatar
frankie committed
43
```
frankie's avatar
frankie committed
44
45
46
47
48

**beautifulsoup01.py** : advanced example using selenium's webdriver

requirements:

frankie's avatar
frankie committed
49
50
works on linux desktop 64 bits, for raspberry, see below

frankie's avatar
frankie committed
51
```bash
frankie's avatar
frankie committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
sudo apt install python-bs4
sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
sudo apt-get install -f
sudo apt-get install xvfb
sudo apt-get install unzip
wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
sudo -H python3 -m pip install pyvirtualdisplay selenium
frankie's avatar
frankie committed
66
```
frankie's avatar
frankie committed
67

frankie's avatar
frankie committed
68
69
70
71
on raspberry:

- https://raspberrypi.stackexchange.com/questions/4941/can-i-run-selenium-webdriver-using-firefox-as-the-browser

frankie's avatar
frankie committed
72
73
74
75
source: https://christopher.su/2015/selenium-chromedriver-ubuntu/

test your installation with the script **selenium_chrome.py**

frankie's avatar
minor    
frankie committed
76
77
78
79
80
**tweetscrapper00.py** : get all tweets from a user, depending on twitter definition of 'all'

**tweetscrapper01.py** : another script to get tweets form a specific user

source: https://stackoverflow.com/questions/42273089/scraping-number-of-retweets-using-beautifulsoup
frankie's avatar
frankie committed
81

frankie's avatar
frankie committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
**twitter/get_tweets_by_hashtag.py**: script developped by [Amélie Dumont](https://gitlab.com/ameliedumont) retrieving tweets by hashtag

#### flask

[Flask](https://www.fullstackpython.com/flask.html) is an advanced webserver, compare to SimpleHTTPServer used in the scripts above.

The folder **app_flask** contains a project of [Amélie Dumont](https://gitlab.com/ameliedumont) that controls the rotation of a servo motor via a web interface.

#### json

**json_example.py** : demo on how to serialise and deserialise python data using json

### OSC

[Open Sound Control](http://opensoundcontrol.org/) demo in python, allows to send and receive data using UDP protocol.

**osc_sender.py** : how to send OSC messages

**osc_receiver.py** : how to receive OSC messages 

**osc_broadcaster.py** : quick and dirty way to send messages to all computers in [LAN](https://en.wikipedia.org/wiki/Local_area_network)

### Threading

[Multithreading](https://en.wikipedia.org/wiki/Thread_%28computing%29) designates the fact that several application runs simultaneously in a computer. The lastest Raspberry PI having a multicore CPU, threading allows to run several applications/scripts at the same time, without waiting for a process to complete to starts another one. This is an important topics to master when creating games, robots, or any other demanding application running in realtime, reacting to inputs or parsing live feeds.

**threading_example.py** : shows how to run several 'applications' within one script

frankie's avatar
frankie committed
110
111
112
113
114
115
### subprocess

to launch programs with python, use subprocess.call, see [documentation](https://docs.python.org/2/library/subprocess.html) for more details

**vlc_launcher.py** : play a video in fullscreen with vlc

frankie's avatar
frankie committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
### PIL

Pillow is a library to manipulate images in python.

**image_manipulation.py** : shows basic PIL functionalities

### OpenCV

[OpenCV](http://opencv.org) is a powerful image processing library, massively used in computer vision. It is trickier to understand then PIL because it is based on matrix transformations.

**motion_detection.py** : detection of motions in live video (webcam in this case)

**motion_detection_pi.py** : adaptation of the script for [picam](https://picamera.readthedocs.io/en/release-1.13/), with desactivation of white balance and OSC emission of the anaylis.

### PyGame

[PyGame](https://www.pygame.org/news) is a library containing all the required features to create games in python.

**pygam/game.py** : basic script to run a game with keyboard interactions

**pygam/particles.py** : a simple particles system interacting with the mouse (2D)

**pygam/particles_3dgrid.py** : 3D adaptation of the particles, using OpenGL
frankie's avatar
frankie committed
139

frankie's avatar
frankie committed
140
**pygam/pygl.py** : OpenGL demo using pygame by [Bastiaan Zapf](https://python-opengl-examples.blogspot.com/)
frankie's avatar
frankie committed
141

frankie's avatar
frankie committed
142
143
## resources

frankie's avatar
frankie committed
144
145
### links

frankie's avatar
frankie committed
146
- [Simple HTTP server](https://docs.python.org/2/library/simplehttpserver.html)
147
148
149
150
151
- [Basic example of GET and POST management](https://www.codexpedia.com/python/python-web-server-for-get-and-post-requests/)
- [Serving an image with Simple HTTP Server](https://www.reddit.com/r/learnpython/comments/1sendp/display_image_on_browser_using_python_http_server/)
- [Checking fields in a POST form](https://blog.anvileight.com/posts/simple-python-http-server/#do-post)
- [Saving file in a POST form](https://stackoverflow.com/questions/49734694/how-to-upload-files-to-server-from-html-form-using-python-cgi)
- [Documentation about CGI](https://docs.python.org/2/library/cgi.html)
frankie's avatar
frankie committed
152
153
154
155

- [Build a Python Web Server with Flask](https://projects.raspberrypi.org/en/projects/python-web-server-with-flask)
- [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/)

156
157
- [All possible Content-Type for HTTP headers](https://stackoverflow.com/questions/23714383/what-are-all-the-possible-values-for-http-content-type-header#37416922)

frankie's avatar
frankie committed
158
- [Beautiful soup official website](https://www.crummy.com/software/BeautifulSoup/)
frankie's avatar
frankie committed
159
- [Beautiful soup documentation](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors)
frankie's avatar
frankie committed
160
- [Beautiful soup for beginners](https://www.pythonforbeginners.com/beautifulsoup/beautifulsoup-4-python)
frankie's avatar
frankie committed
161
162
- [Installation of selenium with chrome driver](https://christopher.su/2015/selenium-chromedriver-ubuntu/)

163
164
- [Detect keyboard events](https://pythonhosted.org/pynput/keyboard.html)

frankie's avatar
frankie committed
165
166
all images of folder *html/images* are from https://libreshot.com/tag/red/ and uses a CC0 license

frankie's avatar
frankie committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
### about beautiful soup

to find nested tags, use this kind of call: loop over the first search, it will returns tags, and apply a new filter on these tags

the example below is retrieving H2 text of a wikipedia page

```python
for h2 in soup.find_all('h2'):
	for ptag in h2.find_all('span',{'class','mw-headline'}):
		print(ptag.text)
```

source: https://stackoverflow.com/questions/46510966/beautiful-soup-nested-tag-search?rq=1

with css selectors (same effect as above):

```python
for c in soup.select('h2 .mw-headline'):
    print(c.string)
```

source: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors

### usefull commands

frankie's avatar
frankie committed
192
```bash
frankie's avatar
frankie committed
193
194
killall python3
python3 webserver.py
frankie's avatar
frankie committed
195
196
197
sudo python3 -m pip install
sudo -H pip3 install --upgrade pip
sudo -H python3 -m pip install selenium
frankie's avatar
frankie committed
198
```