Skip to content

Commit

Permalink
Add support for sensor mh-z19. #247
Browse files Browse the repository at this point in the history
  • Loading branch information
theyosh committed Feb 2, 2019
1 parent 2f8a18f commit 4048fef
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
24 changes: 20 additions & 4 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ dpkg-reconfigure tzdata
# Enable 1Wire en I2C during boot
if [ -f /boot/config.txt ]; then

if [ `grep -ic "#dtparam=i2c_arm=on" /boot/config.txt` -eq 1 ]; then
sed -i.bak 's/^#dtparam=i2c_arm=on/dtparam=i2c_arm=on/' /boot/config.txt
fi
# Enable I2C
if [ `grep -ic "dtparam=i2c_arm=on" /boot/config.txt` -eq 0 ]; then
echo "dtparam=i2c_arm=on" >> /boot/config.txt
fi

# Enable 1-Wire
if [ `grep -ic "dtoverlay=w1-gpio" /boot/config.txt` -eq 0 ]; then
echo "dtoverlay=w1-gpio" >> /boot/config.txt
fi
Expand All @@ -90,6 +89,17 @@ if [ -f /boot/config.txt ]; then
echo "gpu_mem=128" >> /boot/config.txt
fi

# Enable serial
if [ `grep -ic "enable_uart=1" /boot/config.txt` -eq 0 ]; then
echo "enable_uart=1" >> /boot/config.txt
fi

# Make TerrariumPI start during boot
if [ `grep -ic "start.sh" /etc/rc.local` -eq 0 ]; then
sed -i.bak "s@^exit 0@# Starting TerrariumPI server\n${BASEDIR}/start.sh\n\nexit 0@" /etc/rc.local
fi


fi

# Create needed groups
Expand Down Expand Up @@ -136,6 +146,12 @@ fi

# Make sure pigpiod is started at boot, and that user PI can restart it with sudo command
echo "${SCRIPT_USER} ALL=(ALL) NOPASSWD: /usr/sbin/service pigpiod restart" > /etc/sudoers.d/terrariumpi
# https://github.com/UedaTakeyuki/mh-z19/blob/master/pypi/mh_z19/__init__.py#L18
echo "${SCRIPT_USER} ALL=(ALL) NOPASSWD: /bin/systemctl stop [email protected]" >> /etc/sudoers.d/terrariumpi
echo "${SCRIPT_USER} ALL=(ALL) NOPASSWD: /bin/systemctl start [email protected]" >> /etc/sudoers.d/terrariumpi
echo "${SCRIPT_USER} ALL=(ALL) NOPASSWD: /bin/systemctl stop [email protected]" >> /etc/sudoers.d/terrariumpi
echo "${SCRIPT_USER} ALL=(ALL) NOPASSWD: /bin/systemctl start [email protected]" >> /etc/sudoers.d/terrariumpi

systemctl enable pigpiod

# Remove unneeded OWS services
Expand Down Expand Up @@ -174,7 +190,7 @@ EOF
git submodule update > /dev/null
cd "${BASEDIR}/.."

PIP_MODULES="python-dateutil rpi.gpio psutil picamera pigpio requests gpiozero gevent untangle uptime bottle bottle_websocket pylibftdi pyalsaaudio pyserial python-twitter python-pushover requests[socks] Adafruit_DHT Adafruit_SHT31 luma.oled bluepy pywemo pyownet emails"
PIP_MODULES="python-dateutil rpi.gpio psutil picamera pigpio requests gpiozero gevent untangle uptime bottle bottle_websocket pylibftdi pyalsaaudio pyserial python-twitter python-pushover requests[socks] Adafruit_DHT Adafruit_SHT31 luma.oled bluepy pywemo pyownet emails mh-z19"
if [ $PYTHON -eq 3 ]; then
PIP_MODULES="${PIP_MODULES} opencv-python-headless"
fi
Expand Down
1 change: 0 additions & 1 deletion terrariumI2CSensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import smbus
import sys
#import time
import Adafruit_SHT31

from terrariumSensor import terrariumSensorSource
Expand Down
25 changes: 22 additions & 3 deletions terrariumSensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,25 @@ def scan_sensors(callback=None):
except Exception as ex:
logger.warning('OWFS file system is not actve / installed on this device! If this is not correct, try \'i2cdetect -y 1\' to see if device is connected.')

class terrariumMHZ19Sensor(terrariumSensorSource):
TYPE = 'mh-z19'
VALID_SENSOR_TYPES = ['co2']

def set_address(self,address):
# Address is not needed according to source....
self.sensor_address = 'N/A'

def load_data(self):
data = None
if self.get_address() is not None:
data = mh_z19.read()

if data is None:
return None
else:
data = data['co2']

return { self.get_sensor_type() : data}

from terrariumAnalogSensor import terrariumSKUSEN0161Sensor
from terrariumBluetoothSensor import terrariumMiFloraSensor
Expand Down Expand Up @@ -444,7 +463,8 @@ class terrariumSensor(object):
terrariumBME280Sensor,
terrariumVEML6075Sensor,
terrariumChirpSensor,
terrariumSHT3XSensor}
terrariumSHT3XSensor,
terrariumMHZ19Sensor}

def __new__(self, sensor_id, hardware_type, sensor_type, address, name = '', callback_indicator = None):
for sensor in terrariumSensor.SENSORS:
Expand All @@ -469,8 +489,7 @@ def valid_sensor_types():
for sensor_type in sensor_types:
data[sensor_type] = sensor_type

# CO2 and volume is only through remote
data['co2'] = 'co2'
# Volume is only through remote
data['volume'] = 'volume'
return data

Expand Down
1 change: 1 addition & 0 deletions views/sensor_settings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<option value="sku-sen0161">{{_('pH SKU-SEN0161')}}</option>
<option value="veml6075">{{_('VEML6075')}}</option>
<option value="miflora">{{_('MiFlora')}}</option>
<option value="mh-z19">{{_('mh-z19')}}</option>
</select>
</div>
</div>
Expand Down

0 comments on commit 4048fef

Please sign in to comment.