Raspi Dont Connect to Router when Router Restarts

I have raspberry pi 3 model b which is powered by UPS System. I run octoprint on it to monitor 3d printing. Router is not connected to UPS so when power (mains) goes off and Router restarts, so Raspberry pi cannot connect to Wifi, I have to restart raspberry pi to make it work and connect it to wifi.
What to do to solve this, I want raspberry pi to continuously look for Wifi?

I have a few RPi3b and a b+. They are on a different circuit than the router, which often loses power.

While I have had issues with the Pi getting a different IP address upon router reboot, I have not had it not reconnect at all. I would hazard a guess that you are looking for the Pi on the old address, but, since it now has a new one, you don't see it

My solution for this problem was to input the MAC address of the Pi into the router, and assign it a static IP address in the router. This way, when the power comes back on the router will assign the same IP address it had before and you should be able to find it

I think I've troubleshot your problem. :wink:

Normally, this would be the allow-hotplug wlan0 command on a line by itself in /etc/network/interfaces but it looks like Raspbian Stretch has moved this functionality away to wpa_supplicant... only it doesn't appear to be in that area in the form of a conf file either.

I'm assuming of course that you're on wifi rather than using an RJ-45 Ethernet connection.

I run (a slightly modified version of) this script https://github.com/wxlcat/NetReconnector that constantly checks my network

yes i am on wifi. i cannot connect router to UPS as I have to run wires from one room to another which i dont want right now. any suggestion how to add auto reconnect functionality?

I could point you to here or here or here, support threads for Raspbian Stretch in which others are complaining and their workarounds.

The first link includes what appears to be a work-around similar to what @PythonAteMyPerl is suggesting. The third link looks promising and it's more of an approach that I would take by re-introducing the standard /etc/network/interfaces configuration file. You want that allow-hotplug.

I note that I'm on one of the newest OctoPi images and mine has no difficulties. If you're not using the 0.15.x version of OctoPi then perhaps upgrading would make it work. Some caveats though which might make my configuration different:

  • In my router, I've dedicated an IP address to my printer by its MAC address
  • I have turned off the Connectivity Check in Settings

OK.
Currently this is my network interafaces

auto lo
iface lo inet loopback

iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0-raspbian inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1-raspbian inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
mapping wlan0
  script /root/bin/map_iface
mapping wlan1
  script /root/bin/map_iface
source /boot/octopi-network.txt

i tried solutions you have mentioned, the raspi is unable to reconnect automatically when router restarts.

As for the first solution you mentioned i am not able to run the following code manually, code for wifi_rebooter.sh



#!/bin/bash
# ip server you wish to ping(google open source dns 8.8.8.8)
SERVER=8.8.8.8
echo "Hello World"
#only send two pings
ping -c2 ${SERVER} > /dev/null

#if return code from ping($?) is not 0  (meaning there was an error)
if  ${SERVER} != 0 :
then
        #restart wireless interface
        ifdown --force wlan0
        ifup wlan0
fi

when i run this this error occurs

pi@octopi:~/usr/local/bin $ sh wifi_rebooter.sh
Hello World
wifi_rebooter.sh: 9: wifi_rebooter.sh: 8.8.8.8: not found

Can you tell me where am I going wrong?

Oh, just remove all the comment lines from the file. The parentheses are confusing the bash shell and it's trying to run that for some reason.

lol thats very strange, why does it do that when '#' has been used for comments.
Anyways, i did a workaround, i added MAC address of pi in my router and now it auto-connects when router restarts.
Now next task is to control OCTOPI out of local network. Will use astroprint plugin or noip. lets see which works better.
Thanks for the help

I know, right? I'm always amazed when shell scripts get that stupid.

Good luck with controlling the printer outside of your network. Just don't do port-forwarding; neither the operating system nor OctoPrint were really designed to be Internet-safe.

I've never tried the AsroPrint, so I can't say how well that works, or even how it works

I guess I should try it

But, there are other safe ways that I can vouch for, you could install a VPN, either in the router or your computer, which can get complicated, and is kinda slow, or you could go the VNC route, which works faster than VPN, but, doesn't seem to transmit the video feed very well