Skip to content

Commit bda64a6

Browse files
committed
Updates
1 parent 8dcb249 commit bda64a6

12 files changed

+81
-43
lines changed

BookCover.png

91.4 KB
Loading

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
The code in this repository accompanies the Packt Book Practical Python Programming for IoT, published by Packt.
44

5+
![Practical Python Programming for IoT Book Cover](./BookCover.png)
6+
7+
[Table of Contents (PDF)](./TableOfContents1stEd.pdf)
8+
9+
---
10+
11+
## Purchasing Practical Python Programming for IoT
12+
13+
Practical Python Programming for IoT is available through Packt and Amazon.
14+
15+
* [Amazon US](https://www.amazon.com/Practical-Python-Programming-IoT-WebSockets/dp/1838982469/ref=sr_1_1)
16+
* [Amazon AU](https://www.amazon.com.au/Practical-Python-Programming-IoT-WebSockets-ebook/dp/B08K3MD5Z1)
17+
* [Packt](https://www.packtpub.com/product/practical-python-programming-for-iot/9781838982461)
18+
19+
---
20+
21+
## Repository Contents
22+
523
## [Errata](errata) - Corrections and clarifications to the published content
624

725
## [Chapter 1](chapter01) - Setting Up Your Development Environment

TableOfContents1stEd.pdf

766 KB
Binary file not shown.

chapter02/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Chapter 2 - Introduction to Python and IoT
44

5+
Please see [Errata](../errata/README.md) for corrections and clarifications to the content and code found in the published book.
6+
57
* `requirements.txt` - Python dependencies required for this chapter
68

79
* `led_gpiozero.py` - Flashing a LED using GPIOZero

chapter02/button_pigpio.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,33 @@
1919

2020
# LED provides 'Output'
2121
pi.set_mode(LED_GPIO_PIN, pigpio.OUTPUT)
22-
pi.write(LED_GPIO_PIN, 0) # LED Off
22+
pi.write(LED_GPIO_PIN, 0) # LED Off
2323

2424

2525
# 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)
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, 100000) # 100000 microseconds = 0.1 secs # (3)
2929

3030

3131
# Button pressed handler
32-
def pressed(gpio_pin, level, tick): # (4)
32+
def pressed(gpio_pin, level, tick): # (4)
3333
# Get current pin state for LED.
34-
led_state = pi.read(LED_GPIO_PIN) # (5)
34+
led_state = pi.read(LED_GPIO_PIN) # (5)
3535

36-
if led_state == 1: # (6)
36+
if led_state == 1: # (6)
3737
# LED is on, so turn it off.
38-
pi.write(LED_GPIO_PIN, 0) # 0 = Pin Low = Led Off
38+
pi.write(LED_GPIO_PIN, 0) # 0 = Pin Low = Led Off
3939
print("Button pressed: Led is off")
40-
else: # 0
40+
else: # 0
4141
# LED is off, so turn it on.
42-
pi.write(LED_GPIO_PIN, 1) # 1 = Pin High = Led On
42+
pi.write(LED_GPIO_PIN, 1) # 1 = Pin High = Led On
4343
print("Button pressed: Led is on")
4444

4545

4646
# Register button handler.
47-
pi.callback(BUTTON_GPIO_PIN, pigpio.FALLING_EDGE, pressed) # (7)
47+
pi.callback(BUTTON_GPIO_PIN, pigpio.FALLING_EDGE, pressed) # (7)
4848

4949
print("Press button to turn LED on and off.")
5050

51-
signal.pause() # Stops program from exiting.
51+
signal.pause() # Stops program from exiting.

chapter02/dweet_button.py

100755100644
File mode changed.

chapter02/dweet_led.py

100755100644
+11-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# Initialize Logging
3333
logging.basicConfig(level=logging.WARNING) # Global logging configuration
3434
logger = logging.getLogger('main') # Logger for this module
35-
logger.setLevel(logging.INFO) # Debugging for this file. # (2)
35+
logger.setLevel(logging.INFO) # Debugging for this file. # (2)
3636

3737

3838
# Initialize GPIO
@@ -72,7 +72,7 @@ def get_latest_dweet():
7272
r = requests.get(resource) # (7)
7373

7474
if r.status_code == 200: # (8)
75-
dweet = r.json() # return a Python dict.
75+
dweet = r.json() # return a Python dict.
7676
logger.debug('Last dweet for thing was %s', dweet)
7777

7878
dweet_content = None
@@ -95,7 +95,7 @@ def poll_dweets_forever(delay_secs=2):
9595
if dweet is not None:
9696
process_dweet(dweet) # (12)
9797

98-
sleep(delay_secs) # (13)
98+
sleep(delay_secs) # (13)
9999

100100

101101
def stream_dweets_forever():
@@ -106,7 +106,7 @@ def stream_dweets_forever():
106106
session = requests.Session()
107107
request = requests.Request("GET", resource).prepare()
108108

109-
while True: # while True to reconnect on any disconnections.
109+
while True: # while True to reconnect on any disconnections.
110110
try:
111111
response = session.send(request, stream=True, timeout=1000)
112112

@@ -115,7 +115,7 @@ def stream_dweets_forever():
115115
try:
116116
json_str = line.splitlines()[1]
117117
json_str = json_str.decode('utf-8')
118-
dweet = json.loads(eval(json_str)) # json_str is a string in a string.
118+
dweet = json.loads(eval(json_str)) # json_str is a string in a string.
119119
logger.debug('Received a streamed dweet %s', dweet)
120120

121121
dweet_content = dweet['content']
@@ -125,7 +125,7 @@ def stream_dweets_forever():
125125
logger.error('Failed to process and parse dweet json string %s', json_str)
126126

127127
except requests.exceptions.RequestException as e:
128-
#Lost connection. The While loop will reconnect.
128+
# Lost connection. The While loop will reconnect.
129129
#logger.error(e, exc_info=True)
130130
pass
131131

@@ -143,13 +143,13 @@ def process_dweet(dweet):
143143
led_state = dweet['state']
144144

145145
if led_state == last_led_state: # (14)
146-
return; # LED is already in requested state.
146+
return # LED is already in requested state.
147147

148148
if led_state == 'on': # (15)
149149
led.on()
150150
elif led_state == 'blink':
151151
led.blink()
152-
else: # Off, including any unhandled state.
152+
else: # Off, including any unhandled state.
153153
led_state = 'off'
154154
led.off()
155155

@@ -189,6 +189,6 @@ def signal_handler(sig, frame):
189189
process_dweet(last_dweet)
190190

191191
print('Waiting for dweets. Press Control+C to exit.')
192-
#Only use one of the following. See notes later in Chapter.
193-
#stream_dweets_forever() # Stream dweets real-time.
194-
poll_dweets_forever() # Get dweets by polling a URL on a schedule. # (19)
192+
# Only use one of the following. See notes later in Chapter.
193+
# stream_dweets_forever() # Stream dweets real-time.
194+
poll_dweets_forever() # Get dweets by polling a URL on a schedule. # (19)

chapter02/pigpio_led_class.py

100755100644
File mode changed.

errata/Chapter2a.jpg

131 KB
Loading

errata/Chapter2b.png

140 KB
Loading

errata/Chapter2c.png

72.8 KB
Loading

errata/README.md

+38-20
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,63 @@
22

33
This page contains corrections and clarifications to the content found in the published book.
44

5-
## Chapter 2
5+
## Chapter 2 - Understanding the breadboard
66

7-
### Page 43 - Breadboard Examples
7+
Page 44 printed 1st edition; Page 42 PDF 1st edition.
88

9-
**Incorrect Text**
9+
![Code Example](./Chapter2a.jpg)
1010

11-
The 3rd bullet point reads _"A2 is not electrically connected to B2 (they don't share the same row)."_. This statement is incorrect because A2 _is_ electrically connected to B2 because they do share the same row.
11+
---
1212

13-
**Corrected Text**
13+
## Chapter 2 - Responding to a button press with PiGPIO
1414

15-
The 3rd bullet point should be _"A2 is not electrically connected to **A3** (they don't share the same row)."_.
15+
Page 67 printed 1st edition; Page 65 PDF 1st edition.
1616

17+
![Code Example](./Chapter2c.png)
1718

18-
### Page 72 - poll_dweets_forever() function
19+
Under the sub heading **Button pin configuration**, we see a snippet of code from the file [chapter02/button_pigpio.py](../chapter02/button_pigpio.py)
1920

20-
There is a 4 space indentation missing in the code example for the function `poll_dweets_forever()` at line 13. The effect of this means the statement `sleep(delay_secs)` is outside the `while` block and the loop never incurs the delay.
21+
At line 3 `set_glitch_filter` has the parameter 10000, which is the debounce time measured in __microseconds__.
2122

22-
This error does not affect a readers ability to successfully run the example, but it does make the following paragraph describing line 13 practically incorrect as the code does not incur the 2-second delay mentioned.
23+
The proceeding paragraph states the value is in __milliseconds__, which is incorrect.
2324

24-
The following image illustrates the issue.
25+
In this example, the timeout is therefore 10000 / 1000000 = 0.01 seconds
2526

26-
![Code Example](./Chapter2Page72.png)
27+
This difference in timing value is not expected to affect the reader's ability to complete the exercise. However, should the example behave erratically when the button is pressed, the reader should change the parameter at line 3 from `10000` to `100000` to make the debounce time 0.1 seconds. The corrected line will therefore be:
2728

28-
### Page 64 - Responding to a button press with PiGPIO
29+
```
30+
pi.set_glitch_filter(BUTTON_GPIO_PIN, 100000) # (3)
31+
```
2932

30-
Under the heading **Button pin configuration**, we see a snippet of code from the file `chapter02/button_gpiozero.py`
33+
This code change has been applied to the file [chapter02/button_pigpio.py](../chapter02/button_pigpio.py) in this repository.
3134

32-
At line 3 `set_glitch_filter` has the parameter 10000, which is the debounce time measured in __microseconds__.
35+
---
3336

34-
The proceeding paragraph states the value is in __milliseconds__, which is incorrect.
37+
## Chapter 2 - poll_dweets_forever() method
3538

36-
![Code Example](./Chapter2Page65.png)
39+
Page 74 printed 1st edition; Page 72 PDF 1st edition.
3740

38-
In this example, the timeout is therefore 10000 / 1000000 = 0.01 seconds
41+
![Code Example](./Chapter2b.png)
3942

40-
This difference in timing value is not expected to affect the reader's ability to complete the exercise. However, should the example behave erratically when the button is pressed, the reader should change the parameter at line 3 from `10000` to `100000` to make the debounce time 0.1 seconds. The corrected line will therefore be:
43+
44+
Under the heading **poll_dweets_forever() method**, we see a snippet of code from the file [chapter02/dweet_led.py](../chapter02/dweet_led.py)
45+
46+
There is a 4 space indentation missing in the code example for the function `poll_dweets_forever()` at line 13. The effect of this means the statement `sleep(delay_secs)` is outside the `while` block and the loop never incurs the delay.
47+
48+
This error does not affect a readers ability to successfully run the example, but it does make the paragraph (as pictured above) practically incorrect as the code does not incur the 2-second delay at line 13.
49+
50+
The corrected block of code is:
4151

4252
```
43-
pi.set_glitch_filter(BUTTON_GPIO_PIN, 100000) # (3)
53+
def poll_dweets_forever(delay_secs=2):
54+
"""Poll dweet.io for dweets about our thing."""
55+
while True:
56+
dweet = get_latest_dweet() # (11)
57+
if dweet is not None:
58+
process_dweet(dweet) # (12)
59+
60+
sleep(delay_secs) # (13)
4461
```
4562

46-
Also note that earlier in the chapter on Page 63, we see a code example from file `chapter02/button_gpiozero.py` using a debounce time of 0.1 seconds. The error on Page 64 and in file `chapter02/button_gpiozero.py` mean that the GPIOZero and PiGPIO examples are not strictly identical, although from an illustrative perspective for the purposes of the chapter they can be considered functionally identical.
63+
64+
This code correction has been applied to the file [chapter02/dweet_led.py](../chapter02/dweet_led.py) in this repository.

0 commit comments

Comments
 (0)