Skip to content

Commit

Permalink
Merge pull request #1 from robervalwalsh/develop
Browse files Browse the repository at this point in the history
Several improvements
  • Loading branch information
robervalwalsh authored Sep 7, 2020
2 parents ad20e17 + 0efdbed commit 7e42bdf
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Other tools/files:

Usage:

- `python3 sensor_monitor.py --<sensors> --dir <directory>` for continuous read-out, results are saved to <directory>
- `python3 sensor_monitor.py --<sensors> --dir <directory>` for continuous read-out, results are saved to `<directory>`
- `python3 sensor_monitor_gui.py` contains a GUI
- `python3 server.py <file>` reports the current measurement status to a TCP client

Expand All @@ -44,3 +44,24 @@ Usage:
Still under development

- You can compile `monitor.cc` for a continuous measurement of some sensors


# System Services

Automatically start `sensor_monitor.py` and `server.py` at boot or when the processes crash

The files `sensor_monitor.service` and `sensor_server.service` must be copied to `/etc/systemd/system`

Enable the services (services start during boot)

- `systemctl enable sensor_monitor`
- `systemctl enable sensor_server`

Start the services
- `systemctl start sensor_monitor`
- `systemctl start sensor_server`

Stop the services
- `systemctl stop sensor_monitor`
- `systemctl stop sensor_server`

4 changes: 2 additions & 2 deletions bme680.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ def __init__(self, i2c_bus_number, i2c_address):
self.i2c_bus_number = i2c_bus_number
self.i2c_bus = smbus.SMBus(self.i2c_bus_number)

self.sensor = bme680.BME680()
self.sensor = bme680.BME680(i2c_addr=self.i2c_address,i2c_device=self.i2c_bus)
self.sensor.set_humidity_oversample(bme680.OS_2X)
self.sensor.set_pressure_oversample(bme680.OS_4X)
self.sensor.set_temperature_oversample(bme680.OS_8X)
self.sensor.set_filter(bme680.FILTER_SIZE_3)
self.sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)

def read(self):
if bme680.BME680().get_sensor_data():
if self.sensor.get_sensor_data():
return BME680Result(self.get_sensor_name(), True, self.sensor.data.temperature, self.sensor.data.humidity, self.sensor.data.pressure)

def get_sensor_type_name(self):
Expand Down
36 changes: 29 additions & 7 deletions sensor_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,19 @@ def load_sensors ( self, sensors ) :
for sensor_name, sensor_opts in sensors :
sensor_class = self.KNOWN_SENSORS[sensor_name][0]
if sensor_opts is None :
loaded_sensors.extend ( sensor_class.detect_sensors ( ) )
try:
loaded_sensors.extend ( sensor_class.detect_sensors ( ) )
except:
print(f'\033[93mWarning: The sensor {sensor_name} does not exist!\033[0m')
else :
loaded_sensors.append ( sensor_class ( *sensor_opts ) )
try:
loaded_sensors.append ( sensor_class ( *sensor_opts ) )
except:
print(f'\033[93mWarning: The sensor {sensor_name}_i2c-{sensor_opts[0]}_{hex(sensor_opts[1])} does not exist!\033[0m')
if len(loaded_sensors) == 0:
print('\033[91mError: No sensor available!\033[0m')
sys.exit()

return loaded_sensors

def add_sensor ( self, sensor ) :
Expand Down Expand Up @@ -287,8 +297,17 @@ def _ring_alarm ( self, field_name, over_limit_values ) :
parser.add_argument ( "--sht75", action = "store_true", help = "Enable SHT75 sensors and try to auto-detect them." )
parser.add_argument ( "--bme680", action = "store_true", help = "Enable BME680 sensors and try to auto-detect them." )
parser.add_argument ( "--dust", action = "store_true", help = "Enable dust sensors and try to auto-detect them." )
parser.add_argument ( "--i2c-addr", type = str, default = '0x77', help = "I2C address. Default: '0x77'" )
parser.add_argument ( "--i2c-bus", type = int, default = 1, help = "I2C bus. Default: 1" )
args = parser.parse_args ( )

multiple_sensors = not (args.w1 ^ args.dht11 ^ args.sht21 ^ args.bme280 ^ args.sht75 ^ args.bme680 ^ args.dust )

if multiple_sensors:
print('You enabled multiple sensors, which is not allowed')
print('Exiting!')
sys.exit()

sensors = list ( )
if args.w1 :
sensors.append ( ( "W1Temp", None ) )
Expand All @@ -301,21 +320,24 @@ def _ring_alarm ( self, field_name, over_limit_values ) :
if args.sht75 :
sensors.append ( ( "SHT75", None ) )
if args.bme680 :
sensors.append ( ( "BME680", None ) )
sensors.append ( ( "BME680", (args.i2c_bus,int(args.i2c_addr,16)) ) )
if args.dust :
sensors.append ( ( "DUST", None ) )

if not args.dir is None :
readings_path = join ( args.dir, "readings.txt" )
readings_log_path = join ( args.dir, "readings_log.txt" )
else :
readings_path = None
readings_log_path = None
default_path = "/opt/measurements"
sensor_name = sensors[0][0]+'_i2c-'+str(args.i2c_bus)+'_'+args.i2c_addr
readings_path = join ( default_path, sensor_name+"_readings.txt" )
readings_log_path = join ( default_path, sensor_name+"_readings_log.txt" )

if not args.config is None :
monitor = SensorMonitor ( sensors, readings_path, readings_log_path, options_path=args.config, alarm_number = args.num_alarm )
else :
monitor = SensorMonitor ( sensors, readings_path, readings_log_path, alarm_number = args.num_alarm )

if not args.alarm_temp is None :
monitor.set_alarm_limits ( "temp", args.alarm_temp[0], args.alarm_temp[1] )
if not args.alarm_hum is None :
Expand All @@ -335,5 +357,5 @@ def _ring_alarm ( self, field_name, over_limit_values ) :
while True :
readings = monitor.get_readings ( )
line = monitor.save_readings ( datetime.datetime.now ( ), readings )
print ( line )
# print ( line )
time.sleep ( args.interval )
15 changes: 15 additions & 0 deletions sensor_monitor.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Sensor Monitor

[Service]
Type=simple
User=pi
WorkingDirectory=/opt/fhlthermorasp
#ExecStart=/opt/fhlthermorasp/sensor_monitor.sh
ExecStart=/usr/bin/nohup /usr/bin/python3 /opt/fhlthermorasp/sensor_monitor.py --dir /opt/measurements --bme680
# Other Restart options: or always, on-abort, etc
Restart=always
RestartSec=5

[Install]
WantedBy=default.target
15 changes: 15 additions & 0 deletions sensor_server.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Sensor Monitor Server
After=sensor_monitor.service

[Service]
Type=simple
User=pi
WorkingDirectory=/opt/fhlthermorasp
ExecStart=/usr/bin/nohup /usr/bin/python3 /opt/fhlthermorasp/server.py /opt/measurements/readings.txt
# Other Restart options: or always, on-abort, etc
Restart=always
RestartSec=5

[Install]
WantedBy=default.target

0 comments on commit 7e42bdf

Please sign in to comment.