全部mpy增加心知天气、网络请求、ds18x20
This commit is contained in:
65
boards/default_src/micropython/origin/build/lib/ds18b20.py
Normal file
65
boards/default_src/micropython/origin/build/lib/ds18b20.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# DS18x20 temperature sensor driver for MicroPython.
|
||||
# MIT license; Copyright (c) 2016 Damien P. George
|
||||
|
||||
import onewire
|
||||
from machine import Pin
|
||||
from micropython import const
|
||||
|
||||
_CONVERT = const(0x44)
|
||||
_RD_SCRATCH = const(0xBE)
|
||||
_WR_SCRATCH = const(0x4E)
|
||||
|
||||
class DS18X20:
|
||||
__species = {}
|
||||
__first_init = True
|
||||
def __new__(cls, pin, *args, **kwargs):
|
||||
if pin not in cls.__species.keys():
|
||||
cls.__first_init = True
|
||||
cls.__species[pin] = object.__new__(cls)
|
||||
return cls.__species[pin]
|
||||
|
||||
def __init__(self, pin):
|
||||
if self.__first_init:
|
||||
self.__first_init = False
|
||||
self._ow = onewire.OneWire(Pin(pin, pull=Pin.PULL_UP))
|
||||
self._buf = bytearray(9)
|
||||
self._roms = self.scan()
|
||||
|
||||
def scan(self):
|
||||
return [rom for rom in self._ow.scan() if rom[0] in (0x10, 0x22, 0x28)]
|
||||
|
||||
def convert_temp(self):
|
||||
self._ow.reset(True)
|
||||
self._ow.writebyte(self._ow.SKIP_ROM)
|
||||
self._ow.writebyte(_CONVERT)
|
||||
|
||||
def read_scratch(self, rom):
|
||||
self._ow.reset(True)
|
||||
self._ow.select_rom(rom)
|
||||
self._ow.writebyte(_RD_SCRATCH)
|
||||
self._ow.readinto(self._buf)
|
||||
if self._ow.crc8(self._buf):
|
||||
raise Exception("CRC error")
|
||||
return self._buf
|
||||
|
||||
def read_temp(self, rom):
|
||||
buf = self.read_scratch(rom)
|
||||
if rom[0] == 0x10:
|
||||
if buf[1]:
|
||||
t = buf[0] >> 1 | 0x80
|
||||
t = -((~t + 1) & 0xFF)
|
||||
else:
|
||||
t = buf[0] >> 1
|
||||
return t - 0.25 + (buf[7] - buf[6]) / buf[7]
|
||||
else:
|
||||
t = buf[1] << 8 | buf[0]
|
||||
if t & 0x8000: # sign bit set
|
||||
t = -((t ^ 0xFFFF) + 1)
|
||||
return t / 16
|
||||
|
||||
def temperature(self):
|
||||
temp = []
|
||||
self.convert_temp()
|
||||
for rom in self._roms:
|
||||
temp.append(round(self.read_temp(rom), 2))
|
||||
return temp [0] if len(temp) == 1 else tuple(temp)
|
||||
@@ -20,115 +20,198 @@ _weather_alarm="http://api.seniverse.com/v3/weather/alarm.json?" #气象灾害
|
||||
_life_suggestion="http://api.seniverse.com/v3/life/suggestion.json?" #生活指数
|
||||
_air_now="http://api.seniverse.com/v3/air/now.json?" #空气质量实况
|
||||
_air_daily="http://api.seniverse.com/v3/air/daily.json?" #逐日空气质量预报
|
||||
_tide_daily="http://api.seniverse.com/v3/tide/daily.json?" #逐小时潮汐预报
|
||||
_air_hourly="http://api.seniverse.com/v3/air/hourly.json?" #逐时空气质量预报
|
||||
_tide_daily="http://api.seniverse.com/v3/tide/daily.json?" #逐时潮汐预报
|
||||
_geo_sun="http://api.seniverse.com/v3/geo/sun.json?" #日出日落
|
||||
_geo_moon="http://api.seniverse.com/v3/geo/moon.json?" #月出月落和月相
|
||||
_location_search="http://api.seniverse.com/v3/location/search.json?" #城市搜索
|
||||
|
||||
#数据请求
|
||||
def _urequests_api(url):
|
||||
try:
|
||||
results=json.loads(urequests.post(url).text)
|
||||
except Exception as e:
|
||||
raise RuntimeError("API request failed or WiFi is not connected",e)
|
||||
|
||||
if "status" in results.keys():
|
||||
raise ValueError(results["status"])
|
||||
if "results" in results.keys():
|
||||
return results["results"]
|
||||
|
||||
#天气实况 https://docs.seniverse.com/api/weather/now.html
|
||||
def weather_now(key,location):
|
||||
url="{}key={}&location={}".format(_weather_now,key,location)
|
||||
results=_urequests_api(url)[0]
|
||||
return results['now']
|
||||
class API_BASE:
|
||||
_results = None
|
||||
def _urequests_api(self, url):
|
||||
try:
|
||||
results=json.loads(urequests.post(url).text)
|
||||
except Exception as e:
|
||||
raise RuntimeError("API request failed or WiFi is not connected",e)
|
||||
|
||||
if "status" in results.keys():
|
||||
raise ValueError(results["status"])
|
||||
if "results" in results.keys():
|
||||
return results["results"]
|
||||
|
||||
#天气实况 https://docs.seniverse.com/api/weather/now.html
|
||||
class Weather_now(API_BASE):
|
||||
def request(self, key, location):
|
||||
url = "{}key={}&location={}".format(_weather_now, key, location)
|
||||
self._results = self._urequests_api(url)[0]['now']
|
||||
|
||||
def analysis(self, key=None):
|
||||
if key is None:
|
||||
return self._results
|
||||
else:
|
||||
return self._results[key]
|
||||
|
||||
Weather_now = Weather_now()
|
||||
|
||||
#逐日天气预报 https://docs.seniverse.com/api/weather/daily.html
|
||||
def weather_daily(key,location,days=1):
|
||||
url="{}key={}&location={}&days={}".format(_weather_daily,key,location,days)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['daily'])):
|
||||
data.append(results['daily'][i])
|
||||
return tuple(data)
|
||||
class Weather_daily(API_BASE):
|
||||
def request(self, key, location, days=1):
|
||||
url = "{}key={}&location={}&days={}".format(_weather_daily, key, location, days)
|
||||
self._results = self._urequests_api(url)[0]['daily']
|
||||
|
||||
def analysis(self, days=1, key=None):
|
||||
if key is None:
|
||||
return self._results[days]
|
||||
else:
|
||||
return self._results[days][key]
|
||||
|
||||
Weather_daily = Weather_daily()
|
||||
|
||||
#逐时天气预报 https://docs.seniverse.com/api/weather/hourly.html
|
||||
def weather_hourly(key,location,hours=1):
|
||||
url="{}key={}&location={}&hours={}".format(_weather_hourly,key,location,hours)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['hourly'])):
|
||||
data.append(results['hourly'][i])
|
||||
return tuple(data)
|
||||
class Weather_hourly(API_BASE):
|
||||
def request(self, key, location, hours=1):
|
||||
url = "{}key={}&location={}&hours={}".format(_weather_hourly, key, location, hours)
|
||||
self._results = self._urequests_api(url)[0]['hourly']
|
||||
|
||||
#气象灾害预警 https://docs.seniverse.com/api/weather/alarm.html
|
||||
def weather_alarm(key,location):
|
||||
url="{}key={}&location={}".format(_weather_alarm,key,location)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['alarms'])):
|
||||
data.append(results['alarms'][i])
|
||||
return tuple(data)
|
||||
def analysis(self, hours=1, key=None):
|
||||
if key is None:
|
||||
return self._results[hours]
|
||||
else:
|
||||
return self._results[hours][key]
|
||||
|
||||
#生活指数 https://docs.seniverse.com/api/life/suggestion.html
|
||||
def life_suggestion(key,location,days=1):
|
||||
url="{}key={}&location={}&days={}".format(_life_suggestion,key,location,days)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['suggestion'])):
|
||||
data.append(results['suggestion'][i])
|
||||
return tuple(data)
|
||||
#Weather_hourly = Weather_hourly() #暂不开启
|
||||
|
||||
#空气质量实况 https://docs.seniverse.com/api/air/now.html
|
||||
def air_now(key,location):
|
||||
url="{}key={}&location={}&scope=city".format(_air_now,key,location)
|
||||
results=_urequests_api(url)[0]
|
||||
return results['air']['city']
|
||||
|
||||
#逐日空气质量预报 https://docs.seniverse.com/api/air/daily5d.html
|
||||
def air_daily(key,location,days=1):
|
||||
url="{}key={}&location={}&days={}".format(_air_daily,key,location,days)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['daily'])):
|
||||
data.append(results['daily'][i])
|
||||
return tuple(data)
|
||||
class Air_now(API_BASE):
|
||||
def request(self, key, location):
|
||||
url = "{}key={}&location={}&scope=city".format(_air_now, key, location)
|
||||
self._results = self._urequests_api(url)[0]['air']['city']
|
||||
|
||||
#逐时潮汐预报 https://docs.seniverse.com/api/ocean/tide.html
|
||||
def tide_daily(key,location):
|
||||
url="{}key={}&location={}&days=1".format(_tide_daily,key,location)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['ports'])):
|
||||
data.append({'port':results['ports'][i]['port'],
|
||||
'tide':results['ports'][i]['data'][0]['tide'],
|
||||
'range':results['ports'][i]['data'][0]['range']})
|
||||
return tuple(data)
|
||||
def analysis(self, key=None):
|
||||
if key is None:
|
||||
return self._results
|
||||
else:
|
||||
return self._results[key]
|
||||
|
||||
Air_now = Air_now()
|
||||
|
||||
#逐日空气质量预报 https://docs.seniverse.com/api/air/daily5d.html
|
||||
class Air_daily(API_BASE):
|
||||
def request(self, key, location, days=1):
|
||||
url = "{}key={}&location={}&days={}".format(_air_daily, key, location, days)
|
||||
self._results = self._urequests_api(url)[0]['daily']
|
||||
|
||||
def analysis(self, days=1, key=None):
|
||||
if key is None:
|
||||
return self._results[days]
|
||||
else:
|
||||
return self._results[days][key]
|
||||
|
||||
Air_daily= Air_daily()
|
||||
|
||||
#逐时空气质量预报 https://docs.seniverse.com/api/air/hourly5d.html
|
||||
class Air_hourly(API_BASE):
|
||||
def request(self, key, location, hours=1):
|
||||
url = "{}key={}&location={}&hours={}&days=1".format(_air_hourly, key, location, hours)
|
||||
self._results = self._urequests_api(url)[0]['hourly']
|
||||
|
||||
def analysis(self, hours=1, key=None):
|
||||
if key is None:
|
||||
return self._results[hours]
|
||||
else:
|
||||
return self._results[hours][key]
|
||||
|
||||
#Air_hourly = Air_hourly() #暂不开启
|
||||
|
||||
#气象灾害预警 https://docs.seniverse.com/api/weather/alarm.html
|
||||
class Weather_alarm(API_BASE):
|
||||
def request(self, key, location):
|
||||
url = "{}key={}&location={}".format(_weather_alarm, key, location)
|
||||
results = self._urequests_api(url)[0]['alarms']
|
||||
self._results = results[0] if results else {}
|
||||
|
||||
def analysis(self, key=None):
|
||||
if key is None:
|
||||
return self._results
|
||||
if key in self._results.keys():
|
||||
return self._results[key]
|
||||
|
||||
Weather_alarm = Weather_alarm()
|
||||
|
||||
#生活指数 https://docs.seniverse.com/api/life/suggestion.html
|
||||
class Life_suggestion(API_BASE):
|
||||
def request(self, key, location):
|
||||
url = "{}key={}&location={}".format(_life_suggestion, key, location)
|
||||
self._results = self._urequests_api(url)[0]['suggestion']
|
||||
|
||||
def analysis(self, key=None, brief=False):
|
||||
if key is None:
|
||||
return self._results
|
||||
else:
|
||||
return self._results[key]['brief'] if brief else self._results[key]['details']
|
||||
|
||||
Life_suggestion = Life_suggestion()
|
||||
|
||||
#24时潮汐预报 https://docs.seniverse.com/api/ocean/tide.html
|
||||
class Tide_daily(API_BASE):
|
||||
def request(self, key, location):
|
||||
url = "{}key={}&location={}&days=1".format(_tide_daily, key, location)
|
||||
self._results = self._urequests_api(url)[0]['ports'][0]['data'][0]
|
||||
|
||||
def analysis(self, key=None):
|
||||
if key is None:
|
||||
return self._results
|
||||
else:
|
||||
key = key.split(',')
|
||||
if len(key) == 1:
|
||||
return self._results[key[0]]
|
||||
if len(key) == 2:
|
||||
return self._results['range'][int(key[0])][key[1]]
|
||||
|
||||
Tide_daily = Tide_daily()
|
||||
|
||||
#日出日落 https://docs.seniverse.com/api/geo/sun.html
|
||||
def geo_sun(key,location,days=1):
|
||||
url="{}key={}&location={}&days={}".format(_geo_sun,key,location,days)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['sun'])):
|
||||
data.append(results['sun'][i])
|
||||
return tuple(data)
|
||||
|
||||
class Geo_sun(API_BASE):
|
||||
def request(self, key, location, days=1):
|
||||
url = "{}key={}&location={}&days={}".format(_geo_sun, key, location, days)
|
||||
self._results = self._urequests_api(url)[0]['sun']
|
||||
|
||||
def analysis(self, days=1, key=None):
|
||||
if key is None:
|
||||
return self._results[days]
|
||||
else:
|
||||
return self._results[days][key]
|
||||
|
||||
Geo_sun = Geo_sun()
|
||||
|
||||
#月出月落和月相 https://docs.seniverse.com/api/geo/moon.html
|
||||
def geo_moon(key,location,days=1):
|
||||
url="{}key={}&location={}&days={}".format(_geo_moon,key,location,days)
|
||||
results=_urequests_api(url)[0]
|
||||
data=[]
|
||||
for i in range(len(results['moon'])):
|
||||
data.append(results['moon'][i])
|
||||
return tuple(data)
|
||||
|
||||
class Geo_moon(API_BASE):
|
||||
def request(self, key, location, days=1):
|
||||
url = "{}key={}&location={}&days={}".format(_geo_moon, key, location, days)
|
||||
self._results = self._urequests_api(url)[0]['moon']
|
||||
|
||||
def analysis(self, days=1, key=None):
|
||||
if key is None:
|
||||
return self._results[days]
|
||||
else:
|
||||
return self._results[days][key]
|
||||
|
||||
Geo_moon = Geo_moon()
|
||||
|
||||
#城市搜索 https://docs.seniverse.com/api/fct/search.html
|
||||
def location_search(key,location):
|
||||
url="{}key={}&q={}&limit=50".format(_location_search,key,location)
|
||||
results=_urequests_api(url)
|
||||
data=[]
|
||||
for i in range(len(results)):
|
||||
data.append(results[i])
|
||||
return tuple(data)
|
||||
class Location_search(API_BASE):
|
||||
def request(self, key, location):
|
||||
url = "{}key={}&q={}&limit=50".format(_location_search, key, location)
|
||||
results = self._urequests_api(url)
|
||||
self._results = results[0] if results else {}
|
||||
|
||||
def analysis(self, key=None):
|
||||
if key is None:
|
||||
return self._results
|
||||
else:
|
||||
if key in self._results.keys():
|
||||
return self._results[key]
|
||||
|
||||
Location_search = Location_search()
|
||||
|
||||
Reference in New Issue
Block a user