Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify log file location #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ Now you can create the sensor object and take measurements:

This will create the SHT1x object using `data_pin=18`, `sck_pin=23`, `gpio_mode=GPIO.BCM`, and default values for `vdd` (3.5V), `resolution` (High), `heater` (False), `otp_no_reload` (False), and `crc_check` (True). The output will look something like this:

Temperature: 24.05*C [75.25*F]
Temperature: 24.05°C [75.25°F]
Humidity: 22.80%
Dew Point: 1.38*C
Dew Point: 1.38°C

> Note that this library should be used with a context manager like the `with` statement. Using it with a context manager will allow the program to properly clean up after itself and reset the GPIO pins back to default states.

Expand All @@ -78,47 +78,47 @@ Running the script exercises all of the functionality for the sensor. Be sure to

$ sudo python3 examples/examples.py
Test: using default values: 3.5V, High resolution, no heater, otp_no_reload off, CRC checking enabled...
Temperature: 24.49*C [76.04*F]
Temperature: 24.49°C [76.04°F]
Humidity: 20.68%
Dew Point: 0.47*C
Dew Point: 0.47°C

Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.68%
Dew Point: 0.46*C
Dew Point: 0.46°C

Temperature: 24.47*C [76.01*F]
Temperature: 24.47°C [76.01°F]
Humidity: 20.68%
Dew Point: 0.45*C
Dew Point: 0.45°C

Temperature: 24.51*C [76.06*F]
Temperature: 24.51°C [76.06°F]
Humidity: 20.68%
Dew Point: 0.47*C
Dew Point: 0.47°C

Temperature: 24.51*C [76.06*F]
Temperature: 24.51°C [76.06°F]
Humidity: 20.68%
Dew Point: 0.47*C
Dew Point: 0.47°C
Test complete.

Test: reading all measurements using GPIO.BCM mode, 3V, High resolution, heater off, otp_no_reload off, and CRC check on.
Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.61%
Dew Point: 0.42*C
Dew Point: 0.42°C

Temperature: 24.46*C [75.98*F]
Temperature: 24.46°C [75.98°F]
Humidity: 20.61%
Dew Point: 0.40*C
Dew Point: 0.40°C

Temperature: 24.46*C [75.98*F]
Temperature: 24.46°C [75.98°F]
Humidity: 20.61%
Dew Point: 0.40*C
Dew Point: 0.40°C

Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.68%
Dew Point: 0.46*C
Dew Point: 0.46°C

Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.65%
Dew Point: 0.44*C
Dew Point: 0.44°C
Test complete.
.
.
Expand All @@ -134,25 +134,25 @@ This script is callable from the terminal and the sensor parameters are passed i
This executes the sensor script using `data_pin=18`, `sck_pin=23`, and `gpio_mode=GPIO.BCM`. The script will then create an instance of the SHT1x class and read in the temperature, humidity, and calculate the dew point five times, sleeping 2 seconds in between each measurement. The output will looks something like this:

$ sudo python3 examples/sensor.py 18 23 -g 'BCM'
Temperature: 24.05*C [75.25*F]
Temperature: 24.05°C [75.25°F]
Humidity: 22.79%
Dew Point: 1.37*C
Dew Point: 1.37°C

Temperature: 24.03*C [75.21*F]
Temperature: 24.03°C [75.21°F]
Humidity: 22.79%
Dew Point: 1.36*C
Dew Point: 1.36°C

Temperature: 24.01*C [75.16*F]
Temperature: 24.01°C [75.16°F]
Humidity: 22.79%
Dew Point: 1.33*C
Dew Point: 1.33°C

Temperature: 24.01*C [75.17*F]
Temperature: 24.01°C [75.17°F]
Humidity: 22.86%
Dew Point: 1.38*C
Dew Point: 1.38°C

Temperature: 24.02*C [75.19*F]
Temperature: 24.02°C [75.19°F]
Humidity: 22.86%
Dew Point: 1.39*C
Dew Point: 1.39°C

To get a listing of all the parameters you can provide to the script, use `python3 sensor.py -h` for help:

Expand Down
64 changes: 32 additions & 32 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ This will create the SHT1x object using ``data_pin=18``, ``sck_pin=23``, ``gpio_

::

Temperature: 24.05*C [75.25*F]
Temperature: 24.05°C [75.25°F]
Humidity: 22.80%
Dew Point: 1.38*C
Dew Point: 1.38°C

Note that this library should be used with a context manager like the ``with`` statement. Using it with a context manager will allow the program to properly clean up after itself and reset the GPIO pins back to default states.

Expand All @@ -105,47 +105,47 @@ Running the script exercises all of the functionality for the sensor. Be sure to

$ sudo python3 examples/examples.py
Test: using default values: 3.5V, High resolution, no heater, otp_no_reload off, CRC checking enabled...
Temperature: 24.49*C [76.04*F]
Temperature: 24.49°C [76.04°F]
Humidity: 20.68%
Dew Point: 0.47*C
Dew Point: 0.47°C

Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.68%
Dew Point: 0.46*C
Dew Point: 0.46°C

Temperature: 24.47*C [76.01*F]
Temperature: 24.47°C [76.01°F]
Humidity: 20.68%
Dew Point: 0.45*C
Dew Point: 0.45°C

Temperature: 24.51*C [76.06*F]
Temperature: 24.51°C [76.06°F]
Humidity: 20.68%
Dew Point: 0.47*C
Dew Point: 0.47°C

Temperature: 24.51*C [76.06*F]
Temperature: 24.51°C [76.06°F]
Humidity: 20.68%
Dew Point: 0.47*C
Dew Point: 0.47°C
Test complete.

Test: reading all measurements using GPIO.BCM mode, 3V, High resolution, heater off, otp_no_reload off, and CRC check on.
Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.61%
Dew Point: 0.42*C
Dew Point: 0.42°C

Temperature: 24.46*C [75.98*F]
Temperature: 24.46°C [75.98°F]
Humidity: 20.61%
Dew Point: 0.40*C
Dew Point: 0.40°C

Temperature: 24.46*C [75.98*F]
Temperature: 24.46°C [75.98°F]
Humidity: 20.61%
Dew Point: 0.40*C
Dew Point: 0.40°C

Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.68%
Dew Point: 0.46*C
Dew Point: 0.46°C

Temperature: 24.48*C [76.02*F]
Temperature: 24.48°C [76.02°F]
Humidity: 20.65%
Dew Point: 0.44*C
Dew Point: 0.44°C
Test complete.
.
.
Expand All @@ -166,25 +166,25 @@ This executes the sensor script using ``data_pin=18``, ``sck_pin=23``, and ``gpi
::

$ sudo python3 examples/sensor.py 18 23 -g 'BCM'
Temperature: 24.05*C [75.25*F]
Temperature: 24.05°C [75.25°F]
Humidity: 22.79%
Dew Point: 1.37*C
Dew Point: 1.37°C

Temperature: 24.03*C [75.21*F]
Temperature: 24.03°C [75.21°F]
Humidity: 22.79%
Dew Point: 1.36*C
Dew Point: 1.36°C

Temperature: 24.01*C [75.16*F]
Temperature: 24.01°C [75.16°F]
Humidity: 22.79%
Dew Point: 1.33*C
Dew Point: 1.33°C

Temperature: 24.01*C [75.17*F]
Temperature: 24.01°C [75.17°F]
Humidity: 22.86%
Dew Point: 1.38*C
Dew Point: 1.38°C

Temperature: 24.02*C [75.19*F]
Temperature: 24.02°C [75.19°F]
Humidity: 22.86%
Dew Point: 1.39*C
Dew Point: 1.39°C

To get a listing of all the parameters you can provide to the script, use `python3 sensor.py -h` for help:

