一旦所有值都分配了python,就分配变量并发送到数据库

时间:2015-03-05 04:23:03

标签: python mysql python-2.7

这是我的第一个python脚本。我试图从Arduino获取数据,在Raspberry Pi上读取它并将其保存到数据库中。代码单独工作(我可以正确分配变量并将数据发送到数据库,但似乎无法使它们都工作。我不确定我的逻辑是否正常工作(将变量设置为null然后保存一旦他们都有价值观点。感谢您的投入。

    import re
    import serial
    import MySQLdb
    import time

db =MySQLdb.connect(host = "localhost",user = "root",passwd = "example", db = "arduino")
ser =serial.Serial('/dev/ttyACM0',9600) 

humidityPattern = "Humidity\:(\d\d\.\d\d)"
tempDhPattern = "TemperatureDH\:(\d\d\.\d\d)"
barometerPattern = "PressureBMP\:(\d\d\.\d\d)"
tempBmpPattern = "TemperatureBMP\:(\d\d\.\d\d)"
tempTmpPattern = "TemperatureTMP\:(\d\d\.\d\d)"
blLightPattern = "BLLight\:(\d+)"
brLightPattern = "BRLight\:(\d+)"
frLightPattern = "FRLight\:(\d+)"
flLightPattern = "FLLight\:(\d+)"


while 1:
    line = ser.readline()
    humidity = None
    tempDh = None
    pressure = None
    tempBmp = None
    tempTmp = None
    blLight = None
    brLight = None
    frLight = None
    flLight = None

    #Humidity Sensor
    m = re.match(humidityPattern, line)
    if m is not None:      
        humidity = m.group(1)
        print "Humidity is "+humidity

    m = re.match(tempDhPattern, line)
    if m is not None:
        tempDh= m.group(1)
        print "Humidity Temp is "+tempDh

    #Pressure Sensor
    m = re.match(barometerPattern, line)
    if m is not None:
        pressure = m.group(1)
        print "Pressure is "+tempDh

    m = re.match(tempBmpPattern, line)
    if m is not None:
        tempBmp= m.group(1)
        print "Pressure Temp is "+tempBmp

    #Temp Sensor
    m = re.match(tempTmpPattern, line)
    if m is not None:
        tempTmp= m.group(1)
        print "Temp is "+tempTmp

    #Light Sensors
    m = re.match(blLightPattern, line)
    if m is not None:
        blLight= m.group(1)
        print "BL Light is "+ blLight

    m = re.match(brLightPattern, line)
    if m is not None:
        brLight= m.group(1)
        print "BR Light is "+ brLight

    m = re.match(frLightPattern, line)
    if m is not None:
        frLight = m.group(1)
        print "FR Light is "+ frLight

    m = re.match(flLightPattern, line)
    if m is not None:
        flLight = m.group(1)
        print "FL Light is "+ flLight

    if humidity and tempDh and pressure and tempBmp and tempTmp and blLight and brLight and frLight and flLight is not None:
        with db:
            cur = db.cursor()
            cur.execute('insert into weather(humidity, temp_dh, pressure,temp_bmp, temp_tmp, bl_light, br_light, fr_light, fl_light) values("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")'%(humidity, tempDh, pressure, tempBmp, tempTmp, blLight, brLight, frLight, flLight ))
            time.sleep(5)
            print 'upload'

1 个答案:

答案 0 :(得分:0)

测试中存在一个问题:

if humidity and tempDh and pressure and tempBmp and tempTmp and blLight and brLight and frLight and flLight is not None:

仅测试None最后一个变量flLight。但是,如果看起来其他所有其他内容都是None或非空字符串,则应该意外工作,因为None是假的,每个非空字符串都是truthy。

所以一个更大的问题是,每次通过循环,你都会丢掉你之前读过的每一个值,无论你是否保存过它们。

要修复那个,请添加一个布尔标志must_init并在循环开始时将逻辑更改为类似的内容:

must_init = True 而真:     line = ser.readline()     如果must_init:         湿度=无         tempDh =无         压力=无         tempBmp =无         tempTmp =无         blLight =无         brLight =无         frLight =无         flLight =无         must_init = False

并仅在最后must_init = True之后的with语句的最末端再次设置print 'upload'

这样,您只会在第一次将所有变量归零(A),或者在将之前的值保存到DB后立即将(B)归零,这似乎是更正确的逻辑。

其他简化改进是可能的(例如,将变量保存为dict中的项目,因此您不必在any检查中枚举它们;将RE表达式也放在以相同名称键入的dict中你在变量dict中使用来极大地压缩和简化你的代码)但关键是我之前建议添加must_init布尔标志的代码应该工作 - 你之后可以改进它! - )