Skip to content

Instantly share code, notes, and snippets.

@makvoid
Created February 27, 2022 02:16
Show Gist options
  • Save makvoid/b4b2479d74ffe6f88380a8350c083e74 to your computer and use it in GitHub Desktop.
Save makvoid/b4b2479d74ffe6f88380a8350c083e74 to your computer and use it in GitHub Desktop.
AHT20 collect, export, cli methods
@click.group()
def cli():
pass
@cli.command()
@click.option('--chart-path', required=True, help='Path to store chart at')
@click.option('--location', required=True, help='Sensor location name')
def export(chart_path, location):
# Ensure the entries.json file is not missing
if not os.path.isfile('entries.json'):
print('Error: entries.json file is missing, please run the collect command first.')
sys.exit(1)
# Load entries from JSON file and convert to DataFrames
data = get_entries(location)
# Create the figure and both y-axes
fig, ax1 = plt.subplots(figsize=(10, 8))
ax2 = ax1.twinx()
# Plot the data on two separate y-axes
plot_data(data['temperature'], ax1, 'Date', 'Temperature (F)', 'red')
plot_data(data['humidity'], ax2, 'Date', 'Humidity %', 'blue', 0.33)
# Show the grid
plt.grid()
# Set the date and label formatter for the x-axis
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
fig.autofmt_xdate()
# Save the chart
plt.savefig(chart_path)
print('Chart saved to:', chart_path)
@cli.command()
@click.option('--location', required=True, help='Sensor location name')
def collect(location):
# Collect data and convert/round
temperature = round_num(c_to_f(sensor.temperature))
humidity = round_num(sensor.relative_humidity)
# Save entry
try:
save_entry(location, temperature, humidity)
except:
# Print error traceback
print(traceback.format_exc())
sys.exit(1)
print('Entry saved:', temperature, 'F,', humidity, '% H')
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment