4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ポケットwifi sim設定 自動ツール

Last updated at Posted at 2020-12-31

目的

大量のpocket wifiを自動で設定する必要があるため、pocket wifiのapn設定を自動化する。
動作としてはqr読んでwifi設定取得、wifi自動接続、seleniumでwifiのlocalホストにはいりapn書き換え。

環境

mac catalina
soracom sim plan-k
fs030w (auとdocomo回線のsimが動くポケットwifiのためfs030wを使用。(Teltonikaは自分の環境だと不安定だったので、お勧めしないです。))

事前準備

qr読み込み

下記を参考にzbarのinstall
Python3で複数のQRコードを読み取る

brew install zbar

上記でえらったのでここを参考に修正

brew link zbar

libexec/pyenv: No such file or directory
とか言われpyenv環境壊れたので下記で修正

pyenv rehash

Could not find the Qt platform plugin "cocoa" in ""
とかいわれて、opencv環境壊れてたので、下記で修正

pip3 install opencv-contrib-python==4.1.2.30

実装

qr読み込み
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接続
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設定
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)
  # ここでssidやパスワードかえれる
  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()

結果

下記は4倍速での実際の動画(途中で10秒ほど画面止まるのはwifi切り替え待ちのsleep処理時間)
output2.gif

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?