Software source reference
"""
Creates a small app to read the current sensor measurements from the weather station.
The app can, for example, be executed within a remote Desktop session.
Authors:
Min Tai Yap
"""
import time
import smbus2
import board
import busio
import os
import tkinter as tk
import asyncio
from tkinter import messagebox
from adafruit_blinka.microcontroller.bcm283x.pin import Pin
# Functions
from wind_bearer_software.utils.io_utils import read_parameter_dict
# Sensors
from wind_bearer_software.sensor_func.ads1115_voltage_V2 import bat_volts
from wind_bearer_software.sensor_func.as5600 import as5600_read
from wind_bearer_software.sensor_func.sunlight_detect import sunlight_read
from wind_bearer_software.sensor_func.sht45 import sht45_read, init_sht45
from wind_bearer_software.sensor_func.bmp388 import bmp388_init, bmp388_read
[docs]
async def async_button_click_event():
"""
Reads all sensor measurements when the button is clicked.
"""
try:
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
try:
voltage = await bat_volts(i2c)
except:
pass
try:
raw_degrees = await as5600_read(bus)
actual_degrees = raw_degrees + cali_degrees
# Calibrating to actual wind direction
if actual_degrees <= 360 and actual_degrees >= 0:
angle_degrees = actual_degrees
elif actual_degrees > 360:
angle_degrees = actual_degrees - 360
elif actual_degrees < 0:
angle_degrees = actual_degrees + 360
except:
pass
try:
sht = await init_sht45(i2c)
sht_temp, sht_rh = await sht45_read(sht)
except:
pass
try:
SI1151_visible, SI1151_uv, SI1151_ir = await sunlight_read()
except:
pass
try:
bmp = await bmp388_init(i2c, bmp_slp)
_, bmp_pressure, bmp_altitude = await bmp388_read(bmp)
except:
pass
# Delay
time.sleep(0.1)
# Message
message = (
f"Weather Station: {ws_name}\n"
f"Time: {timestamp}\n"
f"Battery Voltage: {voltage:.2f} V\n"
f"raw wind direction: {raw_degrees:.2f} degrees\n"
f"Wind direction: {angle_degrees:.2f} degrees\n"
f"(0 is North, 90 is East, 180 is South and 270 is West)\n"
f"Temperature: {sht_temp:.2f}\n"
f"Relative humidity: {sht_rh:.2f}\n"
f"Visible light: {SI1151_visible}\n"
f"IR: {SI1151_ir}\n"
f"UV: {SI1151_uv}\n"
f"Pressure: {bmp_pressure:.2f}\n"
f"Altitude: {bmp_altitude:.2f}"
)
# Display the results in a message box
messagebox.showinfo("Battery Status", message)
except Exception as e:
messagebox.showerror("Error", f"Failed to read battery voltage:\n{e}")
if __name__ == "__main__":
# Get file paths and parameters
file_paths_dict = read_parameter_dict('user_path.json')
param_dict = read_parameter_dict('parameters.json')
bmp_slp = param_dict["sea_level_pressure"]
# GPIO pins and parameters
ws_name = param_dict["ws_name"]
south_deg = float(param_dict["south_deg"])
# Wind calibration factor
# cali_degrees = south_deg - 180
if south_deg > 180:
cali_degrees = south_deg - 180
elif south_deg < 180:
cali_degrees = 180 - south_deg
else: # when south_deg = 0
cali_degrees = south_deg
# configure pins
bus = smbus2.SMBus(1) # Adjust the bus number according to setup
# Initialize I2C
i2c = busio.I2C(scl=Pin(3), sda=Pin(2))
# Create the GUI application
root = tk.Tk()
root.title("Battery Voltage Monitor")
root.geometry("300x200")
# Add a label
label = tk.Label(root, text="Press the button to check Windbearer's parameters.", font=("Arial", 12), wraplength=280)
label.pack(pady=20)
# Add a button
button = tk.Button(root, text="Check parameters", font=("Arial", 14), command=on_button_click)
button.pack(pady=20)
# Run the GUI event loop
root.mainloop()
