#!/usr/bin/env python #version : 2023.04.04 #language : en import os from maix import display from maix import image from maix import camera import sys sys.path.append('/root/') import time import http.client import json import requests import bs4 from datetime import datetime from CocoPi import AHT20 def getNetworkDate_noexit(): global getDateNum try: coon = http.client.HTTPConnection('www.baidu.com') coon.request("GET","/") r = coon.getresponse() ts = r.getheader('date') GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S") BeiJing_time = time.localtime(time.mktime(GMT_time) + 8*60*60) format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time) command = "date -s "+"\"{}\"".format(format_time) os.system(command) getDateNum = 1 # sys.exit() except: pass camera.camera.config(size=(320,240)) ScreenOrientation = False image.load_freetype("/root/preset/fonts/DigitalNumbers-Regular.ttf") def getNetworkDate(): try: coon = http.client.HTTPConnection('www.baidu.com') coon.request("GET","/") r = coon.getresponse() ts = r.getheader('date') GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S") BeiJing_time = time.localtime(time.mktime(GMT_time)+ 8*60*60) format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time) command = "date -s "+"\"{}\"".format(format_time) os.system(command) sys.exit() except: pass def get_html(url): ''' 封装请求 ''' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', 'ContentType': 'text/html; charset=utf-8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', } try: htmlcontet = requests.get(url, headers=headers, timeout=30) htmlcontet.raise_for_status() htmlcontet.encoding = 'utf-8' return htmlcontet.text except: return " 请求失败 " def get_content(url): ''' 抓取页面天气数据 ''' weather_list = [] html = get_html(url) soup = bs4.BeautifulSoup(html, 'lxml') content_ul = soup.find('div', class_='t').find('ul', class_='clearfix').find_all('li') for content in content_ul: try: weather = {} weather['day'] = content.find('h1').text weather['temperature'] = content.find( 'p', class_='tem').span.text + content.find( 'p', class_='tem').em.text weather_list.append(weather) except: print('查询不到') print(weather_list) return weather_list def getCurrent_data(type): now = datetime.now() return now.strftime("%"+type+"") aht20 = AHT20(2) def screenShow(inputImg,rotationAngle): #global img_drop if rotationAngle==90 or rotationAngle == 270: screenCapture=inputImg.crop(0,0,240,320) else: screenCapture=inputImg.crop(0,0,320,240) canvas_screenShow = screenCapture.rotate(-rotationAngle,adjust=1) display.show(canvas_screenShow) canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB") canvas.draw_string(8 , 100 , "Checking Wi-Fi connection", scale = 1, color = (209,72,54), thickness = 1) display.show(canvas) #os.system("wifi_disconnect_ap_test") #os.system('wifi_connect_chinese_ap_test "CocoRobo_SZ" "cocorobo2019"') #CLIENT = ntplib.NTPClient() #RESPONSE = CLIENT.request('127.0.0.1') getNetworkDate_noexit() canvas = image.new(size = (320, 240)) getNetworkDate() weather = get_content('http://www.weather.com.cn/weather1d/101280601.shtml') while True: time.sleep(1) canvas.clear() 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) 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) canvas.draw_string(190,190, str((str(int((aht20.get_humidity()))))) + str("%RH"), scale = 2, color = (111,195,223) , thickness = 1) canvas.draw_string(30,190, (str(weather[1].get("temperature"))), scale = 2, color = (111,195,223) , thickness = 1) screenShow(canvas,0)