Need help tweaking code

Hi, In order to relieve the lockown boredom I've resurrected my Piborg. Over the years I've added to the original DiddyWeb script. I've changed backgound colours and button shapes. I've added wifi strength and a shutdown button. I've been having a bit of trouble lately.
I run this script using sudo ~/diddyborg-web/diddyWeb1.py in the terminal.

1. it takes ages for the script to run .
2. when I try "Press CTRL+C " to terminate the web-server I get an error in the terminal ( see attached) and it fails to shutdown even after repeated tries. I have resorted to using my shutdown button but this shuts down the Raspberry pi and I have to disconnect and reboot.
3. my video stream freezes but the buttons still function refreshing the page does nothing.
4 . video is very slow lagging ( what are the best settings for increased frame rate)

I am viewing this predominantly on my windows laptop using Chrome.

Hopefully You can help me resolve this. I've attached both the error log and the code I'm running.

John

piborg's picture

I have had a look and these are my thoughts on what might be happening.

1. it takes ages for the script to run .

Since not much has been changed I suspect that the addition of the signal strength may be part of the culprit here. Looking at the code it is refreshing this value several times a second (same rate as the video):

        elif getPath == ('/wifi'):
            # Repeated reading of the WiFi strength, set a delayed refresh
            # We use AJAX to avoid screen refreshes caused by refreshing a frame
            displayDelay = int(1000 / displayRate)

I would slow this down to say once every couple of seconds:

        elif getPath == ('/wifi'):
            # Repeated reading of the WiFi strength, set a delayed refresh
            # We use AJAX to avoid screen refreshes caused by refreshing a frame
            displayDelay = int(2000)

1000 would be one second between updates, larger is slower.

I am not sure how long it takes to run the iwconfig wlan0|grep Link|cut -d"=" -f2|cut -d" " -f1 command, but if it is slow you might want to read the value either in the Watchdog thread or a new thread and save the result to a global value. The new global value can then be read by the elif getPath.startswith('/wifi-once'): section instead of directly calling the WiFiSignalStrength function and having to wait for it to finish each time.

2. when I try "Press CTRL+C " to terminate the web-server I get an error in the terminal ( see attached) and it fails to shutdown even after repeated tries. I have resorted to using my shutdown button but this shuts down the Raspberry pi and I have to disconnect and reboot.

It seems like the WiFiSignalStrength function is not handling the CTRL+C exception as expected. I would insert an exception handler into the function directly with

try:

at the top of the function and

except KeyboardInterrupt:
    global running
    print('User shutdown in WiFiSignalStrength')
    running = False

at the end of the function to catch the CTRL+C and set the script to stop running on its own.

3. my video stream freezes but the buttons still function refreshing the page does nothing.

Does the image go blank / missing, or does it get stuck on a fixed image?

If it gets stuck it could be that the ImageCapture or StreamProcessor thread has failed and has stopped processing images.

4 . video is very slow lagging ( what are the best settings for increased frame rate)

I think part of the problem here is answered in question 1 as the code will slow down with more requests. Changing that may actually fix this problem as well.

Unfortunately the ideal value depends on the WiFi connection quite a lot. If the value is too large the stream gets slower rather than faster, causing the images to get delayed. The best thing to do is to start with a really low number, say 2, which should work correctly but does not update very often. When you have a working value increase it by 1 and see if it keeps working. When the number gets too high the video will get laggy again and you can swap back to the last value that worked correctly.

Subscribe to Comments for "Need help tweaking code"