Skip to content

Commit bd92c67

Browse files
committed
Merge remote-tracking branch 'origin/BlinkaMigration'
2 parents f5971e8 + b319191 commit bd92c67

14 files changed

+672
-23
lines changed

mudpi.py

100755100644
+15-20
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,21 @@
2121
from action import Action
2222
from config_load import load_config_json
2323
from server.mudpi_server import MudpiServer
24-
from workers.pi.lcd_worker import LcdWorker
25-
from workers.pi.i2c_worker import PiI2CWorker
26-
from workers.pi.relay_worker import RelayWorker
27-
from workers.pi.camera_worker import CameraWorker
28-
from workers.pi.sensor_worker import PiSensorWorker
29-
from workers.pi.control_worker import PiControlWorker
30-
from workers.trigger_worker import TriggerWorker
31-
from workers.sequence_worker import SequenceWorker
32-
24+
# from workers.pi.lcd_worker import LcdWorker
25+
from workers.linux.i2c_worker import PiI2CWorker
26+
from workers.linux.relay_worker import RelayWorker
27+
# from workers.pi.camera_worker import CameraWorker
28+
from workers.linux.sensor_worker import PiSensorWorker
29+
# from workers.pi.control_worker import PiControlWorker
30+
# from workers.trigger_worker import TriggerWorker
31+
# from workers.sequence_worker import SequenceWorker
3332
try:
3433
from workers.arduino.arduino_worker import ArduinoWorker
35-
3634
NANPY_ENABLED = True
3735
except ImportError:
3836
NANPY_ENABLED = False
39-
4037
try:
4138
from workers.adc_worker import ADCMCP3008Worker
42-
4339
MCP_ENABLED = True
4440
except ImportError:
4541
MCP_ENABLED = False
@@ -75,12 +71,12 @@
7571
print(chr(27) + "[2J")
7672
# Print a display logo for startup
7773
print("\033[1;32m")
78-
print(' __ __ _ _____ _ ')
79-
print('| \/ | | | __ (_)')
80-
print('| \ / |_ _ __| | |__) | ')
81-
print('| |\/| | | | |/ _` | ___/ | ')
82-
print('| | | | |_| | (_| | | | | ')
83-
print('|_| |_|\__,_|\__,_|_| |_| ')
74+
print(r' __ __ _ _____ _ ')
75+
print(r'| \/ | | | __ (_)')
76+
print(r'| \ / |_ _ __| | |__) | ')
77+
print(r'| |\/| | | | |/ _` | ___/ | ')
78+
print(r'| | | | |_| | (_| | | | | ')
79+
print(r'|_| |_|\__,_|\__,_|_| |_| ')
8480
print('_________________________________________________')
8581
print('')
8682
print('Eric Davisson @theDavisson')
@@ -420,7 +416,6 @@
420416
t = worker.run()
421417
threads.append(t)
422418
time.sleep(.5)
423-
424419
for node in nodes:
425420
t = node.run()
426421
threads.append(t)
@@ -457,7 +452,7 @@
457452

458453
try:
459454
server.sock.shutdown(socket.SHUT_RDWR)
460-
except:
455+
except Exception:
461456
pass
462457

463458
# Clear main running event to signal threads to close

requirements.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pycron
22
redis
33
picamera
4-
Adafruit_DHT
4+
Adafruit-Blinka
5+
adafruit-circuitpython-dht
56
adafruit-circuitpython-mcp3xxx
6-
adafruit-blinka
77
adafruit-circuitpython-bme680
88
https://github.com/olixr/nanpy/archive/master.zip
9-
git+https://github.com/olixr/Adafruit_CircuitPython_CharLCD.git#egg=adafruit-circuitpython-charlcd
9+
git+https://github.com/olixr/Adafruit_CircuitPython_CharLCD.git#egg=adafruit-circuitpython-charlcd

sensors/linux/__init__.py

Whitespace-only changes.

sensors/linux/float_sensor.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
import json
3+
import redis
4+
from .sensor import Sensor
5+
import digitalio
6+
import board
7+
8+
9+
# PIN MODE : OUT | IN
10+
11+
class FloatSensor(Sensor):
12+
13+
def __init__(self, pin, name=None, key=None, redis_conn=None):
14+
super().__init__(pin, name=name, key=key, redis_conn=redis_conn)
15+
self.pin_obj = getattr(board, pin)
16+
return
17+
18+
def init_sensor(self):
19+
"""Initialize the sensor here (i.e. set pin mode, get addresses, etc) this gets called by the worker"""
20+
self.gpio_pin = digitalio.DigitalInOut(self.pin_obj) # Default to input : https://github.com/adafruit/Adafruit_Blinka/blob/master/src/digitalio.py#L111
21+
return
22+
23+
def read(self):
24+
"""Read the sensor(s), parse the data and store it in redis if redis is configured"""
25+
value = self.gpio_pin.value
26+
return value
27+
28+
def readRaw(self):
29+
"""Read the sensor(s) but return the raw data, useful for debugging"""
30+
return self.read()