Expand Down
8 changes: 4 additions & 4 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main():
with SHT1x(DATA_PIN, SCK_PIN, gpio_mode=GPIO.BCM) as sensor:
for i in range(5):
sensor.read_humidity()
print('Temperature: {0}*C [{1}*F]\nHumidity: {2}'.format(sensor.temperature_celsius,
print('Temperature: {0}°C [{1}°F]\nHumidity: {2}'.format(sensor.temperature_celsius,
sensor.temperature_fahrenheit, sensor.humidity))
sleep(2)
print('Test complete.\n')
Expand Down Expand Up @@ -103,7 +103,7 @@ def main():
with SHT1x(DATA_PIN, SCK_PIN, gpio_mode=GPIO.BCM) as sensor:
sensor.reset_connection()
sensor.read_temperature()
print('Temperature: {0}*C [{1}*F]'.format(sensor.temperature_celsius, sensor.temperature_fahrenheit))
print('Temperature: {0}°C [{1}°F]'.format(sensor.temperature_celsius, sensor.temperature_fahrenheit))
print('Test complete.\n')

print('Test: performing a soft reset of the sensor...')
Expand All @@ -112,7 +112,7 @@ def main():
status_register = sensor.read_status_register()
print('Status Register: {0:08b}'.format(status_register))
sensor.read_temperature()
print('Temperature: {0}*C [{1}*F]'.format(sensor.temperature_celsius, sensor.temperature_fahrenheit))
print('Temperature: {0}°C [{1}°F]'.format(sensor.temperature_celsius, sensor.temperature_fahrenheit))
print('Test complete.\n')

print('Test: resetting the status register...')
Expand All @@ -121,7 +121,7 @@ def main():
status_register = sensor.read_status_register()
print('Status Register: {0:08b}'.format(status_register))
sensor.read_temperature()
print('Temperature: {0}*C [{1}*F]'.format(sensor.temperature_celsius, sensor.temperature_fahrenheit))
print('Temperature: {0}°C [{1}°F]'.format(sensor.temperature_celsius, sensor.temperature_fahrenheit))
print('Test complete.\n')

print('Test: CRC disabled...')
Expand Down
22 changes: 19 additions & 3 deletions pi_sht1x/logging.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from os import path
from os import getenv, path, mkdir
from logging import getLogger, Formatter, WARNING
from logging.handlers import RotatingFileHandler


LOG_FORMAT = '[%(asctime)s] %(module)12s:%(funcName)12s:%(lineno)-4s %(levelname)-9s %(message)s'
LOG_FOLDER_NAME = '.pi_sht1x'
LOG_FILE_NAME = 'pi_sht1x.log'


Expand All @@ -13,10 +14,11 @@ def create_logger(name):
in case there was a logger with the same name.
"""
logger = getLogger(name)
log_filename = path.join(path.dirname(path.realpath(__file__)), LOG_FILE_NAME)
log_filename = os.path.join(_get_log_folder(), LOG_FILE_NAME)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the import above you can change this to path.join.

log_formatter = Formatter(LOG_FORMAT)

file_handler = RotatingFileHandler(log_filename, mode='a', maxBytes=512000, backupCount=3)
file_handler = RotatingFileHandler(log_filename, mode='a',
maxBytes=512000, backupCount=3)
file_handler.setLevel(WARNING)
file_handler.setFormatter(log_formatter)

Expand All @@ -25,3 +27,17 @@ def create_logger(name):
logger.setLevel(WARNING)

return logger


def _get_log_folder() -> str:
"""
Combine and return realpath of log folder. If the folder does not exist,
then create it.
"""
log_folder = path.join(getenv('HOME'), LOG_FOLDER_NAME)
if path.exists(log_folder):
pass
else:
mkdir(log_folder)

return log_folder
12 changes: 6 additions & 6 deletions pi_sht1x/sht1x.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def read_temperature(self):
self.temperature_fahrenheit = round(raw_temperature * COF.D2_SO_F[self._resolution[0]] +
COF.D1_VDD_F[self.vdd], 2)

self.logger.info('Temperature: {0}*C [{1}*F]'.format(self.temperature_celsius, self.temperature_fahrenheit))
self.logger.info('Temperature: {0}°C [{1}°F]'.format(self.temperature_celsius, self.temperature_fahrenheit))
return self.temperature_celsius

def read_humidity(self, temperature=None):
Expand Down Expand Up @@ -231,7 +231,7 @@ def calculate_dew_point(self, temperature=None, humidity=None):
ew = (m * temperature) / (tn + temperature)
self.dew_point = round(tn * (log_humidity + ew) / m - (log_humidity + ew), 2)

self.logger.info('Dew Point: {0}*C'.format(self.dew_point))
self.logger.info('Dew Point: {0}°C'.format(self.dew_point))
return self.dew_point

def _send_command(self, measurement=True):
Expand Down Expand Up @@ -273,7 +273,7 @@ def _wait_for_result(self):
Raises an exception if the Data Ready signal hasn't been received after 350 milliseconds.
:return: None
"""
GPIO.setup(self.data_pin, GPIO.IN)
GPIO.setup(self.data_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
data_ready = GPIO.HIGH

for i in range(35):
Expand Down Expand Up @@ -313,7 +313,7 @@ def _get_byte(self):
Reads a single byte from the SHT1x sensor.
:return: 8-bit value.
"""
GPIO.setup(self.data_pin, GPIO.IN)
GPIO.setup(self.data_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.sck_pin, GPIO.OUT)

data = 0b00000000
Expand Down Expand Up @@ -384,7 +384,7 @@ def _get_ack(self, command_name):
:param command_name: Command issued to the sensor.
:return: None
"""
GPIO.setup(self.data_pin, GPIO.IN)
GPIO.setup(self.data_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.sck_pin, GPIO.OUT)

self._toggle_pin(self.sck_pin, GPIO.HIGH)
Expand Down Expand Up @@ -557,7 +557,7 @@ def __str__(self):
humidity = self.humidity if self.humidity is not None else '-'
dew_point = self.dew_point if self.dew_point is not None else '-'

return 'Temperature: {0}*C [{1}*F]\nRelative Humidity: {2}%\nDew Point: {3}*C\n'.format(celsius, fahrenheit,
return 'Temperature: {0}°C [{1}°F]\nRelative Humidity: {2}%\nDew Point: {3}°C\n'.format(celsius, fahrenheit,
humidity, dew_point)


Expand Down