flake8 fiddling
authorSandy Macdonald <sandyjmacdonald@gmail.com>
Fri, 14 Jun 2019 12:26:56 +0000 (13:26 +0100)
committerSandy Macdonald <sandyjmacdonald@gmail.com>
Fri, 14 Jun 2019 12:26:56 +0000 (13:26 +0100)
examples/all-in-one.py
examples/compensated-temperature.py
examples/luftdaten.py

index cf34f89ce1d971c87e15a0b1118fbf0844775891..413b0999be3c6cdc59e70ffdabb3f875972c8c19 100755 (executable)
@@ -54,12 +54,14 @@ message = ""
 # The position of the top bar
 top_pos = 25
 
+
 # Displays data and text on the 0.96" LCD
 def display_text(variable, data, unit):
     # Maintain length of list
     values[variable] = values[variable][1:] + [data]
     # Scale the values for the variable between 0 and 1
-    colours = [(v - min(values[variable]) + 1) / (max(values[variable]) - min(values[variable]) + 1) for v in values[variable]]
+    colours = [(v - min(values[variable]) + 1) / (max(values[variable])
+               - min(values[variable]) + 1) for v in values[variable]]
     # Format the variable name and value
     message = "{}: {:.1f} {}".format(variable[:4], data, unit)
     print(message)
@@ -67,22 +69,26 @@ def display_text(variable, data, unit):
     for i in range(len(colours)):
         # Convert the values to colours from red to blue
         colour = (1.0 - colours[i]) * 0.6
-        r, g, b = [int(x * 255.0) for x in colorsys.hsv_to_rgb(colour, 1.0, 1.0)]
+        r, g, b = [int(x * 255.0) for x in colorsys.hsv_to_rgb(colour,
+                   1.0, 1.0)]
         # Draw a 1-pixel wide rectangle of colour
         draw.rectangle((i, top_pos, i+1, HEIGHT), (r, g, b))
         # Draw a line graph in black
-        line_y = HEIGHT - (top_pos + (colours[i] * (HEIGHT - top_pos))) + top_pos
+        line_y = HEIGHT - (top_pos + (colours[i] * (HEIGHT - top_pos)))\
+                 + top_pos
         draw.rectangle((i, line_y, i+1, line_y+1), (0, 0, 0))
     # Write the text at the top in black
     draw.text((0, 0), message, font=font, fill=(0, 0, 0))
     st7735.display(img)
 
+
 # Get the temperature of the CPU for compensation
 def get_cpu_temperature():
     process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
     output, _error = process.communicate()
     return float(output[output.index('=') + 1:output.rindex("'")])
 
+
 # Tuning factor for compensation. Decrease this number to adjust the
 # temperature down, and increase to adjust up
 factor = 0.8
index b572ad5127eddd247a858cfb805db4d996cae076..b587999d84fc0153cb1152a24e65c69aef7412fd 100755 (executable)
@@ -9,9 +9,10 @@ try:
 except ImportError:
     from smbus import SMBus
 
