Home > Back-end >  Telemetry data through python socket, without stopping execution of the program
Telemetry data through python socket, without stopping execution of the program

Time:07-27

I'm building photovoltaic motorized solar trackers. They're controlled by Raspberry Pi's running python script. RPI's are connected to my public openVPN server for remote control and continuous software development. That's working fine. Recently a passionate customer asked me for some sort of telemetry data for his tracker - let's say, it's current orientation, measured wind speed etc.. By being new to python, I'm really struggling with this part.

I've decided to use socket approach from guides like this. Python script listens on a socket, and my openVPN server, which is also web server, connects to it using PHP fsockopen. Python sends telemetry data, PHP makes it user friendly and displays it on the web. Everything so far works, however I don't know how to design my python script around it.

The problem is, that my script has to run continuously, and socket.accept() halts it's execution, waiting for a connection. Didn't find any obvious solution on the web. Would multi-threading work for this? Sounds a bit like overkill.

Is there a way to run socket listening asynchronously? Like, for example, pigpio callback's which I'm using abundantly?

Or alternatively, is there a better way to accomplish my goal?

I tried with remote accessing status file that my script is maintaining, but that proved to be extremely involved with setup and prone to errors when the file was being written.

I also tried running the second script. Problem is, then I have no access to relevant data, or I need to read beforementioned status file, and that leads to the same problems as above.

Relevant bit of code is literally only this:

# Main loop
try:
    while True:
        
        # Telemetry
        conn, addr = S.accept()
        conn.send(data.encode())
        conn.close()

Best regards.

CodePudding user response:

For a simple case like this I would probably just wrap the socket code into a separate thread.

With multithreading in python, the Global Interpreter Lock (GIL) means that only one thread executes at a time, so you don't really need to add any further locks to the data if you're just reading the values, and don't care if it's also being updated at the same time.

Your code would essentially read something like:

from threading import Thread 

def handle_telemetry_requests():
   
    # Main loop
    try:
        while True:
            
            # Telemetry
            conn, addr = S.accept()
            conn.send(data.encode())
            conn.close()
    except:
        # Error handling here (this will cause thread to exit if any error occurs) 
        pass 

    
socket_thread = Thread(target=handle_telemetry_requests)
socket_thread.daemon = True 
socket_thread.start() 

Setting the daemon flag means that when the main application ends, the thread will also be terminated.

Python does provide the asyncio module - which may provide the callbacks you're looking for (though I don't have any experience with this).

Other options are to run a flask server in the python apps which will handle the sockets for you and you can just code the endpoints to request the data. Or think about using an MQTT broker - the current data can be written to that - and other apps can subscribe to updates.

  • Related