Page 1 of 1

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

Posted: Sun Jul 01, 2018 1:17 pm
by CAP33
Is it possible to do so?
if <Connecting to a specific wifi network>:
Action

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

Posted: Mon Jul 02, 2018 8:41 am
by lmarmisa
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
  

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

Posted: Mon Jul 02, 2018 11:55 am
by CAP33
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".

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

Posted: Mon Jul 02, 2018 12:29 pm
by scotty101
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.

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

Posted: Mon Jul 02, 2018 1:19 pm
by CAP33
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?

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

Posted: Mon Jul 02, 2018 2:10 pm
by scotty101
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.

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

Posted: Mon Jul 02, 2018 2:15 pm
by CAP33
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!