[PYTHON] Pocket wifi sim settings automatic tool

Purpose

Since a large amount of pocket wifi needs to be set automatically, the apn setting of pocket wifi is automated. As the operation, read qr, get wifi setting, wifi automatic connection, go to wifi local host with selenium and rewrite apn.

environment

mac catalina soracom sim plan-k fs030w (I use fs030w for pocket wifi where au and docomo line sim works. (Teltonika was unstable in my environment, so I don't recommend it.))

Advance preparation

qr read

Install zbar referring to the following Read multiple QR codes with Python 3

brew install zbar

Since I got it above, I corrected it referring to here

brew link zbar

libexec/pyenv: No such file or directory It was said that the pyenv environment was broken, so fix it below

pyenv rehash

Could not find the Qt platform plugin "cocoa" in "" It was said that the opencv environment was broken, so fix it below

pip3 install opencv-contrib-python==4.1.2.30

Implementation

qr read


def read_qr():
  cap = cv2.VideoCapture(1)
  while (True):
    ret, frame = cap.read()
    ret, img_thresh_color = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
    img_gray = cv2.cvtColor(img_thresh_color, cv2.COLOR_BGR2GRAY)
    th, img_gray_thresh = cv2.threshold(img_gray, 200, 255, cv2.THRESH_BINARY)
    data = decode(img_gray_thresh)
    if len(data) > 0:
      line=data[0][0].decode('utf-8', 'ignore')
      print(line)
      line_sp = re.split(':|;', line)
      ssid = line_sp[2]
      password = line_sp[4]
      cv2.destroyAllWindows()
      return ssid,password
      
    # Display the resulting frame
    cv2.imshow('img_gray_thresh', img_gray_thresh)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

wifi connection


def change_wifi_spot(ssid, password):
  try:
    cmd = "networksetup -setairportnetwork en0 " + ssid + " " + password
    tag_register_process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
    tag_register_out, _ = tag_register_process.communicate()
    tag_register_out_decode = tag_register_out.decode('utf-8')
    print(tag_register_out_decode)
    if "Could not find network" in tag_register_out_decode:
      return {"error": "Could not find network"}
    return {"success": True}
  except:
    error_info = str(sys.exc_info()[1])
    return {"error": str(sys.exc_info()[0]) + error_info}

apn settings


def setting_apn():
  options = Options()
  # options.add_argument('--headless')
  driver = webdriver.Chrome("./chromedriver", chrome_options=options)

  driver.get(WIFI_URL)
  time.sleep(3)
  driver.find_element_by_id("ipt_admin_password").send_keys("admin")
  time.sleep(1)
  
  driver.find_element_by_id('login_ok').click()
  time.sleep(3)

  driver.find_element_by_id('lg_device_simple').click()
  time.sleep(3)

  iframe = driver.find_element_by_id('mainframe')
  driver.switch_to_frame(iframe)

  element=driver.find_element_by_id("net_connect_profile_name")
  element.clear()
  element.send_keys("sora")
  element=driver.find_element_by_id("net_connect_user_name")
  element.clear()
  element.send_keys("sora")

  element=driver.find_element_by_id("net_connect_password")
  element.clear()
  element.send_keys("sora")

  element=driver.find_element_by_id("net_connect_apn")
  element.clear()
  element.send_keys("soracom.io")
  time.sleep(2)
  driver.find_element_by_id('sp_next').click()
  time.sleep(2)
  #You can change the ssid and password here
  driver.find_element_by_id('sp_next').click()
  time.sleep(2)
  driver.find_element_by_id('sp_skip').click()
  time.sleep(2)
  driver.find_element_by_id('sp_complete').click()
  time.sleep(120)

main.py


change_wifi_spot(ssid,password)
time.sleep(30)
setting_apn()

result

The following is the actual video at 4x speed (the screen stops for about 10 seconds in the middle is the sleep processing time waiting for wifi switching) output2.gif

Recommended Posts

Pocket wifi sim settings automatic tool