Capturing Data from Sensors Sequentially Using Raspberry Pi 5 and NRF24

I am developing a project on Raspberry Pi 5. For this project, I have 6 sensors and I collect data from these sensors. I use NRF24 when collecting data. I store the data I collect in the database I installed on Raspberry Pi 5. But I have a problem. The data I collect from these sensors comes to me in a scattered way, and since I will use this data in machine learning, it must come in an orderly manner. As you can see, NRF24 receives data from my sensors randomly. Sometimes, while receiving data from one sensor 2-3 times, it does not receive data from the other sensor for 20 seconds. I need to solve this problem and collect data from my sensors respectively (For example, Sensor 1 -> 0.12, Sensor 2 -> 0.07, ….., Sensor 6-> 0.12). For this, I will share with you the code I currently use and ask for your support on what corrections I should make.

app = Flask(__name__)
CORS(app)

# MySQL bağlantısı kurma fonksiyonu
def connect_mysql():
    return pymysql.connect(
        host="localhost",
        user="*****",
        password="***",
        database="***"
    )


def get_data():
    message = (ser.readline()).decode("utf-8").rstrip("rn").split(":")
    print(message)
    ser.reset_input_buffer()
    print(ser.in_waiting)
    return message

def add_data():
    message = get_data()
    # MySQL bağlantısı oluşturma
    con = connect_mysql()
    cursor = con.cursor()
    global sem_val
    global wem_val
    global gem_val
    global bem_val
    global iem_val
    global anm_val
    
    try:
        match message[0]:
        
            case "EOS-SEM":
                cursor.execute("""INSERT INTO SayacOlcumleri (Sayac_ID, Olcum_Deger, OlcumYapilanCihaz_ID) VALUES (%s, %s, %s)""",("1", message[1], "1"))
                sem_val = float(message[1])
        
            case "EOS-WEM":
                cursor.execute("""INSERT INTO SayacOlcumleri (Sayac_ID, Olcum_Deger, OlcumYapilanCihaz_ID) VALUES (%s, %s, %s)""",("2", message[1], "2"))
                wem_val = float(message[1])
    
            case "EOS-GEM":
                cursor.execute("""INSERT INTO SayacOlcumleri (Sayac_ID, Olcum_Deger, OlcumYapilanCihaz_ID) VALUES (%s, %s, %s)""",("3", message[1], "3"))
                gem_val = float(message[1])
    
            case "EOS-BEM":
                cursor.execute("""INSERT INTO SayacOlcumleri (Sayac_ID, Olcum_Deger, OlcumYapilanCihaz_ID) VALUES (%s, %s, %s)""",("4", message[1], "4"))
                bem_val = float(message[1])
    
            case "EOS-IEM":
                cursor.execute("""INSERT INTO SayacOlcumleri (Sayac_ID, Olcum_Deger, OlcumYapilanCihaz_ID) VALUES (%s, %s, %s)""",("5", message[1], "5"))
                iem_val = float(message[1])
    
            case "EOS-ANM":
                cursor.execute("""INSERT INTO SayacOlcumleri (Sayac_ID, Olcum_Deger, OlcumYapilanCihaz_ID) VALUES (%s, %s, %s)""",("6", message[1], "6"))
                anm_val = float(message[1])
    
        con.commit()
        print("Veri başarıyla eklendi.")
    except Exception as e:
        print("Veri eklenirken bir hata oluştu:", str(e))
        con.rollback()
    finally:
        cursor.close()
        con.close()

Let me share with you the version added to the database as an example and help you understand the situation better.

|     3629 |        5 | 2024-07-22 13:07:05 | 0.06        |                    5 |
|     3630 |        5 | 2024-07-22 13:07:05 | 0.06        |                    5 |
|     3631 |        6 | 2024-07-22 13:07:06 | 0.00        |                    6 |
|     3632 |        6 | 2024-07-22 13:07:06 | 0.00        |                    6 |
|     3633 |        5 | 2024-07-22 13:07:07 | 0.06        |                    5 |
|     3634 |        5 | 2024-07-22 13:07:07 | 0.06        |                    5 |
|     3635 |        4 | 2024-07-22 13:07:08 | 0.02        |                    4 |
|     3636 |        5 | 2024-07-22 13:07:08 | 0.06        |                    5 |
|     3637 |        6 | 2024-07-22 13:07:09 | 0.00        |                    6 |
|     3638 |        6 | 2024-07-22 13:07:09 | 0.00        |                    6 |
|     3639 |        1 | 2024-07-22 13:07:10 | 0.12        |                    1 |
|     3640 |        1 | 2024-07-22 13:07:10 | 0.12        |                    1 |
|     3641 |        5 | 2024-07-22 13:07:11 | 0.06        |                    5 |
|     3642 |        2 | 2024-07-22 13:07:11 | 0.02        |                    2 |