Raspberry Pi connection with Arduino over XRF continuous sensor monitoring
Question: I'm trying to make a system for monitoring sensor continuously including
registering the time when the sensors are offline (not reachable by the
server).

The system is based on Raspberry Pi as server and Arduino Leonardo as node
with sensors and the data is communicated from Arduino to Raspberry via XRF.

I'm using Python script with pySerial for collecting data. My Python skills
are poor and I need help to figure out how to continue monitoring when the
connection between Arduino and Raspberry is lost and when the connection is
re-established the sensor data could be collected again without restarting the
script.

the Python script follows:

    
    
    import serial
    from datetime import datetime
    
    ser = serial.Serial('/dev/ttyAMA0', 9600)
    
    while True:
       #Arduino sends sensor analog reading when receiving the letter "k"
       ser.write('k') 
       datetime = str(datetime.now())
       print(datetime)
       print(ser.readline())
    

At the moment once the server looses connection to the client the script will
stop.


Answer: I guess you need periodically reopen the connection, and also log the moments
when the connection is lost? something like this...

    
    
    import serial
    import time
    from datetime import datetime
    
    ser = serial.Serial('/dev/ttyAMA0', 9600)
    
    while True:
       if ser.isOpen()==True:
           ser.write('k') # Arduino sends sensor analog reading when receiving the letter "k"
           datetime = str(datetime.now())
           print(datetime)
           print(ser.readline())
           time.sleep(1) # take a break so that you dont thrash the sensor with requests all the time
       else:
           try:
               print "Lost connection at %s" % str(datetime.now())
               ser.open()              
           except:
               print "Cannot open port."
               exit(-1)
    

