Untitled

mail@pastecode.io avatar
unknown
python
a month ago
1.5 kB
2
Indexable
Never
import logging
from config import *
from arduino_reader import ArduinoReader
from database import DatabaseConnection
from data_processing import DataProcessor
from datetime import datetime
import time

def main():
    logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT)
    logging.info("Starting main application")

    db = DatabaseConnection(DB_HOST, DB_USER, DB_PASSWORD, DB_PORT, DB_NAME)
    db.clear_database()  # Clear the database if DB_CLEAR is set to True, otherwise use.
    db.create_tables()

    now = datetime.now()
    session_id = db.insert_session(now)

    arduino = ArduinoReader(SERIAL_PORT, SERIAL_BAUDRATE, SERIAL_TIMEOUT)
    data_processor = DataProcessor(AVERAGING_INTERVAL_SECONDS)

    try:
        while True:
            raw_data = arduino.read_sensor_data()
            if raw_data:
                data_processor.add_measurement(raw_data)

                processed_data = data_processor.process_data()
                if processed_data:
                    # Assuming processed_data is a dictionary with the correct keys
                    db.insert_sensor_data(session_id, (processed_data['measure_time'], processed_data['sensor'], processed_data['value'], processed_data['volts'], processed_data['temperature']))

            # time.sleep(1)  # Adjust as needed based on your sensor reading frequency

    except KeyboardInterrupt:
        logging.info("Application stopped by user")

if __name__ == '__main__':
    main()
Leave a Comment