from machine import Pin, SPI, UART,I2C
import urequests as request
import ure as re
import time
import _thread
import network,sys
bili_fans_api_url='''http://api.bilibili.com/x/relation/stat?vmid=1875597160'''
header={
'Accept': 'application/json; charset=utf-8',
'Connection': 'keep-alive',
'Cookie': '__gads=ID=e304bb25403c8bcb:T=1587393632:S=ALNI_MY84hw6en9jJfoI6-mnZri7sRR-sg; _ga=GA1.2.609307016.1587393632',
'Host': 'api.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
def get_ncov():
try:
print('Get_ncov')
print(bili_fans_api_url)
r = request.get(bili_fans_api_url,headers=header)#
print('Get ok')
print(r.text)
#content = r.text()
except:
print('error')
ip_address =""
def wifi_connect():
wifi_led=Pin(12,Pin.OUT) # 板载指示灯初始化
wlan = network.WLAN(network.STA_IF) # 以工作站 (wlan) 模式运行,需要创建一个工作站Wi-Fi接口的实例
wlan.active(True) # 在工作站对象上调用激活方法并以True作为输入值传递来激活网络接口
start_time=time.time() # 记录开始时间
if not wlan.isconnected(): # 如果尚未联网成功
print("当前无线未联网,正在连接中....")
wlan.connect("NBWIFI", "z7758521") # 无线网SSID、密码,开始联网 WIFI_Connect("NBWIFI","z7758521")
while not wlan.isconnected(): # 如果还未连接成功,则LED灯闪烁提示
wifi_led.value(1)
time.sleep_ms(1000)
wifi_led.value(1)
time.sleep_ms(1000)
print("正在尝试连接到wifi....")
print(time.time())
if time.time()-start_time>15: # 如果超过15秒还不行,就退出
print("连接失败!!!无线网连接超过15秒,请检查无线网名称和密码是否正确..")
break
if wlan.isconnected(): # 如果联接成功
wifi_led.value(1) # LED灯常亮
IP_info=wlan.ifconfig()
print("无线网已经连接,信息如下:")
print("IP地址:"+IP_info[0])
print("子网掩码:"+IP_info[1])
print("网关:"+IP_info[2])
print("DNS:"+IP_info[3])
global ip_address
ip_address = wlan.ifconfig()[0]
print("ip_address "+ip_address)
print("Network Connecting...")
#开始执行联网模块
wifi_connect()
while True:
get_ncov()
time.sleep_ms(15000)