Skip to content

Commit e62ae30

Browse files
committed
Upload
1 parent 43b444a commit e62ae30

File tree

127 files changed

+12120
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+12120
-2
lines changed

Diff for: README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
# Practical-Python-Programming-for-IoT
2-
Practical Python Programming for IoT, published by Packt
1+
# Practical Python Programming for IoT
2+
3+
The code in this repository accompanies the Packt Book Practical Python Programming for IoT, published by Packt.
4+
5+
## [Chapter 1](chapter01) - Setting Up Your Development Environment
6+
7+
## [Chapter 2](chapter02) - Introduction to Python and IoT
8+
9+
## [Chapter 3](chapter03) - RESTFul APIs and Web Socket Services with Flask
10+
11+
## [Chapter 4](chapter04) - Distributed Systems with Python and MQTT
12+
13+
## [Chapter 5](chapter05) - Connecting Your Raspberry Pi to the Physical World
14+
15+
## [Chapter 6](chapter06) - Electronics 101 for the Software Engineer
16+
17+
## [Chapter 7](chapter07) - Switching Things On and Off
18+
19+
## [Chapter 8](chapter08) - Lights, Indicators and Displaying Information
20+
21+
## [Chapter 9](chapter09) - Measuring Temperature, Humidity and Moisture
22+
23+
## [Chapter 10](chapter10) - Movement with Motors, Servos and Steppers
24+
25+
## [Chapter 11](chapter11) - Measuring Distance and Detecting Movement
26+
27+
## [Chapter 12](chapter12) - Advanced IoT Programming Concepts - Event-Loops, Threads, PubSub & AsyncIO
28+
29+
## [Chapter 13](chapter13) - IoT and Automation Platforms
30+
31+
## [Chapter 14](chapter14) - Tying it Altogether - An IoT Christmas Tree
32+

Diff for: chapter01/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Practical Python Programming for IoT
2+
3+
## Chapter 1 - Setting Up Your Development Environment
4+
5+
* `requirements.txt` - Python dependencies required for this chapter
6+
7+
* `gpio_pkg_check.py` - Verify availability of Python GPIO Libraries
8+
9+
* `run_on_boot.sh` - Bash helper script to start a Python program on boot

Diff for: chapter01/gpio_pkg_check.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
File: chapter01/gpio_pkg_check.py
3+
4+
This Python script checks for the availability of various Python GPIO Library Packages for the Raspberry Pi.
5+
It does this by attempting to import the Python package. If the package import is successful
6+
we report the package as Available, and if the import (or import initialization) fails for any reason,
7+
we report the package as Unavailable.
8+
9+
Dependencies:
10+
pip3 install gpiozero pigpio
11+
12+
Built and tested with Python 3.7 on Raspberry Pi 4 Model B
13+
"""
14+
try:
15+
import gpiozero
16+
print('GPIOZero Available')
17+
except:
18+
print('GPIOZero Unavailable. Install with "pip install gpiozero"')
19+
20+
try:
21+
import pigpio
22+
print('PiGPIO Available')
23+
except:
24+
print('PiGPIO Unavailable. Install with "pip install pigpio"')
25+

Diff for: chapter01/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
colorzero==1.1
2+
gpiozero==1.5.1
3+
pigpio==1.44

Diff for: chapter01/run_on_boot.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
#File: chapter01/run_on_boot.sh
3+
4+
# Absolute path to Virtual Environment python interpreter
5+
PYTHON=/home/pi/pyiot/chapter01/venv/bin/python
6+
7+
# Absolute path to Python script
8+
SCRIPT=/home/pi/pyiot/chapter01/gpio_pkg_check.py
9+
10+
# Absolute path to output log file
11+
LOG=/home/pi/pyiot/chapter01/gpio_pkg_check.log
12+
13+
echo -e "\n####### STARTUP $(date) ######\n" >> $LOG
14+
15+
$PYTHON $SCRIPT >> $LOG 2>&1

Diff for: chapter02/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Practical Python Programming for IoT
2+
3+
## Chapter 2 - Introduction to Python and IoT
4+
5+
* `requirements.txt` - Python dependencies required for this chapter
6+
7+
* `led_gpiozero.py` - Flashing a LED using GPIOZero
8+
9+
* `led_pigpio.py` - Flashing a LED using PiGPIO
10+
11+
* `button_gpiozero.py` - Responding to a button using GPIOZero
12+
13+
* `button_pigpio.py` - Responding to a button using PiGPIO
14+
15+
* `dweet_led.py` - Controlling a LED over the Internet using dweet.io
16+
17+
* `dweet_button.py` - Creating a dweet using a push button
18+
19+
* `pigpio_led_class.py` - PiGPIO class wrapper example

Diff for: chapter02/button_gpiozero.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
File: chapter02/button_gpiozero.py
3+
4+
Turn on and off an LED with a Button using GPIOZero.
5+
6+
Dependencies:
7+
pip3 install gpiozero pigpio
8+
9+
Built and tested with Python 3.7 on Raspberry Pi 4 Model B
10+
"""
11+
from gpiozero import Device, LED, Button # (1)
12+
from gpiozero.pins.pigpio import PiGPIOFactory
13+
import signal # (2)
14+
15+
LED_GPIO_PIN = 21
16+
BUTTON_GPIO_PIN = 23
17+
18+
Device.pin_factory = PiGPIOFactory() #set gpiozero to use pigpio by default.
19+
20+
def pressed():
21+
led.toggle() # (3)
22+
state = 'on' if led.value == 1 else 'off' # (4)
23+
print("Button pressed: LED is " + state) # (5)
24+
25+
led = LED(LED_GPIO_PIN)
26+
led.off()
27+
28+
button = Button(BUTTON_GPIO_PIN, pull_up=True, bounce_time=0.1) # (6)
29+
button.when_pressed = pressed # (7)
30+
31+
print("Press button to turn LED on and off.")
32+
33+
signal.pause() # Stops program from exiting. # (8)

