Keep losing wi-fi connection

Hi!
So I have made an adapter to a BT05 temperature logger based on https://github.com/tim-hellhake/xiaomi-temperature-humidity-sensor-adapter which uses Bluetooth.
I was able to get it to work but I am constantly losing my wi-fi connection (can only get it back by rebooting my raspberry pi) and I wonder if it has anything to do with my adapter. I think it was not happening before.
Are there any known problems that I should be aware of?

Ps: I am using a raspberry 3 b+. Right now I am only reading info from advertisements that are broadcast every 2 seconds or so. I’ll happily provide adapter code if needed.

Wi-Fi and Bluetooth are on the same chip on the Raspberry Pi and can cause a lot of issues if used in tandem. My recommendation would be to either use ethernet and disable wi-fi, or get a separate Bluetooth dongle and disable the one on the board.

Instructions for disabling things are available here.

1 Like

I believe the RPi4 eliminates this problem by separating the USB from the Bluetooth/WiFi and Ethernet. Please, someone correct me if I am wrong. I have a RPi4 that I was planning to use as a wall controller with 7" touch screen. I may swap it with my RPi3 which is currently running my Gateway. Much more throughput, from what I have read.

1 Like

Oooh that’s unfortunate!
Guess I’ll have to use a dongle or update to the rpi 4 then… Thanks @mrstegeman and @lacojim

So, I have now tested in a RPI 4 and it’s happening the same…
I have my sensor working and I am connected to my home wi-fi.
At first everything works fine, I can ping different addresses from the rpi4 and I can see my gateway homepage on my laptop. After some time I stop being able to ping any address BUT I am still connected to my home wi-fi and I can still see the temperature values updating on my gateway homepage… Also, for example, adding new addons always fail now…
If I reboot everything will be normal again for a couple of minutes…
Any idea of what is happening @mrstegeman ?
Thanks again for the help!

PS:Also ssh works fine all the time.

If you SSH into the Pi and run ifconfig, does your wlan0 have an IPv4 address?

Also, I believe the RPi 4 still has the Bluetooth and Wi-Fi on the same chip, so the same problem may still exist.

Your problem seems very odd. If you are still getting temp updates and the WebGUI is still active, then it would appear you are still indeed connected to your WiFi. Perhaps try temporarily turning off the bluetooth and test if the problem persists. You might also consider changing the WiFi channel on your router.

Sorry for the long absence. Busy days… Had to stop a bit this issue.
@mrstegeman I think you are right, they have. But now I think it’s ok, I’ll run further tests on my normal wifi when I have the time!
@lacojim I tried that and the problem persisted so I tried the solution below.
So I think it’s my router firewall messing with the raspberry… As I am trying to use my phone as hotspot and I think everything is going ok and the described problem it’s not happening here… Still it’s very weird because I don’t have any special settings or problems with other devices…
Thank you both for the help!

