auto detect network connection

Place code snippets and demo code here
Post Reply
efalken
Posts: 38
Joined: Tue Mar 02, 2021 3:47 pm
Has thanked: 17 times
Been thanked: 26 times

auto detect network connection

Post by efalken »

Code: [Local Link Removed for Guests]


'********************************************************************************************
'wifi self-connecting and scanner example; shows ALL visible SSIDs and connect to strongest
'first insert your own credentials
'uncomment wlog messages to follow code execution

timer0 10000, concheck 'checks connection every 10 sec

'Declare APs and Passwords
own_aps_nr = 3 ' number of own APs
Dim own_aps$(own_aps_nr) = "SSID_1","SSID_2","SSID_3" ', ... ' my known APs
Dim own_pwds$(own_aps_nr) = "pw1", "pw2", "pw3" ', ... ' my known passwords

gosub newconnection 'establish first connection

'********************************************
' insert your main code here....
'********************************************
wait


'sub-routines*******************************
concheck: 'checked every 10 sec defined by timer0
if (wifi.status <> 3) or (wifi.rssi < -80) then ' no or week signal - you can define your owen threshhold for strength
  gosub newconnection
endif
return

newconnection: 'new connection is established after program start and when no or week signal was found
gosub scan
gosub split
gosub sort
gosub connect
'**************
if connect=0 then 'no connection found
 wlog "-----------------"
 wlog "NO CONNECTION !!!"
 wlog "-----------------"
endif

WIFI.CONNECT connect_ap$, connect_pw$ 'connection found, then connect...
While WiFi.status <> 3 'wait for success
  Pause 200
  wlog  "."
Wend
'new connection
'wlog "--------------------------------------"
'wlog "SUCCESS - Connected to: ",WIFI.RSSI, BAS.SSID$
'wlog "--------------------------------------"
return

'******** subroutine:  Scanning nearby APs*****************************
scan:
wifi.scan
while wifi.networks(html$) = -1
wend
return
'*******subroutine: Determine AP-names and signal-strengths**************
split:
NL$ = chr$(10)   ' line breaks
statnr=word.count(html$, NL$) 'equals no of APs found
dim stations$(statnr-1) 'define array of found APs
dim strength(statnr-1) ' and their signal strength
for i=0 to (statnr-1)  'browse APs
  ap$ = word$(html$, i+1,NL$) 'get one line per AP
  sid$ = word$(ap$, 1, ",") ' get SSID (1st parameter)
  RSSI$ = word$(ap$, 3, ",") ' get RSSID (3rd parameter)
  stations$(i)=sid$ ' store APs
  strength(i)=val(RSSI$) 'and their strengths
  'wlog RSSI$, sid$ 'list signals and names of found APs
next i
return

'********subroutine: bubble-sorting found APs******************************
sort:
for i=0 to (statnr-2)
  for j=0 to (statnr-2)
    if strength(j) <= strength(j+1) then
      x=strength(j)
      strength(j) = strength(j+1)
      strength(j+1) = x
      x$=stations$(j)
      stations$(j)=stations$(j+1)
      stations$(j+1)=x$
    endif
  next j
next i
for z=0 to (statnr-1)
  'wlog strength(z), stations$(z) 'output ordered signals and names
next z
return

'*********subroutine: Find the best AP amongst your own AP-list*********
connect:
for i = 0 to (statnr - 1) ' outer loop for all found APs
  for j = 0 to (own_aps_nr - 1) 'inner loop for all own APs
    if stations$(i)=own_aps$(j) then
      connect_ap$=own_aps$(j) 'give back the new AP to connect with
      connect_pw$=own_pwds$(j) ' and its password
      connect=1 'flag successfully found
      return ' exits subroutine
    endif
  next j
next i
connect=0
return

User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: auto detect network connection

Post by cicciocb »

Thanks, very interesting solution.
Post Reply