How do I get an Arduino software to call a Python script when serial data stops?

I have been able to get serial data using an Arduino Nano w/ATmega328 to collect serial data and have a phython script to send an email alert. by itself the python send email no problem however when I add in ser = serial.Serial('/dev/ttyUSB0', 9600) the python script simply prints the serial data. I have then been trying to play with

while True: message = serial.Serial('/dev/ttyUSB0', 9600) print(message) if message[0] == 'null': send_email() time.sleep(0.5)

but this just gives an error (of course). I really am just trying to get the script to send an email when serial data stops. How can I do this?

thank you in advance for any tips.

How do I get an Arduino software to call a Python script when serial data stops?

You can’t. When the Arduino quits sending serial data, presumably that happened because the Arduino crashed or lost power. How you expect it to then send data to the PC asking it to execute a Python script is a mystery.

but this just gives an error (of course)

It might be obvious to you that it will generate an error, and what that error is. But you’re blocking the monitor.

cmbaker: I really am just trying to get the script to send an email when serial data stops. How can I do this?

If the Arduino sends data regularly it should be an easy job to make the Python program so it can detect the fact that no data is coming. The simplest way is probably to use the Python equivalent of the Arduino "if Serial.available() == 0".

...R

Agree with Robin2

you need to use

x = ser.inWaiting() # Return the number of chars in the receive buffer.

see - http://stackoverflow.com/questions/676172/full-examples-of-using-pyserial-package -

set the time when last char is received and if it exceeds a threshold --> trigger your "emergency code"