108 lines
2.9 KiB
Python
108 lines
2.9 KiB
Python
import time
|
|
import ntptime
|
|
import json
|
|
import os
|
|
from hx710b import HX710B # Correct driver
|
|
from config import *
|
|
|
|
CALIB_FILE = "calibration.cfg"
|
|
|
|
tz_offset = 0
|
|
utc = time.localtime()
|
|
local = time.localtime(time.mktime(utc) + tz_offset * 3600)
|
|
ctime = f"{local[0]:02}-{local[1]:02}-{local[2]:02} {local[3]:02}:{local[4]:02}:{local[5]:02}"
|
|
#print("Time =", local)
|
|
print("Time =", ctime)
|
|
|
|
# ---------------------------------------------------------
|
|
# USER INPUT
|
|
# ---------------------------------------------------------
|
|
# Water depth (meters)
|
|
# KNOWN_DEPTH_M = 1.680
|
|
|
|
# Pressure created by water column (kPa)
|
|
KNOWN_PRESSURE_KPA = KNOWN_DEPTH_M * 9.81
|
|
|
|
print(
|
|
"\nUsing depth {:.3f} m → expected pressure {:.3f} kPa".format(
|
|
KNOWN_DEPTH_M, KNOWN_PRESSURE_KPA
|
|
)
|
|
)
|
|
|
|
# ---------------------------------------------------------
|
|
# SAVE CALIBRATION
|
|
# ---------------------------------------------------------
|
|
def save_calibration(ctime, zero_offset, raw_at_pressure, scale_factor):
|
|
data = {
|
|
"zero_offset": zero_offset,
|
|
"raw_at_pressure": raw_at_pressure,
|
|
"scale_factor": scale_factor,
|
|
"Date_Time": ctime
|
|
}
|
|
|
|
with open(CALIB_FILE, "w") as f:
|
|
json.dump(data, f)
|
|
|
|
print("\n✔ Saved calibration:")
|
|
print(" Date_Time =", ctime)
|
|
print(" zero_offset =", zero_offset)
|
|
print(" raw_at_pressure =", raw_at_pressure)
|
|
print(" scale_factor =", scale_factor)
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# RAW AVERAGING
|
|
# ---------------------------------------------------------
|
|
def read_average(hx, samples=30):
|
|
total = 0
|
|
for _ in range(samples):
|
|
total += hx.read_raw()
|
|
time.sleep_ms(5)
|
|
return total / samples
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# CALIBRATION PROCESS
|
|
# ---------------------------------------------------------
|
|
print("\n=== HX710B Depth-Based Calibration ===")
|
|
|
|
hx = HX710B(dout_pin=0, sck_pin=1)
|
|
|
|
# Step 1 — ZERO PRESSURE
|
|
print("\n➡ Ensure tank is OPEN TO AIR (0 kPa)...")
|
|
time.sleep(3)
|
|
|
|
zero_offset = read_average(hx)
|
|
print("Zero offset captured:", zero_offset)
|
|
|
|
# Step 2 — APPLY KNOWN PRESSURE
|
|
print(
|
|
"\n➡ Now apply EXACTLY {:.3f} kPa (depth {:.3f} m)...".format(
|
|
KNOWN_PRESSURE_KPA, KNOWN_DEPTH_M
|
|
)
|
|
)
|
|
time.sleep(10)
|
|
|
|
raw_at_pressure = read_average(hx)
|
|
print("Raw at known depth:", raw_at_pressure)
|
|
|
|
# Compute scale factor
|
|
scale_factor = KNOWN_PRESSURE_KPA / (raw_at_pressure - zero_offset)
|
|
|
|
# Save calibration (INCLUDING raw value)
|
|
save_calibration(ctime, zero_offset, raw_at_pressure, scale_factor)
|
|
|
|
print("\n=== Calibration Complete ===")
|
|
|
|
# ---------------------------------------------------------
|
|
# VERIFY
|
|
# ---------------------------------------------------------
|
|
hx.zero_offset = zero_offset
|
|
hx.scale_factor = scale_factor
|
|
|
|
test = hx.read_kpa()
|
|
|
|
print("\nVerification reading:", test, "kPa")
|
|
print("Expected:", KNOWN_PRESSURE_KPA, "kPa")
|
|
print("\nDone.\n")
|