Silicon's blog

Menu
  • Home
  • Kadena
  • Solana
  • Ethereum
  • Bot Automation
  • Proxmox
  • Nginx Proxy Manager
  • Others
  • Contact
Menu

How to auto restart your router daily if connection loss using Tasmota with Sonoff S20

Posted on May 14, 2023May 17, 2023 by Silicon
Sharing is Caring:
Twitter 0
Copy 0

 

In the previous article, you learned how to configure your smart home plug using Tasmota firmware. This article will teach you some specific use cases of Tasmota. Assuming a 4G/5G router is connected to your smart plug and there is a Raspberry Pi or industrial PC on the same network in a remote site if your 4G/5G router loses connection, it will be costly to reboot the router manually. However, we can use the Raspberry Pi or IPC on the same network to check whether your router loses connection. Combined with Tasmota smart plug, your router can be restarted at a specific time during connection loss to minimize data loss.

The Python script is attached below; you may need time to digest it. It will turn off today’s restart Timer and reset yesterday’s Timer once your device connects to the network. If your router loses connection for a whole day, the Timer won’t be turned off and will restart at 00:00 the next day. Replace IP with your Tasmota smart plug IP.


import requests

if __name__ == "__main__":
    ip = "x.x.x.x"
    while True:
        try:
            result = myping("google.hk")    
            print(result)
            result += myping("yahoo.com.hk")
            print(result)
            result += myping("cloudflare.com")
            print(result)
            
            if(result != 0):
                day_of_the_week = datetime.today().weekday()
                
                if(day_of_the_week == 0): #Monday
                    today = '"Days":"-M-----"'
                    tomorrow = '"Days":"--T----"'
                elif(day_of_the_week == 1): #Tuesday
                    today = '"Days":"--T----"'
                    tomorrow =  '"Days":"---W---"'
                elif(day_of_the_week == 2): #Wednesday
                    today = '"Days":"---W---"'
                    tomorrow = '"Days":"----T--"'
                elif(day_of_the_week == 3): #Thursday
                    today = '"Days":"----T--"'
                    tomorrow = '"Days":"-----F-"'
                elif(day_of_the_week == 4): #Friday
                    today = '"Days":"-----F-"'
                    tomorrow = '"Days":"------S"'
                elif(day_of_the_week == 5): #Saturday
                    today = '"Days":"------S"'
                    tomorrow = '"Days":"S------"'   
                elif(day_of_the_week == 6): #Sunday
                    today = '"Days":"S------"'   
                    tomorrow = '"Days":"-M-----"'     
                
                #disable tomorrow timer
                print('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2+1)+' {"Enable":0,"Time":"00:00","Window":0,'+today+',"Repeat":1,"Output":1,"Action":0}')
                print(requests.get('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2+1)+' {"Enable":0,"Time":"00:00","Window":0,'+today+',"Repeat":1,"Output":1,"Action":0}'))
                print('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2+2)+' {"Enable":0,"Time":"00:10","Window":0,'+today+',"Repeat":1,"Output":1,"Action":1}')
                print(requests.get('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2+2)+' {"Enable":0,"Time":"00:10","Window":0,'+today+',"Repeat":1,"Output":1,"Action":1}'))
                
                #re-enable today timer
                if(day_of_the_week != 0): #today != monday 
                    print('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2-1)+' {"Enable":1,"Time":"00:00","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":0}')
                    print(requests.get('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2-1)+' {"Enable":1,"Time":"00:00","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":0}'))
                    print('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2)+' {"Enable":1,"Time":"00:10","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":1}')
                    print(requests.get('http://'+ip+'/cm?cmnd=Timer'+str(day_of_the_week*2)+' {"Enable":1,"Time":"00:10","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":1}'))            
                else: #today = monday 
                    print('http://'+ip+'/cm?cmnd=Timer'+str(13)+' {"Enable":1,"Time":"00:00","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":0}')
                    print(requests.get('http://'+ip+'/cm?cmnd=Timer'+str(13)+' {"Enable":1,"Time":"00:00","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":0}'))
                    print('http://'+ip+'/cm?cmnd=Timer'+str(14)+' {"Enable":1,"Time":"00:10","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":1}')
                    print(requests.get('http://'+ip+'/cm?cmnd=Timer'+str(14)+' {"Enable":1,"Time":"00:10","Window":0,'+yesterday+',"Repeat":1,"Output":1,"Action":1}'))            
    
                pass
            else: 
                pass
        except Exception:
            print("No network!")
        
        print("next cycle in 21600s (6hrs)")
        time.sleep(21600)  

Leave a Reply Cancel reply


The reCAPTCHA verification period has expired. Please reload the page.

©2025 Silicon's blog
Click to Copy