I made a code in python, but it hangs after a while running, can anyone help me to optimize it so it does not crash?
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import serial
import time
locations=['/dev/ttyACM0']
while 1:
arquivo = open('temperaturaumidade.txt','r')
conteudo = arquivo.readlines()
for device in locations:
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
break
except:
print "Failed to connect on",device
try:
time.sleep(1)
conteudo.append(arduino.readline())
print arduino.readline()
conteudo.append('\n')
arquivo = openarquivo = open('temperaturaumidade.txt','w')
arquivo.writelines(conteudo)
except:
print "Failed to read!"
arquivo.close()