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

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

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

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

Micropython ESP频道

micropython esp32 请求api json解析


import time


try:

  import urequests as requests

except:

  import requests

  

try:

  import ujson as json

except:

  import json


import network


import esp

esp.osdebug(None)


import gc

gc.collect()


ssid = 'REPLACE_WITH_YOUR_SSID'

password = 'REPLACE_WITH_YOUR_PASSWORD'


city = 'REPLACE_WITH_YOUR_CITY_NAME'

country_code = 'REPLACE_WITH_YOUR_2_LETTER_COUNTRY_CODE'

#example

#city = 'Porto'

#country_code = 'PT'


open_weather_map_api_key = 'REPLACE_WITH_YOUR_OPEN_WEATHER_MAP_API_KEY'


station = network.WLAN(network.STA_IF)


station.active(True)

station.connect(ssid, password)


while station.isconnected() == False:

  pass


print('Connection successful')

print(station.ifconfig())


#set your unique OpenWeatherMap.org URL

open_weather_map_url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + ',' + country_code + '&APPID=' + open_weather_map_api_key


weather_data = requests.get(open_weather_map_url)

print(weather_data.json())


# Location (City and Country code)

location = 'Location: ' + weather_data.json().get('name') + ' - ' + weather_data.json().get('sys').get('country')

print(location)


# Weather Description

description = 'Description: ' + weather_data.json().get('weather')[0].get('main')

print(description)


# Temperature

raw_temperature = weather_data.json().get('main').get('temp')-273.15


# Temperature in Celsius

temperature = 'Temperature: ' + str(raw_temperature) + '*C'

#uncomment for temperature in Fahrenheit

#temperature = 'Temperature: ' + str(raw_temperature*(9/5.0)+32) + '*F'

print(temperature)


# Pressure

pressure = 'Pressure: ' + str(weather_data.json().get('main').get('pressure')) + 'hPa'

print(pressure)


# Humidity

humidity = 'Humidity: ' + str(weather_data.json().get('main').get('humidity')) + '%'

print(humidity)


# Wind

wind = 'Wind: ' + str(weather_data.json().get('wind').get('speed')) + 'mps ' + str(weather_data.json().get('wind').get('deg')) + '*'

print(wind)



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

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.010508060455322 seconds