sensors/linux/humidity_sensor.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import time
2+
import json
3+
import redis
4+
from .sensor import Sensor
5+
import adafruit_dht
6+
import sys
7+
sys.path.append('..')
8+
9+
10+
from logger.Logger import Logger, LOG_LEVEL
11+
12+
# r = redis.Redis(host='127.0.0.1', port=6379)
13+
# PIN MODE : OUT | IN
14+
15+
16+
class HumiditySensor(Sensor):
17+
18+
def __init__(self, pin, name=None, key=None, model='11', redis_conn=None):
19+
super().__init__(pin, name=name, key=key, redis_conn=redis_conn)
20+
self.type = model
21+
return
22+
23+
def init_sensor(self):
24+
"""Initialize the sensor here (i.e. set pin mode, get addresses, etc) this gets called by the worker"""
25+
sensor_types = {'11': adafruit_dht.DHT11,
26+
'22': adafruit_dht.DHT22,
27+
'2302': adafruit_dht.DHT22} # AM2302 = DHT22
28+
if self.type in sensor_types:
29+
self.sensor = sensor_types[self.type]
30+
else:
31+
Logger.log(LOG_LEVEL["warning"], 'Sensor Model Error: Defaulting to DHT11')
32+
self.sensor = adafruit_dht.DHT11
33+
return
34+
35+
def read(self):
36+
"""Read the sensor(s), parse the data and store it in redis if redis is configured"""
37+
38+
for i in range(15): # read_retry() not implemented in new lib
39+
dhtDevice = self.sensor(self.pin)
40+
try:
41+
temperature_c = dhtDevice.temperature
42+
humidity = dhtDevice.humidity
43+
if humidity is not None and temperature_c is not None:
44+
break
45+
except RuntimeError:
46+
# Errors happen fairly often, DHT's are hard to read, just keep going:
47+
time.sleep(2)
48+
continue
49+
except Exception as error:
50+
self.sensor(self.pin).exit()
51+
Logger.log(LOG_LEVEL["error"], f'DHT Reading was Invalid. Trying again next cycle. Details: {error.args[0]}')
52+
return None
53+
54+
if humidity is not None and temperature_c is not None:
55+
self.r.set(self.key + '_temperature', round(temperature_c * 1.8 + 32, 2))
56+
self.r.set(self.key + '_humidity', humidity)
57+
readings = {'temperature': round(temperature_c * 1.8 + 32, 2), 'humidity': round(humidity, 2)}
58+
self.r.set(self.key, json.dumps(readings))
59+
return readings
60+
else:
61+
Logger.log(LOG_LEVEL["error"], 'DHT Reading was Invalid. Trying again next cycle.')
62+
return None
63+
64+
def readRaw(self):
65+
"""Read the sensor(s) but return the raw data, useful for debugging"""
66+
return self.read()

sensors/linux/i2c/__init__.py

Whitespace-only changes.

sensors/linux/i2c/bme680_sensor.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import time
2+
import json
3+
import redis
4+
from .sensor import Sensor
5+
import board
6+
from busio import I2C
7+
import adafruit_bme680
8+
9+
from logger.Logger import Logger, LOG_LEVEL
10+
11+
import sys
12+
sys.path.append('..')
13+
14+
import variables
15+
16+
17+
class Bme680Sensor(Sensor):
18+
19+
def __init__(self, address=None, name=None, key=None, redis_conn=None):
20+
super().__init__(address, name=name, key=key, redis_conn=redis_conn)
21+
return
22+
23+
def init_sensor(self):
24+
self.sensor = adafruit_bme680.Adafruit_BME680_I2C(self.i2c, debug=False)
25+
# change this to match the location's pressure (hPa) at sea level
26+
self.sensor.sea_level_pressure = 1013.25
27+
return
28+
29+
def read(self):
30+
temperature = round((self.sensor.temperature - 5) * 1.8 + 32, 2)
31+
gas = self.sensor.gas
32+
humidity = round(self.sensor.humidity, 1)
33+
pressure = round(self.sensor.pressure, 2)
34+
altitude = round(self.sensor.altitude, 3)
35+
36+
if humidity is not None and temperature is not None:
37+
self.r.set(self.key + '_temperature', temperature)
38+
self.r.set(self.key + '_humidity', humidity)
39+
self.r.set(self.key + '_gas', gas)
40+
self.r.set(self.key + '_pressure', pressure)
41+
self.r.set(self.key + '_altitude', altitude)
42+
readings = {'temperature': temperature, 'humidity': humidity, 'pressure': pressure, 'gas': gas, 'altitude': altitude}
43+
self.r.set(self.key, json.dumps(readings))
44+
# print('BME680:', readings)
45+
return readings
46+
else:
47+
Logger.log(LOG_LEVEL["error"], 'Failed to get reading [BME680]. Try again!')
48+
49+
def readRaw(self):
50+
"""Read the sensor(s) but return the raw data, useful for debugging"""
51+
return self.read()

