CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Is it possible to make a connection to a certain Wi Fi network as a condition?

Sun Jul 01, 2018 1:17 pm

Is it possible to do so?
if <Connecting to a specific wifi network>:
Action

User avatar
lmarmisa
Posts: 1233
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Is it possible to make a connection to a certain Wi Fi network as a condition?

Mon Jul 02, 2018 8:41 am

Try:

Code: Select all

#!/usr/bin/env python

import subprocess

try:
  ssid =  subprocess.check_output(["iwgetid", "-r"]).rstrip()
except subprocess.CalledProcessError:
  ssid = ""

if ssid == "mySSID" :
  ActionHere
  

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: Is it possible to make a connection to a certain Wi Fi network as a condition?

Mon Jul 02, 2018 11:55 am

lmarmisa wrote:
Mon Jul 02, 2018 8:41 am
Try:

Code: Select all

#!/usr/bin/env python
import subprocess
try:
  ssid =  subprocess.check_output(["iwgetid", "-r"]).rstrip()
except subprocess.CalledProcessError:
  ssid = ""
if ssid == "mySSID" :
  ActionHere
  

Code: Select all

#!/usr/bin/env python

import subprocess
from time import sleep

try:
  ssid =  subprocess.check_output(["iwgetid", "-r"]).rstrip()
except subprocess.CalledProcessError:
  ssid = ""

while True:
  if ssid == "MySSID" :
      print("Connect")
  else:
      print("No connect")
  sleep(1)
Unfortunately, it does not work. Writes "No connect".

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Is it possible to make a connection to a certain Wi Fi network as a condition?

Mon Jul 02, 2018 12:29 pm

Did you put the name of your SSID in to the following line?

Code: Select all

if ssid == "MySSID"
You can of course add

Code: Select all

print(ssid)
to your code to see what wifi network is detected.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: Is it possible to make a connection to a certain Wi Fi network as a condition?

Mon Jul 02, 2018 1:19 pm

scotty101 wrote:
Mon Jul 02, 2018 12:29 pm
Did you put the name of your SSID in to the following line?

Code: Select all

if ssid == "MySSID"
You can of course add

Code: Select all

print(ssid)
to your code to see what wifi network is detected.
Yes, I put the name of my SSID in to the following line

Code: Select all

if ssid == "MySSID"
I added a line

Code: Select all

print(ssid)
and noticed that a string is output b'bn98'.
bn98 it my ssid, but what is b?

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Is it possible to make a connection to a certain Wi Fi network as a condition?

Mon Jul 02, 2018 2:10 pm

b indicates that it is a python 3 "byte literal".

Try the following instead of the current if.

Code: Select all

if ssid.decode('ascii') == 'bn98':
This decodes the "bytes literal" to a standard python string.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: Is it possible to make a connection to a certain Wi Fi network as a condition?

Mon Jul 02, 2018 2:15 pm

scotty101 wrote:
Mon Jul 02, 2018 2:10 pm
b indicates that it is a python 3 "byte literal".
Try the following instead of the current if.

Code: Select all

if ssid.decode('ascii') == 'bn98':
This decodes the "bytes literal" to a standard python string.
Many thanks!

Return to “Python”