28_internetTimeClock.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : en
  4. import os
  5. from maix import display
  6. from maix import image
  7. from maix import camera
  8. import sys
  9. sys.path.append('/root/')
  10. import time
  11. import http.client
  12. import json
  13. import requests
  14. import bs4
  15. from datetime import datetime
  16. from CocoPi import AHT20
  17. def getNetworkDate_noexit():
  18. global getDateNum
  19. try:
  20. coon = http.client.HTTPConnection('www.baidu.com')
  21. coon.request("GET","/")
  22. r = coon.getresponse()
  23. ts = r.getheader('date')
  24. GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
  25. BeiJing_time = time.localtime(time.mktime(GMT_time) + 8*60*60)
  26. format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
  27. command = "date -s "+"\"{}\"".format(format_time)
  28. os.system(command)
  29. getDateNum = 1
  30. # sys.exit()
  31. except:
  32. pass
  33. camera.camera.config(size=(320,240))
  34. ScreenOrientation = False
  35. image.load_freetype("/root/preset/fonts/DigitalNumbers-Regular.ttf")
  36. def getNetworkDate():
  37. try:
  38. coon = http.client.HTTPConnection('www.baidu.com')
  39. coon.request("GET","/")
  40. r = coon.getresponse()
  41. ts = r.getheader('date')
  42. GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
  43. BeiJing_time = time.localtime(time.mktime(GMT_time)+ 8*60*60)
  44. format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
  45. command = "date -s "+"\"{}\"".format(format_time)
  46. os.system(command)
  47. sys.exit()
  48. except:
  49. pass
  50. def get_html(url):
  51. '''
  52. 封装请求
  53. '''
  54. headers = {
  55. 'User-Agent':
  56. 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
  57. 'ContentType':
  58. 'text/html; charset=utf-8',
  59. 'Accept-Encoding':
  60. 'gzip, deflate, sdch',
  61. 'Accept-Language':
  62. 'zh-CN,zh;q=0.8',
  63. 'Connection':
  64. 'keep-alive',
  65. }
  66. try:
  67. htmlcontet = requests.get(url, headers=headers, timeout=30)
  68. htmlcontet.raise_for_status()
  69. htmlcontet.encoding = 'utf-8'
  70. return htmlcontet.text
  71. except:
  72. return " 请求失败 "
  73. def get_content(url):
  74. '''
  75. 抓取页面天气数据
  76. '''
  77. weather_list = []
  78. html = get_html(url)
  79. soup = bs4.BeautifulSoup(html, 'lxml')
  80. content_ul = soup.find('div', class_='t').find('ul', class_='clearfix').find_all('li')
  81. for content in content_ul:
  82. try:
  83. weather = {}
  84. weather['day'] = content.find('h1').text
  85. weather['temperature'] = content.find(
  86. 'p', class_='tem').span.text + content.find(
  87. 'p', class_='tem').em.text
  88. weather_list.append(weather)
  89. except:
  90. print('查询不到')
  91. print(weather_list)
  92. return weather_list
  93. def getCurrent_data(type):
  94. now = datetime.now()
  95. return now.strftime("%"+type+"")
  96. aht20 = AHT20(2)
  97. def screenShow(inputImg,rotationAngle):
  98. #global img_drop
  99. if rotationAngle==90 or rotationAngle == 270:
  100. screenCapture=inputImg.crop(0,0,240,320)
  101. else:
  102. screenCapture=inputImg.crop(0,0,320,240)
  103. canvas_screenShow = screenCapture.rotate(-rotationAngle,adjust=1)
  104. display.show(canvas_screenShow)
  105. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  106. canvas.draw_string(8 , 100 , "Checking Wi-Fi connection", scale = 1, color = (209,72,54), thickness = 1)
  107. display.show(canvas)
  108. #os.system("wifi_disconnect_ap_test")
  109. #os.system('wifi_connect_chinese_ap_test "CocoRobo_SZ" "cocorobo2019"')
  110. #CLIENT = ntplib.NTPClient()
  111. #RESPONSE = CLIENT.request('127.0.0.1')
  112. getNetworkDate_noexit()
  113. canvas = image.new(size = (320, 240))
  114. getNetworkDate()
  115. weather = get_content('http://www.weather.com.cn/weather1d/101280601.shtml')
  116. while True:
  117. time.sleep(1)
  118. canvas.clear()
  119. canvas.draw_string(10,10, (''.join([str(x) for x in [getCurrent_data("Y"), "-", getCurrent_data("m"), "-", getCurrent_data("d"), " "]])), scale = 2, color = (111,195,223) , thickness = 1)
  120. canvas.draw_string(25,85, (''.join([str(x) for x in [getCurrent_data("H"), "-", getCurrent_data("M"), "-", getCurrent_data("S")]])), scale = 3, color = (111,195,223) , thickness = 1)
  121. canvas.draw_string(190,190, str((str(int((aht20.get_humidity()))))) + str("%RH"), scale = 2, color = (111,195,223) , thickness = 1)
  122. canvas.draw_string(30,190, (str(weather[1].get("temperature"))), scale = 2, color = (111,195,223) , thickness = 1)
  123. screenShow(canvas,0)