Search code examples
mysqlpython-3.xarduinosensors

Set maximum and minimum value of temperature and humidity that will be inserted to MySQL using Python


I have some questions about how can we set a max/min value of temperature or humidity because sometimes during restart, the value of humidity suddenly spiked to >5000% or sometimes the temperature >100C.

So, I want to make sure that this data is not inserted to the SQL and filtered using python script added to the .py

Should I change this line?

            if temp is not and hum is not None:

import serial
import time
import pymysql.cursors
#Connect to database
connection = pymysql.connect(host='localhost',
                             user='ubuntu',
                             password='testing',
                             database='Blabla',
                             cursorclass=pymysql.cursors.DictCursor)
arduino = serial.Serial('/dev/ttyACM0', 9600)
print('Established serial connection to Arduino')
with connection:
    while True:
            arduino_data = arduino.readline()
            decoded_values = str(arduino_data[0:len(arduino_data)].decode("utf-8"))
            list_values = decoded_values.split(' ')
            print(f'Collected readings from Arduino: {list_values}')
            arduino_data = 0
            temp = list_values[0]
            hum = list_values[1]
            #Here we are going to insert the data into the Database
            if temp is not and hum is not None:
                with connection.cursor() as cursor:
                    cursor.execute("INSERT INTO `SensorTest` (`Temperature`, `Humidity`) VALUES (%s,%s)", (temp,hum))
                    connection.commit()
                    cursor.close()
            else:
                print('Failed to read the sensor')
            time.sleep(60)

Img1

Img2


Solution

  • Sure you can add more filters to this line to make sure the values are not wrong.

    I would recommended setting const valid minimum and maximum values for both temp and hum at the start of the script (after the imports), for example:

    MIN_VALID_TEMP = -20
    MAX_VALID_TEMP = 90
    MIN_VALID_HUM = 10
    MAX_VALID_HUM = 1000
    

    Next I would recommend splitting the temp and hum validation to an helper functions, such as:

    def validate_reading(temp, hum):
        if not temp or not hum:
            return false
        if (temp > MIN_VALID_TEMP  and temp < MAX_VALID_TEMP and
        hum > MIN_VALID_HUM and hum < MAX_VALID_HUM):
            return true
        return false
    

    and switch your original if:

    if temp and hum is not None:
    

    to:

    if validate_reading(temp, hum):
    

    Also what you can do next is to add a different print when the reading is not valid!

    The final file will look like this:

    import serial
    import time
    import pymysql.cursors
    
    # define valid temp and hum
    MIN_VALID_TEMP = -20
    MAX_VALID_TEMP = 90
    MIN_VALID_HUM = 10
    MAX_VALID_HUM = 1000
    
    
    def validate_reading(temp, hum):
        if not temp or not hum:
            return False
        if temp > MIN_VALID_TEMP and temp < MAX_VALID_TEMP and hum > MIN_VALID_HUM and hum < MAX_VALID_HUM:
            return True
        return False
    
    
    # Connect to database
    connection = pymysql.connect(
        host="localhost", user="ubuntu", password="testing", database="Blabla", cursorclass=pymysql.cursors.DictCursor
    )
    arduino = serial.Serial("/dev/ttyACM0", 9600)
    print("Established serial connection to Arduino")
    with connection:
        while True:
            arduino_data = arduino.readline()
            decoded_values = str(arduino_data[0 : len(arduino_data)].decode("utf-8"))
            list_values = decoded_values.split(" ")
            print(f"Collected readings from Arduino: {list_values}")
            arduino_data = 0
            temp = list_values[0]
            hum = list_values[1]
            # Here we are going to insert the data into the Database
            if validate_reading(temp, hum):
                with connection.cursor() as cursor:
                    cursor.execute("INSERT INTO `SensorTest` (`Temperature`, `Humidity`) VALUES (%s,%s)", (temp, hum))
                    connection.commit()
                    cursor.close()
            else:
                print("Failed to read the sensor")
            time.sleep(60)