#!/usr/bin/env python3 """ main.py - 오케스트레이션 파일 measure.py와 웹서버를 함께 실행 """ from flask import Flask, jsonify, send_file import threading import time # measure.py에서 필요한 것들 import from measure import init_hx711, perform_calibration, get_weight app = Flask(__name__) # 최신 측정 데이터 저장 latest_data = { "volume_ml": 0, "weight_avg": 0, "w1": 0, "w2": 0, "w3": 0, "time": 0 } start_time = 0 @app.route('/') def index(): """웹 페이지 서빙""" return send_file('web.html') @app.route('/data') def data(): """측정 데이터 JSON 반환""" return jsonify(latest_data) def measure_loop(): """측정 루프 (백그라운드 스레드)""" global latest_data, start_time print("측정 시작...") start_time = time.time() while True: try: weight_avg, volume_ml, w1, w2, w3 = get_weight() elapsed = time.time() - start_time latest_data = { "volume_ml": volume_ml, "weight_avg": weight_avg, "w1": w1, "w2": w2, "w3": w3, "time": elapsed } # 콘솔에도 출력 print(f"Volume: {volume_ml:.1f}mL | W1:{w1:.1f} W2:{w2:.1f} W3:{w3:.1f}") except Exception as e: print(f"측정 오류: {e}") time.sleep(0.1) def main(): print("=" * 50) print(" Load Cell Web Monitor") print("=" * 50) # 1. HX711 초기화 if not init_hx711(): print("HX711 초기화 실패!") return # 2. 캘리브레이션 perform_calibration() # 3. 측정 스레드 시작 measure_thread = threading.Thread(target=measure_loop, daemon=True) measure_thread.start() # 4. 웹서버 시작 print("\n" + "=" * 50) print(" 웹서버 시작: http://localhost:5000") print(" Ctrl+C로 종료") print("=" * 50 + "\n") app.run(host='0.0.0.0', port=5000, debug=False) if __name__ == "__main__": main()