So… new chapter on my wifi saga, almost a new problem…
Now I am not using any BT adapter and I am connecting to my mobile phone hotspot.
At first everything worked well… But after some time I lose connection and I can’t get it up again no mater what I do… If I restart my RPI it will try to connect, it will connect (I get the ip on the starting logs and my phone gets a device connected warning) and instantly disconnects and enters AP mode.
Now if I try to do:
sudo dhclient -r wlan0
sudi wpa_cli -i wlan0 reconfigure
sudo dhclient -v wlan0
I can get my previous IP address and it connects to my mobile phone (On my mobile it says that the gateway is connected) BUT if I then do ifconfig wlan0 sometimes (although connected to my phone) it says that I have the AP IP (192.168.2.1) and when I manage to get my normal IP (if I do ifconfig wlan0 and it shows (ex. 192.168.43.166) if I do any sort of ping or download something using wget it shows that I am doing it through 192.168.2.1 and I am not able to download anything or pull from git for example…
This is driving me crazy!

PS: I can ssh to my pi using my normal IP…
Also I booted everything from scrath already… it worked for the day but not working anymore

Using your phone as a wireless hotspot would put your Gateway on a different subnet from the rest of your house. I am not sure what negative effects this might have, but I am sure there could be plenty, unless you are doing some fancy routing between your phone hotspot and your home wifi. I think perhaps doing things this way is only adding to your existing problem. Have you considered just using the ethernet port on your RPi instead of wireless? Ethernet is much more reliable with this type of setup.

At least try testing it with ethernet and see if your problems continue.

I am not interfacing with my home network. I am just using my phone hotspot. If I need to use SSH I connect my laptop to my phone also. Right now I was developing an adapter for PiJuice, so no need to connect other external Things.
I tried testing it with ethernet on my home network but I had a problem with it and it kept asking if I was supposed to be authenticated (which is weird… all other devices work by just plug them in)… I’ll give it another proper try later.
But I need to be able to connect to my hotspot because I am using this for College project and I need a way for it to be portable (that’s another reason for my mobile hotspot)

AAH! So through my debugging I was almost sure that I had a problem with the AP mode. I thought that, somehow, it was conflicting with my wifi when booting (because of what I explained in the previous post of connecting disconnecting and then I managed to have my RPI connected but it would always try to connect to a 192.168.2.1 IP address if I did a Ping or whatsoever…
So I ended up in https://github.com/mozilla-iot/gateway-wifi-setup and tried:
$ sudo systemctl disable hostapd
$ sudo systemctl disable dnsmasq
and it WORKED! I am now able to connect every single time!

I don’t really know what was happening at a detail level… But I am sure it was not because of anything I have done (Ihaven’t done any network configs whatsoever other than the normal setup)… So I believe this problem is reproducible and might occur to others…
@mrstegeman do you know of any cases? Should I open an issue or there is no point?
@lacojim thanks for the help!

When the Pi boots, it tries to connect to the configured Wi-Fi network. If that fails, it reverts back to AP mode (i.e. hostapd and dnsmasq are started). So, maybe that’s what’s happening?

Well in my case I would connect to my phone, get an IP address (it would show on startup logs and my mobile phone would warn me) and then it would automatically disconnect and it would enter in AP mode. From here I couldn’t connect again to my network. If I disable hostapd and dnsmasq It will not revert back to AP mode and I’ll have a stable connection.
Any idea?

This is the code that ends up starting up the AP: https://github.com/mozilla-iot/gateway/blob/d00a92924118939044e17ad2aba0321a21844c3c/src/wifi-setup.js#L335-L373

That, in turn, calls this: https://github.com/mozilla-iot/gateway/blob/d00a92924118939044e17ad2aba0321a21844c3c/src/wifi-setup.js#L375-L445

Basically, the gateway watches for an IPv4 address on wlan0 for 60 seconds. If that doesn’t happen, it bails and starts up the AP. There is nothing else after that point (in the gateway, at least) which would be starting up the AP.

Do you have a log you could post? If it’s sensitive, you can email me: mstegeman@mozilla.com

1 Like

So after midnight, for some reason, hostapd and dnsmasq reactivated automatically and I lost network after a reboot (any update or some checks that are made daily that causes this? Coincidence?).
I used the opportunity to get some logs and some prints (photos sry…) so you guys can see what’s happening! I can add a log of a normal boot with hostapd and dnsmasq off if you want.
Zip content:
Logs before turning off hostapd and dnsmasq: I managed to get my normal IP but then reverted back to AP mode.
photo1: shows initialization and ifconfig result
photo2: ifconfig after dhclient commands, normal ip obtained and mobile phone shows that i am connected.
Photo3: shows unsuccessful ping although I am connected to mobile network
Photo4: Successful ping after turning off hostapd and dnsmasq and rebooted.
logsTiago.zip (848.9 KB)

PS: Thank you for pointing me out what code is starting the AP, I’ll give it a look!

  • Photo 1: after boot, your Pi is going into AP mode, hence the .1 IP address.
  • Photo 2: I’m confused how you’re getting a DHCP address if you’re in AP mode.
  • Photo 3: your Pi is still trying to route through 192.168.2.1, which is your IP address in AP mode.

From your logs, I can see this:

  • hostapd/dnsmasq were started before the gateway, which means those are both enabled system-wide. You can disable them like this:
    sudo systemctl disable dnsmasq
    sudo systemctl stop dnsmasq
    sudo systemctl disable hostapd
    sudo systemctl stop hostapd
    
  • After the gateway starts, things still seem to be in a weird state with the network, so it goes into wifi-setup (AP mode).

So, debugging/fixing:

  • Try manually disabling those 2 services above
  • Make sure /etc/wpa_supplicant/wpa_supplicant.conf looks right. It should have a definition for you Wi-Fi network and not much else:
    country=GB
    update_config=1
    ctrl_interface=/var/run/wpa_supplicant
    
    network={
     ssid="MyNetworkSSID"
     psk="Pa55w0rd1234"
    }
    
  • Restart the Pi.