24_connectWiFi.py 8.4 KB

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