Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython wifi联网获取天气数据


#import run


import urequests as requests

import json

import network                   # Wi-Fi功能所在库

import utime                     # 延时API所在组件

import ujson                     # json字串解析库


wlan = None


# Wi-Fi SSID和Password设置

wifiSsid = "NBWIFI"

wifiPassword = "z7758521"


# 等待Wi-Fi成功连接到路由器

def get_wifi_status():

    global wlan

    wifi_connected = False


    wlan.active(True)                  #激活界面

    wlan.scan()                        #扫描接入点

    print("start to connect ", wifiSsid)

    wlan.connect(wifiSsid, wifiPassword)       # 连接到指定的路由器(路由器名称为wifiSsid, 密码为:wifiPassword)


    while True:

        wifi_connected = wlan.isconnected()    # 获取Wi-Fi连接路由器的状态信息

        if wifi_connected:                     # Wi-Fi连接成功则退出while循环

            break

        else:

            utime.sleep(0.5)

            print("wifi_connected:", wifi_connected)


    ifconfig = wlan.ifconfig()                    #获取接口的IP/netmask/gw/DNS地址

    print(ifconfig)

    utime.sleep(0.5)



class weather(object):

    # 获取天气信息的uri

    weather_uri = "http://wthrcdn.etouch.cn/weather_mini?citykey=101030300"

    def get(self,uri=weather_uri):

        resp=requests.get(self.weather_uri)

        if (resp.status_code==200):

            a=gzip.decompress(resp.content)

            print(a)

            b=a.decode("utf-8")

            print(b)

            data = json.loads(b)

            print(b)

            w_lei=str(data['data']['forecast'][0]['type'],'utf-8')

            w_di=str(data['data']['forecast'][0]['low'],'utf-8')

            w_gao=str(data['data']['forecast'][0]['high'],'utf-8')

            w_fx=str(data['data']['forecast'][0]['fengxiang'],'utf-8')

            temp=str(data['data']['forecast'][0]['fengli'],'utf-8')

            print(temp)

            temp1=temp.split("[")[2]

            print(temp1)

            temp2=temp1.split("]")[0]

            w_fl=temp2

            w_rq=str(data['data']['forecast'][0]['date'],'utf-8')

            print(w_rq)

            return(w_rq,w_lei,w_di,w_gao,w_fx,w_fl)

        

        

        



            

if __name__ == "__main__":

    wlan = network.WLAN(network.STA_IF)    #创建WLAN对象

    get_wifi_status()

    print("wifi")

    weather = weather()

    print(weather.get())



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.010374069213867 seconds