-print("""compensated-temperature.py - Use the CPU temperature to compensate temperature
-readings from the BME280 sensor. Method adapted from Initial State's Enviro pHAT
-review: https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441
+print("""compensated-temperature.py - Use the CPU temperature
+to compensate temperature readings from the BME280 sensor.
+Method adapted from Initial State's Enviro pHAT review:
+https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441
 
 Press Ctrl+C to exit!
 
@@ -20,12 +21,14 @@ Press Ctrl+C to exit!
 bus = SMBus(1)
 bme280 = BME280(i2c_dev=bus)
 
+
 # Get the temperature of the CPU for compensation
 def get_cpu_temperature():
     process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
     output, _error = process.communicate()
     return float(output[output.index('=') + 1:output.rindex("'")])
 
+
 # Tuning factor for compensation. Decrease this number to adjust the
 # temperature down, and increase to adjust up
 factor = 0.8
index d22be88f822a5db6ffdf0292d37d2b558c39cf77..b7f0565e21f2e0ea48edbc97954ae381c01b61f2 100755 (executable)
@@ -1,7 +1,5 @@
 #!/usr/bin/env python
 
-import time
-import json
 import requests
 import ST7735
 from bme280 import BME280
@@ -14,12 +12,15 @@ try:
 except ImportError:
     from smbus import SMBus
 
-print("""luftdaten.py - Reads temperature, pressure, humidity, PM2.5, and PM10 from
-Enviro plus and sends data to Luftdaten, the citizen science air quality project.
+print("""luftdaten.py - Reads temperature, pressure, humidity,
+PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
+the citizen science air quality project.
 
-Note: you'll need to register with Luftdaten at: https://meine.luftdaten.info/ and
-enter your Raspberry Pi serial number that's displayed on the Enviro plus LCD
-along with the other details before the data appears on the Luftdaten map.
+Note: you'll need to register with Luftdaten at:
+https://meine.luftdaten.info/ and enter your Raspberry Pi
+serial number that's displayed on the Enviro plus LCD along
+with the other details before the data appears on the
+Luftdaten map.
 
 Press Ctrl+C to exit!
 
@@ -30,9 +31,6 @@ bus = SMBus(1)
 # Create BME280 instance
 bme280 = BME280(i2c_dev=bus)
 
-# Create PMS5003 instance
-pms5003 = PMS5003()
-
 # Create LCD instance
 disp = ST7735.ST7735(
     port=0,
@@ -46,6 +44,10 @@ disp = ST7735.ST7735(
 # Initialize display
 disp.begin()
 
+# Create PMS5003 instance
+pms5003 = PMS5003()
+
+
 # Read values from BME280 and PMS5003 and return as dict
 def read_values():
     values = {}
@@ -66,18 +68,21 @@ def read_values():
         values["P1"] = str(pm_values.pm_ug_per_m3(10))
     return values
 
+
 # Get CPU temperature to use for compensation
 def get_cpu_temperature():
     process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
     output, _error = process.communicate()
     return float(output[output.index('=') + 1:output.rindex("'")])
 
+
 # Get Raspberry Pi serial number to use as ID
 def get_serial_number():
-    with open('/proc/cpuinfo','r') as f:
+    with open('/proc/cpuinfo', 'r') as f:
         for line in f:
-            if line[0:6]=='Serial':
-                return(line.split(":")[1].strip())
+            if line[0:6] == 'Serial':
+                return line.split(":")[1].strip()
+
 
 # Check for Wi-Fi connection
 def check_wifi():
@@ -86,6 +91,7 @@ def check_wifi():
     else:
         return False
 
+
 # Display Raspberry Pi serial and Wi-Fi status on LCD
 def display_status():
     wifi_status = "connected" if check_wifi() else "disconnected"
@@ -102,34 +108,37 @@ def display_status():
     draw.text((x, y), message, font=font, fill=text_colour)
     disp.display(img)
 
+
 def send_to_luftdaten(values, id):
     pm_values = dict(i for i in values.items() if i[0].startswith("P"))
     temp_values = dict(i for i in values.items() if not i[0].startswith("P"))
 
     resp_1 = requests.post("https://api.luftdaten.info/v1/push-sensor-data/",
-        json={
-            "software_version": "enviro-plus 0.0.1",
-            "sensordatavalues": [{"value_type": key, "value": val} for key, val in pm_values.items()]
-        },
-        headers={
-            "X-PIN":    "1",
-            "X-Sensor": id,
-            "Content-Type": "application/json",
-            "cache-control": "no-cache"
-        }
+             json={
+                 "software_version": "enviro-plus 0.0.1",
+                 "sensordatavalues": [{"value_type": key, "value": val} for
+                                      key, val in pm_values.items()]
+             },
+             headers={
+                 "X-PIN":    "1",
+                 "X-Sensor": id,
+                 "Content-Type": "application/json",
+                 "cache-control": "no-cache"
+             }
     )
 
     resp_2 = requests.post("https://api.luftdaten.info/v1/push-sensor-data/",
-        json={
-            "software_version": "enviro-plus 0.0.1",
-            "sensordatavalues": [{"value_type": key, "value": val} for key, val in temp_values.items()]
-        },
-        headers={
-            "X-PIN":    "11",
-            "X-Sensor": id,
-            "Content-Type": "application/json",
-            "cache-control": "no-cache"
-        }
+             json={
+                 "software_version": "enviro-plus 0.0.1",
+                 "sensordatavalues": [{"value_type": key, "value": val} for
+                                      key, val in temp_values.items()]
+             },
+             headers={
+                 "X-PIN":    "11",
+                 "X-Sensor": id,
+                 "Content-Type": "application/json",
+                 "cache-control": "no-cache"
+             }
     )
 
     if resp_1.ok and resp_2.ok:
@@ -137,6 +146,7 @@ def send_to_luftdaten(values, id):
     else:
         return False
 
+
 # Compensation factor for temperature
 comp_factor = 1.2