Last active
July 17, 2022 09:02
Check tello wifi connection and get current battery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import time | |
import pywifi | |
from pywifi import const | |
import socket | |
import threading | |
""" | |
Global variable | |
""" | |
# Setting tello SSID | |
targetSSID = 'TELLO-A0502E' | |
# UDP socket setting | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
sock.bind(('', 9000)) | |
def main(): | |
# WiFi connection | |
result = checkWiFi() | |
if(result): | |
print('connected!') | |
# Create socket thread listening 9000 PORT form tello | |
recvThread = threading.Thread(target=recv) | |
recvThread.start() | |
# send command message to tello by UDP socket | |
sock.sendto(("command").encode(encoding='utf-8'), ('192.168.10.1', 8889)) | |
sock.sendto(("battery?").encode(encoding='utf-8'), ('192.168.10.1', 8889)) | |
time.sleep(1) | |
sock.close() | |
else: | |
print('connect failed!') | |
""" | |
WiFi connection | |
""" | |
def checkWiFi(): | |
wifi_SSID = get_wifi_info_win()["SSID"] | |
print(f"[1] Current WiFi Network: {wifi_SSID}") | |
if(wifi_SSID != targetSSID): | |
# 獲取網卡介面 | |
wifi = pywifi.PyWiFi() | |
# 得到第一個無線網卡 | |
iface = wifi.interfaces()[0] | |
iface.disconnect() | |
# create wifi connection | |
profile = pywifi.Profile() | |
profile.ssid = targetSSID | |
profile.auth = const.AUTH_ALG_OPEN | |
profile.akm.append(const.AKM_TYPE_WPA2PSK) | |
profile.cipher = const.CIPHER_TYPE_CCMP | |
profile.key = '' # password | |
# 洗掉已連接的所有wifi檔案 | |
iface.remove_all_network_profiles() | |
# 加載新的wifi連接檔案 | |
tep_profile = iface.add_network_profile(profile) | |
# 連接上面的wifi檔案 | |
iface.connect(tep_profile) | |
time.sleep(1) | |
# 如果wifi已連接 | |
wifi_SSID = get_wifi_info_win()["SSID"] | |
print(f"[2] Current WiFi Network: {wifi_SSID}") | |
if(wifi_SSID != targetSSID): | |
return False | |
else: | |
return True | |
else: | |
return True | |
""" | |
Threading listening 9000 PORT form tello | |
""" | |
def recv(): | |
while True: | |
try: | |
data, server = sock.recvfrom(1024) | |
print("{} : {}".format(server, data.decode(encoding="utf-8"))) | |
except Exception: | |
print('\nExit . . .\n') | |
sock.close() | |
break | |
""" | |
Get Wifi information on Mac OSx | |
""" | |
def get_wifi_info_mac(): | |
process = subprocess.Popen(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport','-I'], stdout=subprocess.PIPE) | |
out, err = process.communicate() | |
process.wait() | |
wifi_info = {} | |
for line in out.decode("utf-8").split("\n"): | |
if ": " in line: | |
key, val = line.split(": ") | |
key = key.replace(" ", "") | |
val = val.strip() | |
wifi_info[key] = val | |
return wifi_info | |
""" | |
Get Wifi information on windows | |
""" | |
def get_wifi_info_win(): | |
out = subprocess.check_output('netsh wlan show interfaces') | |
wifi_info = {} | |
for line in out.decode("utf-8", errors="ignore").split("\n"): | |
if ": " in line: | |
key, val = line.split(": ") | |
key = key.replace(" ", "") | |
val = val.strip() | |
wifi_info[key] = val | |
return wifi_info | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dependency
pywifi provides a cross-platform Python module for manipulating wireless interfaces.
install packages: pywifi