I am learning python programming to use arduino sensors.
My first project is a TMP temp sensor that transmits readings wirelessly to a teensy,
which is connected by USB serial to a headless linux box.
My python code to listen to the serial port and upload data to rrd database is here
#!/usr/bin/python
import serial
from threading import Thread
import time
import rrdtool
import re
def receiving(ser):
buffer = ''
while True:
buffer = ser.read(ser.inWaiting())
if re.search("Sensor 1:", buffer):
temperature = buffer[10:15]
rrdtool.update('TMP36.rrd', 'N:%s' %temperature) # Write values to RRD
time.sleep(300) # Time for next read
if __name__ == '__main__':
ser = serial.Serial(
port="/dev/ttyACM0",
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
timeout=0.1,
xonxoff=0,
rtscts=0,
interCharTimeout=None
)
Thread(target=receiving, args=(ser,)).start()
I started the script with this
$ nohup python ./new_thread_read.py &
It ran for a few days and then it stopped in the middle of the night. Nothing in the logs to explain
what happened.
I'm not a programmer but would like to know how to keep the python script running.
Writing a cron script to check if the process stopped and then restart it seems unwieldy.
Is there something in the Python code that makes it vulnerable to serial data hiccups?