在wifi连接不连续后,python程序停止

时间:2017-09-17 12:58:04

标签: python multithreading upload raspberry-pi sensor

两个月前我开始学习Python,我已经为Raspberry pi项目编写了代码。我的问题是该程序在运行几个小时后停滞不前。我认为在所有情况下,它在一些wifi连接丢失后停止。但我不明白为什么如果wifi连接出现问题,整个程序就会停止。它会停止上传值并更新它打印的lcd屏幕消息(我从代码中删除了这些和其他内容,以便更容易阅读。)

代码从启动时开始(sudo python /home/pi/test.py& amp;)并包含两个主题:

" Control"线程通过使用i2c总线和传感器am2315读取温度和湿度,并根据温度阈值,通过GPIO控制继电器。

" Thingspeak"线程从'Thingspeak'中读取温度阈值。频道,然后将上一个主题中的测量值上传到' Thingspeak'。

我真的不知道该怎么做以及如何搜索任何解决方案。 任何帮助将不胜感激。

#! /usr/bin/env python
from time import sleep
import datetime
import urllib2
import RPi.GPIO as GPIO
import threading
import smbus
from tentacle_pi.AM2315 import AM2315
import smtplib
import contextlib

sleep(120)
# Lock
tLock = threading.Lock()
# Global variables
tem_global = 0; hum_global = 0
tem_hi = 35; relay = 21
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay, GPIO.OUT)
GPIO.output(relay, False)
sleep(1)

def Control():
        global temg, humg, tem_hi, relay
        # AM2315 setup
        am = AM2315(0x5c,"/dev/i2c-1")
        I2C_address = 0x70; I2C_bus_number = 1; i2c_channel_setup = 1
        bus = smbus.SMBus(I2C_bus_number)                
        bus.write_byte(I2C_address, i2c_channel_setup)
        sleep(1)

        while True:
                try:
                        tem_local, hum_local = am2315meas()
                except:
                        tem_local = -1; hum_local = -1

                tLock.acquire()
                tem_global = tem_local; hum_global = hum_local
                if tem_local < tem_hi:
                        GPIO.output(relay, True)
                else:
                        GPIO.output(relay, False)
                tLock.release()
                sleep(150)

def Thingspeak():
        global tem_global, hum_global, tem_hi
        myAPI = "..."
        channelID = "..."
        baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
        while True:
                sleep(30)
                try:
                        # Reading value from thingspeak
                        tLock.acquire()
                        with contextlib.closing(urllib2.urlopen("https://api.thingspeak.com/channels/%s/fields/1/last?" % channelID)) as f_read:
                                tem_hi = float(fread.read())
                        t.Lock.release()
                        sleep(30)
                        # Uploading values to thingspeak
                        tLock.acquire()
                        with contextlib.closing(urllib2.urlopen(baseURL + "&field1=%s" % tem_global + "&field2=%s" % hum_global)) as f_upload:
                                pass
                        tLock.release()
                except:
                        with open('/home/pi/errors.txt', mode='a') as file:
                                file.write('Network error recorded at %s.\n' % datetime.datetime.now())
                        file.close()
                        sleep(60)
                        continue

def Main():
        t1 = threading.Thread(target=Thingspeak)
        t2 = threading.Thread(target=Control)
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        GPIO.cleanup()

if __name__ == '__main__':
        Main()

1 个答案:

答案 0 :(得分:0)

问题解决了。正如James K Polk指出的那样,每次互联网连接关闭后,tLock.acquire()之后都会出现错误,从而导致程序死锁。以下是任何人都感兴趣的代码的更正部分。

def Control():
        global tem_global, hum_global, tem_hi, relay
        # AM2315 setup
        am = AM2315(0x5c,"/dev/i2c-1")
        I2C_address = 0x70; I2C_bus_number = 1; i2c_channel_setup = 1
        bus = smbus.SMBus(I2C_bus_number)                
        bus.write_byte(I2C_address, i2c_channel_setup)
        sleep(1)

        while True:
                try:
                        tem_local, hum_local = am2315meas()
                except:
                        tem_local = -1; hum_local = -1

                with tLock:
                        tem_global = tem_local; hum_global = hum_local
                        if tem_local < tem_hi:
                                GPIO.output(relay, True)
                        else:
                                GPIO.output(relay, False)
                sleep(150)

def Thingspeak():
        global tem_global, hum_global, tem_hi
        myAPI = "..."
        channelID = "..."
        baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
        while True:
                sleep(30)
                try:
                        # Reading value from thingspeak
                        with tLock:
                                with contextlib.closing(urllib2.urlopen("https://api.thingspeak.com/channels/%s/fields/1/last?" % channelID)) as f_read:
                                        tem_hi = float(fread.read())
                        sleep(30)
                        # Uploading values to thingspeak
                        with tLock:
                                with contextlib.closing(urllib2.urlopen(baseURL + "&field1=%s" % tem_global + "&field2=%s" % hum_global)) as f_upload:
                                        pass
                except:
                        with open('/home/pi/errors.txt', mode='a') as file:
                                file.write('Network error recorded at %s.\n' % datetime.datetime.now())
                        sleep(60)
                        continue