How to get wifi network ssid in a container?

Hi Balena support team!

I’m using Balena wifi-connect in my application. I installed it inside a container and it’s working well there.
When I want to know now is how to get the SSID of the latest wifi network configured by wifi-connect app when the network connection drops and the router is powered off for some reasons.
The important thing is to get it in case the network connection drops (due to factors such as signal strength or interference) or the router is power off.
I’ve been trying to get it by a python or bash script in the running container, but I still can’t find a good way for it. In my application, it’s one of the important features.
I know wifi-connect app is based on using of NetworkManager and it always saves the last wifi credentials configured by it. So I’m expecting you can give a good soultion.
I have already installed NetworkManager in the container, so I can use nmcli command in the container.

Thanks in advance.
Anatoli

Hi everyone,

Sorry to ask again, but I really hope that you will provide a good solution to my problem
Did you have a chance to look at my question?

Hi there - using nmcli -t -f name connection show --active | head -n 1 should give you the SSID that the machine is currently connected to. You could use that to get the SSID when its up and running and store it for when a disconnect happens.

Out of interest, why do you need to retrieve the SSID? Even if the router is powered off the device that has been configured using wifi-connect should reconnect to it automatically once powers back on again.

Thanks for your reply.
This command returns the SSID only when the wifi network is active. I need to get the SSID when the wifi network is disconnected. Of course, the wifi network is the one where the Pi was connected until it was broken.

Out of interest, why do you need to retrieve the SSID? Even if the router is powered off the device that has been configured using wifi-connect should reconnect to it automatically once powers back on again.

Yes, you’re right. I also know it. And I understand that you wonder why I want to know it.
Let me explain it in detail.
In my application, wifi-connect app is automatically run by python daemon script when the network connection is broken, This script runs wifi-connect when the network connection is disconnected.
This script is running in a web container.
There are two cases of disconnection. The first case is when the user requested it on the web page.
In that case, I deactivate and delete the current wifi network connection using nmcli down and nmcli delete command. Then the network connection is broken, so wifi-connect is automatically executed by the daemon script.
The second case is when the strength of the wifi signal is very weak or the router is off. In that case, I don’t want to run wifi-connect, but my script runs it because the network connection is disconnected.
Therefore, I need to accurately distinguish between the two cases in my daemon script. If it’s the second case, wifi-connect shouldn’t run.
The only difference in two cases is that in the first case the connection is deactivated and deleted by programming in the web server code. The reason of deactivating and deleting is to see all wifi networks in ap list of balena app and choose one I want to connect to. In the first case, I though I couldn’t find the last network ssid because I already deleted the connection.

In the second case, there is no any logic related to wifi connection. So in this case, I thought that the last network credentials will remain somewhere. If I get it, I will know the disconnection is due to the second case.
Does it make sense?
I’ve tired most nmcli commands to get it, but I couldn’t find any solution.
When the balena app reruns, it always shows only the last network ssid in AP list. So I think there is a way to get the ssid even if the connection is lost.
Otherwise, is it a function of the mobile app side?

Hey @anatoli, I think it would help if we knew the exact trigger for your python daemon script:

This script runs wifi-connect when the network connection is disconnected

From what I can tell, it may be easier to adjust this script to only respond to certain types of disconnections (down/deleted vs disconnected). Are you able to share any of the source code with us so we can better offer suggestions?

Also regarding my colleague’s suggestion, what if you ran nmcli -t -f name connection show --active | head -n 1 while the AP was still connected (before it goes down) and saved that value in a file somewhere? Then if/when the AP goes down you can check that file to see the most recent SSID that worked.

Hi. Were you able to solve this issue?