24_connectWiFi.py 8.4 KB

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