Item or rule to reboot RPI3

I have OpenHAB installed on a RPI3 that uses a wireless connection. I have experienced problems with the RPI3 losing the connection every week or so. When this happens obviously OpenHAB is unable to control my lighting, security system, etc… I am looking for a way to reboot the RPI3 when it loses the wireless connection. I am thinking I might be able to do it with the network health and exec binding. Would something like this work?

Switch wireless "Wireless Connection" {nh="192.168.0.1", exec=">[OFF:ssh user@host sudo reboot]" }

The IP address 192.168.0.1 is my router. I am not sure if the exec command is going to reboot my router or the RPI3. I want to reboot just the RPI3. I haven’t actually tried it as I am nervous that it might create some type of looping where it would continually reboot. If someone could help me out it would be greatly appreciated. Thanks.

I would use the network binding and create a device for your gateway/router 192.168.0.1. Under it create a switch for the online switch named “router”

Then under rules create something like:

  
 rule "Reboot Openhab WIFI is down" when
  Item router changed from ON to OFF
 then
executeCommandLine("sudo init 6")
Thread::sleep(30000) // 30 second delay
executeCommandLine("your sudo password here")
end

Alternatively you could try restarting your WLAN instead of rebooting. Same rule would apply just a different command.

Thanks for the reply BK. Maybe it would be better to restart the RPI3’s internet connection, instead of rebooting. Do you know what the commands for that would look like?

Yes, I suppose something like this should work, but would need to be tested.

rule "Restart RPI3 WIFI" when
  Item router changed from ON to OFF
 then
executeCommandLine("sudo /sbin/ifdown wlan0")
Thread::sleep(10000) // 10 second delay
executeCommandLine("your sudo password here")
Thread::sleep(10000) // 10 second delay
executeCommandLine("sudo /sbin/ifup wlan0")
end

I am all but certain this will not work. Each executeCommandLine gets its own shell so the shell where you executed the init will be long gone by the time the line where you enter your password runs, and even if it were not gone, it would be a completely different shell.

Also, anything longer than 300 is considered far too long of a sleep in Rules. Given this rule is trying to shutdown the system that is probably overlookable.

Typically to run sudo commands with an apt-get installed openHAB one needs to add the openhab user to sudoers and configured with no password for those commands one needs to execute from OH.

On a modern Jessy raspbian image, sudo reboot is a cleaner way to reboot.

@MikeH, see OpenHAB sudo [Exec Binding] for a tutorial on how to add the openhab user to sudoers and do not require a passowrd for ifdown and ifup. Then the body of your rule should be

var results = executeCommandLine("sudo /sbin/ifdown wlan0", 5000)
logInfo("Restart Wifi", "Results from shutting down wlan0: " + results)
createTimer(now.plusSeconds(10), [|
    results = executeCommandLine("sudo /sbin/ifup wlan0", 5000)
    logInfo("Restart Wifi", "Results from starting up wlan0: " + results
]
1 Like

Rich, I tried your method. I tested it by typing the command: sudo /sbin/ifdown wlan0 . It seems to work. I was unable to pull up my sitemap in my browser for a minute or so and then it starting working again. I did make some changes in the start wlan0 up line, I think you missed the “var”. Here is what I have:

rule "Reset RPI3 WIFI" 
when
Item router changed from ON to OFF
then
var results = executeCommandLine("sudo /sbin/ifdown wlan0", 5000)
logInfo("Restart Wifi", "Results from shutting down wlan0: " + results)
createTimer(now.plusSeconds(10)) [|
    var results1 = executeCommandLine("sudo /sbin/ifup wlan0", 5000)
    logInfo("Restart Wifi", "Results from starting up wlan0: " + results1)
]    
end

Does that look ok?

The loginfo results are:

2017-10-31 08:30:43.398 [INFO ] [.smarthome.model.script.Restart Wifi] - Results from shutting down wlan0: /sbin/ifdown: interface wlan0 not configured
2017-10-31 08:30:53.964 [INFO ] [.smarthome.model.script.Restart Wifi] - Results from starting up wlan0:

I was unable to get it to work when trying to restrict the user rights of openhab.

Ah, but when you just type it you are running it as a different user than openHAB is running as. To test it as the openhab user you can use a command like:

sudo -u openhab sudo /sbin/ifdown wlan0

I didn’t miss it, it shouldn’t be needed. The Timer gets a full copy of the context where it is created. This includes all vars and vals that exist when createTimer is called. So in my code, I’m just reusing the results var from the full call inside the Timer.

Run the command using the example above so you can see how it runs as the openhab user on the command prompt.

When I change the rule to exactly as you have it I get an error message in smarthome designer:
Cannot refer to the non-final variable results inside a lambda expression.

Oops, I forgot about that little detail. Yes, you can only pass vals into a lambda like that, not vars. And of course a valid cannot be reassigned so there changes you made are necessary.