使用 Python 设置将插入到 MySQL 的温度和湿度的最大值和最小值

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

我有一些关于如何设置 max/min 温度或湿度值的问题,因为有时在重启期间,湿度值突然飙升至 >5000% 或有时温度 >100C。

所以,我想确保此数据不会插入 SQL 并使用添加到 .py

的 python 脚本进行过滤

我应该更改此行吗?

            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

您当然可以在此行中添加更多过滤器以确保值没有错误。

我建议在脚本开始时(导入之后)为温度和嗡嗡声设置常量有效最小值和最大值,例如:

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

如果满足以下条件,请切换您的原始版本:

if temp and hum is not None:

至:

if validate_reading(temp, hum):

此外,您接下来可以做的是在读数无效时添加不同的印刷品!

最终文件将如下所示:

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)