0

I have some problem on Rpi3. I work for kiosk program. I created python script. If rpi connect to internet, chrome shows page from internet but if there is not internet connection on pi, chrome shows the page I previously saved(page.html).

I created python script for this. I added this python script /home/pi/.config/lxsession/LXDE-pi/autostart file with this way @/usr/bin/python /home/pi/Desktop/check.py. My python script works good if you run manually. Script should control internet connection every ten second and it makes doing the above operations. When reboot rpi, chrome launches page correctly one time after that python script does not doing anything. It does not doing control internet connection or not start new tab. But manually work correct. I tried chmod +x but it did not work. Python script:

#!/usr/bin/env python  
import requests
import os
import webbrowser
import time

url='www.google.com'
url2='/home/pi/Downloads/page.html'
netflg=True

def internet_on():
    while True:
        try:
            response = requests.get('https://www.google.com/')
            netflg=True
            print(netflg)
            chrome_path='/usr/bin/chromium-browser %s' 
            webbrowser.get(chrome_path).open(url)
            loop()
            return True

        except:
            netflg=False
            print(netflg)
            chrome_path='/usr/bin/chromium-browser %s'
            webbrowser.get(chrome_path).open(url2)
            loop()
            return False

def loop():
    time.sleep(10)
    internet_on()

internet_on()

How can i fix this wrong?

  • 2
    recursive loops are a recipe for disaster! – Milliways Jan 24 '19 at 10:41
1

Is the script still alive after it opens Chrome on boot? Do a ps aux to check, my guess it that a webbrowser.Error was raised by either get or open while in the except: block and the script simply failed.

You could add some logging or redirect stdout to a file, if once you run the script with autostart you can't simply see the output anymore.

In addition to this, I'd recommend against calling loop() and internet_on() inside each other, it's better if you make a single method with things happening in that single big while TRUE: loop.

  • Yes I controlled my python script with ps aux | grep script.py. It is alive. – Faruk Jan 24 '19 at 8:54
  • Time to add some logging to understand what's happening, it could be looping in except: forever generating exceptions continuously. – Saltama Jan 24 '19 at 9:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.