sensors/linux/i2c/sensor.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import time
2+
import json
3+
import redis
4+
import board
5+
from busio import I2C
6+
7+
8+
# PIN MODE : OUT | IN
9+
10+
class Sensor():
11+
12+
def __init__(self, address, name=None, key=None, redis_conn=None):
13+
self.address = address
14+
15+
if key is None:
16+
raise Exception('No "key" Found in I2C Sensor Config')
17+
else:
18+
self.key = key.replace(" ", "_").lower()
19+
20+
if name is None:
21+
self.name = self.key.replace("_", " ").title()
22+
else:
23+
self.name = name
24+
25+
self.i2c = I2C(board.SCL, board.SDA)
26+
try:
27+
self.r = redis_conn if redis_conn is not None else redis.Redis(host='127.0.0.1', port=6379)
28+
except KeyError:
29+
self.r = redis.Redis(host='127.0.0.1', port=6379)
30+
return
31+
32+
def init_sensor(self):
33+
"""Initialize the sensor here (i.e. set pin mode, get addresses, etc)"""
34+
# GPIO.setmode(GPIO.BCM)
35+
# GPIO.setup(pin, GPIO.IN)
36+
pass
37+
38+
def read(self):
39+
"""Read the sensor(s), parse the data and store it in redis if redis is configured"""
40+
# GPIO.input(pin)
41+
pass
42+
43+
def readRaw(self):
44+
"""Read the sensor(s) but return the raw data, useful for debugging"""
45+
pass
46+
47+
# self.pin not defined and readPin() doesn't seemto be called. So I commented it
48+
'''
49+
def readPin(self):
50+
"""Read the pin from the board. Can be analog or digital based on \"analog_pin_mode\""""
51+
data = self.gpio.input(self.pin)
52+
return data
53+
'''

sensors/linux/sensor.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import time
2+
import json
3+
import redis
4+
import digitalio
5+
import board
6+
import re
7+
from logger.Logger import Logger, LOG_LEVEL
8+
9+
10+
# PIN MODE : OUT | IN
11+
12+
class Sensor():
13+
14+
def __init__(self, pin, name=None, key=None, redis_conn=None):
15+
self.pin_obj = getattr(board, pin)
16+
17+
if re.match(r'D\d+$', pin):
18+
self.is_digital = True
19+
elif re.match(r'A\d+$', pin):
20+
self.is_digital = False
21+
else:
22+
Logger.log(LOG_LEVEL["error"], "Cannot detect pin type (Digital or analog), should be Dxx or Axx for digital or analog. Please refer to https://github.com/adafruit/Adafruit_Blinka/tree/master/src/adafruit_blinka/board")
23+
24+
if key is None:
25+
raise Exception('No "key" Found in Sensor Config')
26+
else:
27+
self.key = key.replace(" ", "_").lower()
28+
29+
if name is None:
30+
self.name = self.key.replace("_", " ").title()
31+
else:
32+
self.name = name
33+
34+
self.gpio = digitalio
35+
try:
36+
self.r = redis_conn if redis_conn is not None else redis.Redis(host='127.0.0.1', port=6379)
37+
except KeyError:
38+
self.r = redis.Redis(host='127.0.0.1', port=6379)
39+
return
40+
41+
def init_sensor(self):
42+
"""Initialize the sensor here (i.e. set pin mode, get addresses, etc)"""
43+
# GPIO.setmode(GPIO.BCM)
44+
# GPIO.setup(pin, GPIO.IN)
45+
pass
46+
47+
def read(self):
48+
"""Read the sensor(s), parse the data and store it in redis if redis is configured"""
49+
# GPIO.input(pin)
50+
pass
51+
52+
def readRaw(self):
53+
# Read the sensor(s) but return the raw data, useful for debugging
54+
pass
55+
56+
def readPin(self):
57+
"""Read the pin from the board.
58+
59+
pin value must be a blinka Pin. D for a digital input and A for an analog input, followed by the pin number.
60+
You check the board-specific pin mapping [here](https://github.com/adafruit/Adafruit_Blinka/blob/master/src/adafruit_blinka/board/).
61+
62+
Examples:
63+
readPin(board.D12)
64+
readPin(board.A12)
65+
"""
66+
if self.is_digital:
67+
data = self.gpio.DigitalInOut(self.pin_obj).value
68+
else:
69+
data = self.gpio.AnalogIn(self.pin_obj).value
70+
return data

workers/linux/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)