Diff for: chapter02/button_pigpio.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
File: chapter02/button_gpiozero.py
3+
4+
Turn on and off an LED with a Button using PiGPIO.
5+
6+
Dependencies:
7+
pip3 install pigpio
8+
9+
Built and tested with Python 3.7 on Raspberry Pi 4 Model B
10+
"""
11+
import pigpio
12+
import signal
13+
14+
LED_GPIO_PIN = 21
15+
BUTTON_GPIO_PIN = 23
16+
17+
pi = pigpio.pi()
18+
19+
20+
# LED provides 'Output'
21+
pi.set_mode(LED_GPIO_PIN, pigpio.OUTPUT)
22+
pi.write(LED_GPIO_PIN, 0) # LED Off
23+
24+
25+
# Button provides 'Input'
26+
pi.set_mode(BUTTON_GPIO_PIN, pigpio.INPUT) # (1)
27+
pi.set_pull_up_down(BUTTON_GPIO_PIN, pigpio.PUD_UP) # (2)
28+
pi.set_glitch_filter(BUTTON_GPIO_PIN, 10000) # microseconds debounce # (3)
29+
30+
31+
# Button pressed handler
32+
def pressed(gpio_pin, level, tick): # (4)
33+
# Get current pin state for LED.
34+
led_state = pi.read(LED_GPIO_PIN) # (5)
35+
36+
if led_state == 1: # (6)
37+
# LED is on, so turn it off.
38+
pi.write(LED_GPIO_PIN, 0) # 0 = Pin Low = Led Off
39+
print("Button pressed: Led is off")
40+
else: # 0
41+
# LED is off, so turn it on.
42+
pi.write(LED_GPIO_PIN, 1) # 1 = Pin High = Led On
43+
print("Button pressed: Led is on")
44+
45+
46+
# Register button handler.
47+
pi.callback(BUTTON_GPIO_PIN, pigpio.FALLING_EDGE, pressed) # (7)
48+
49+
print("Press button to turn LED on and off.")
50+
51+
signal.pause() # Stops program from exiting.

Diff for: chapter02/dweet_button.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
File: chapter02/dweet_button.py
3+
4+
A Python program to control an LED using the public dweet.io service
5+
by using a Button to post a dweet.
6+
7+
Dependencies:
8+
pip3 install gpiozero pigpio requests
9+
10+
Built and tested with Python 3.7 on Raspberry Pi 4 Model B
11+
"""
12+
import signal
13+
import requests
14+
import logging
15+
from gpiozero import Device, Button
16+
from gpiozero.pins.pigpio import PiGPIOFactory
17+
18+
# Initialize Logging
19+
logging.basicConfig(level=logging.WARNING)
20+
logger = logging.getLogger('main')
21+
logger.setLevel(logging.INFO)
22+
23+
# Initialise GPIOZero
24+
Device.pin_factory = PiGPIOFactory()
25+
26+
BUTTON_GPIO_PIN = 23
27+
button = None
28+
LED_STATES = ['off', 'on', 'blink']
29+
current_led_state = 0 # off
30+
31+
# Make sure thing_name matches the "dweet_led thing" you want to control.
32+
thing_name = '**** ADD YOUR THING NAME HERE ****'
33+
URL = 'https://dweet.io'
34+
35+
36+
def init_button():
37+
"""Setup button"""
38+
global button
39+
button = Button(BUTTON_GPIO_PIN, pull_up=True, bounce_time=0.1)
40+
button.when_pressed = button_pressed
41+
42+
43+
def button_pressed():
44+
"""Button pressed handler"""
45+
cycle_led_state()
46+
47+
48+
def cycle_led_state():
49+
"""Send revolving dweet to change LED from off -> on -> blink -> off -> ..."""
50+
global current_led_state
51+
current_led_state += 1
52+
53+
if current_led_state >= len(LED_STATES):
54+
current_led_state = 0
55+
56+
state = LED_STATES[current_led_state]
57+
58+
logger.info('Setting LED state %s', state)
59+
send_dweet(thing_name, {'state': state})
60+
61+
62+
def send_dweet(thing_name, values):
63+
"""Send a dweet to a thing."""
64+
65+
resource = URL + '/dweet/for/' + thing_name
66+
logger.debug('Dweeting to url %s with values %s', resource, values)
67+
68+
r = requests.get(resource, params=values)
69+
70+
if r.status_code == 200:
71+
dweet_response = r.json()
72+
logger.debug('Dweet response was %s', dweet_response)
73+
return dweet_response
74+
75+
else:
76+
logger.error('Dweeting dweet failed with http status %s', r.status_code)
77+
return {}
78+
79+
80+
# Initialise Module
81+
init_button()
82+
83+
84+
# Main entry point
85+
if __name__ == '__main__':
86+
# You could adopt the get_last_dweet() / process_dweet() from dweet_listner.py
87+
# to initialise the led state in this file. For simplicity we're just
88+
# starting with 'Off' (current_led_state = 0)
89+
90+
print("Press button to send a dweet to turn LED on, blink or off.")
91+
92+
# Stop Python from exiting.
93+
signal.pause()

0 commit comments

Comments
 (0)