24_connectWiFi.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from maix import camera, display, zbar, image
  5. import socket
  6. import os
  7. import sys
  8. sys.path.append('/root/')
  9. import http.client
  10. from CocoPi import BUTTON
  11. import time
  12. import ssl
  13. ssl._create_default_https_context = ssl._create_unverified_context
  14. key_A = BUTTON(14)
  15. key_B = BUTTON(8)
  16. key_C = BUTTON(13)
  17. key_D = BUTTON(7)
  18. ScreenOrientation = False
  19. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  20. camera.camera.config(size=(320,240))
  21. def lcdRotation(inputImg,rotationAngle):
  22. from maix import image
  23. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  24. if ScreenOrientation:
  25. imgRotationAim = image.new(size = (240, 320))
  26. else:
  27. imgRotationAim = image.new(size = (320, 240))
  28. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  29. def getPublicIp():
  30. import requests
  31. url = 'https://myip.ipip.net'
  32. res = requests.get(url,verify=False).text.split(" ")
  33. return res[1][3:]
  34. if ScreenOrientation:
  35. CAMERAROTATE = +180
  36. else:
  37. CAMERAROTATE = +90
  38. def getNetworkDate_noexit():
  39. global getDateNum
  40. try:
  41. coon = http.client.HTTPConnection('www.baidu.com')
  42. coon.request("GET","/")
  43. r = coon.getresponse()
  44. ts = r.getheader('date')
  45. GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
  46. BeiJing_time = time.localtime(time.mktime(GMT_time) + 8*60*60)
  47. format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
  48. command = "date -s "+"\"{}\"".format(format_time)
  49. os.system(command)
  50. getDateNum = 1
  51. # sys.exit()
  52. except:
  53. pass
  54. def getWifiConnectState():
  55. cmd = "wifi_get_connection_info_test 1"
  56. res = os.popen(cmd).read()
  57. wifiInfo = {}
  58. if res.find('get connection infomation successfully!') != -1:
  59. wifiInfo["state"]=True
  60. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  61. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  62. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  63. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  64. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  65. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  66. else:
  67. wifiInfo["state"]=False
  68. wifiInfo["AP"] = "N/A"
  69. wifiInfo["IP"] = "N/A"
  70. wifiInfo["frequency"] = "N/A"
  71. wifiInfo["RSSI"] = "N/A"
  72. wifiInfo["link_speed"] = "N/A"
  73. wifiInfo["noise"] = "N/A"
  74. return wifiInfo
  75. def getPrivateIp():
  76. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  77. try:
  78. st.connect(("10.255.255.255",1))
  79. IP = st.getsockname()[0]
  80. except Exception:
  81. IP = "127.0.0.1"
  82. finally:
  83. st.close()
  84. return IP
  85. ssidInfo = ""
  86. passwordInfo = ""
  87. checkConnectState = False
  88. connectText = ""
  89. startConnect=False
  90. wifiConnectState = False
  91. # PublicIp=""
  92. PrivateIP=""
  93. runConnectSig=True
  94. connectSuccessSig=False
  95. while True:
  96. canvas = camera.capture()
  97. IP = getPrivateIp()
  98. if ssidInfo!="" and passwordInfo!="":
  99. startConnect=True
  100. #connectText = "Waitting for Connection..."
  101. #canvas.draw_string(10,40, ssidInfo+" "+str(len(ssidInfo))+" "+str(type(ssidInfo)), scale = 1.5, color = (0,0,0), thickness = 1)
  102. canvas.draw_string(10,50, "正在连接WIFI,请等待...", scale = 1.5, color = (0,0,0), thickness = 1)
  103. display.show(canvas)
  104. if startConnect==True:
  105. canvas_1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  106. canvas.draw_image(canvas_1,0,0, alpha=0.4)
  107. if wifiConnectState == True:
  108. if key_B.is_pressed():
  109. while (key_B.is_pressed() == True):
  110. time.sleep(0.001)
  111. startConnect= False
  112. connectText = ""
  113. checkConnectState = False
  114. connectSuccessSig = False
  115. wifiConnectState = False
  116. runConnectSig== True
  117. ssidInfo=""
  118. passwordInfo=""
  119. if checkConnectState == True:
  120. if connectSuccessSig == False:
  121. # PublicIp=getPublicIp()
  122. PrivateIP=getPrivateIp()
  123. connectSuccessSig = True
  124. connectText = "WiFi连接成功!"
  125. # canvas.draw_string(10,80, "路由器公网IP:" + PublicIp, scale = 1.5, color = (0,0,0), thickness = 1)
  126. canvas.draw_string(10,80, "局域网IP:" + PrivateIP, scale = 1.5, color = (0,0,0), thickness = 1)
  127. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  128. ssidInfo=""
  129. passwordInfo=""
  130. else:
  131. pass
  132. else:
  133. if checkConnectState == False:
  134. os.system("wifi_disconnect_ap_test")
  135. os.system('wifi_connect_chinese_ap_test '+ssidInfo+' '+passwordInfo+'')
  136. wifiConnectState=getWifiConnectState()["state"]
  137. checkConnectState = True
  138. if checkConnectState == True:
  139. if key_B.is_pressed():
  140. while (key_B.is_pressed() == True):
  141. time.sleep(0.001)
  142. startConnect= False
  143. connectText = ""
  144. checkConnectState = False
  145. connectSuccessSig = False
  146. wifiConnectState = False
  147. runConnectSig== True
  148. passwordInfo = ""
  149. ssidInfo = ""
  150. else:
  151. if runConnectSig== True:
  152. connectText = "正在连接WIFI,请等待..."
  153. runConnectSig= False
  154. else:
  155. passwordInfo = ""
  156. ssidInfo = ""
  157. connectText = "WIFI连接失败!"
  158. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  159. canvas.draw_string(10,50, connectText, scale = 1.5, color = (0,0,0) , thickness = 1)
  160. else:
  161. mks = canvas.find_qrcodes()
  162. for mk in mks:
  163. #外框数据
  164. X = mk['x']
  165. Y = mk['y']
  166. W = mk['w']
  167. H = mk['h']
  168. #二维码信息
  169. string = mk['payload']
  170. codeData = string.split(";")
  171. ssidInfo = codeData[0].split(":")[1]
  172. passwordInfo = codeData[1].split(":")[1]
  173. #画外框
  174. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  175. #打印信息
  176. canvas.draw_string(int(X) , int(Y - 45) , "WIFI名:"+ssidInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  177. canvas.draw_string(int(X) , int(Y - 25) , "密码:"+passwordInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  178. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  179. #canvas.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  180. display.show(canvas)