首页/作品列表/Arduinojson 6解析openweather制作气象台历
Arduinojson 6解析openweather制作气象台历
22510 0
主要内容
openweather是一家开源的气象网站,可以免费申请气象信息,数据详实更新及时。https://openweathermap.org/ 注册一下。
登录以后,https://home.openweathermap.org/api_keys 网站会给你一个密钥。
在网站 https://openweathermap.org/api 里面有各种的官方介绍,大家可以仔细阅读一下。网站 https://openweathermap.org/current 里面有通用的API,目前免费的网站协议是2.5版本的。
我在这里列举两种:
(1)使用城市的ID查询当地的气象数据 api.openweathermap.org/data/2.5/weather?id=1798524&APPID=*****密钥***** ,其中id=1798524 是上海浦东的ID,具体列表见附件文件,
(2)使用经纬度定位查询当地的气象数据 api.openweathermap.org/data/2.5/weather?lat=31.14&lon=121.52&APPID=*****密钥***** ,其中 lat=31.14&lon=121.52 是上海浦东三林公园的东经北纬定位信息,大家可以使用google地图获得,系统会自动寻找到最近的气象信息点康桥。
本例子我们使用第一个API,复制黏贴到谷歌浏览器的地址栏,openweather会反馈铺满整个屏幕的典型的json数据。microduino虽然很强大,但是内部存储空间也有限。因此,我们只能截取其中的一段来解析。根据原先的设计,本例子是结合U8g2lib自带的图标来表达天气状况。所以从dt一直截取到icon。其中有"[" 和"]"会影响到解析,那么必须做相应的处理。解析json数据本例子使用Arduinojson,我们升级到最新的6.12.0版本。
本例子使用的材料:644p,ft232,hub,OLED 屏幕,esp8266 WiFi模块。
给esp8266 WiFi模块刷入最近的AT命令固件。本例子使用Microduino FT232模块,ESP8266的uart0在serial 1即D2、D3位置,和FT232串口D0、D1相连,两者接线成交叉状态,如下图:
首先要下载erase_flash_bins,里面对应8M的文件,地址从0x0擦除整块flash。 https://wiki.ai-thinker.com/_media/tools/erase_flash_bins.7z
其次,下载美科科技的at固件:https://wiki.microduino.cn/images/5/57/MicroduinoESP8266.rar

美科科技esp8266 AT固件里面各个bin对应的地址分布如下:
boot_v1.7.bin                                          0x0
user1.4096.new.6_0.0.620.00.bin              0x01000
esp_init_data_default.bin                          0xFC000
blank.bin                                                 0xFE000

要确保左侧勾选两端条框呈现绿色,SpiFlashConfig的各个选项正确。

点击START,按下esp8266上的按钮,等待进程条到底,绿色小方块会有完成字样出现,祝贺你刷入固件成功。
openweather网站介绍图标和气候状态的关系见 https://openweathermap.org/weather-conditions 就是用解析出来的icon来将u8g2lib里面自带的图标对应起来。
u8g2lib里面自带的图标介绍:https://github.com/olikraus/u8g2/wiki/fntgrpiconic 这样我们就不需要再费力地用软件转化了。
这里还要介绍一个使用Arduinojson6的小技巧,点击打开链接 https://arduinojson.org/v6/assistant/ 这是arduinojson官方提供的小助手,把已经截取的字符串黏贴到input区域,网站就会给出记忆体长度和解析出的结构,你再把parsing program黏贴到IDE程序里面。是不是很方便呀!

还要注意的就是延时,不能过于频繁登录openweather查询,最好间隔四小时。长时间延时用delay()不科学。假设延时一小时,1秒 = 1000 millis(),1小时= 60*60*1000=3600000。
millis()五十天会溢出一次,这里使用signed long强制置零。

unsigned long lastConnectionTime = 3600000;      
const unsigned long postingInterval =  3600000;  
(millis() - lastConnectionTime > postingInterval)
lastConnectionTime = (signed long) millis();


最终效果:
(完)
代码展示

1. openweather_arduinojson_oled

编程语言: C/C++(Arduino)

软件工具:Arduino IDE 1.8.4


附件下载
  • 资料展示
  • city.list.zip
0
大牛,别默默的看了,快登录帮我点评一下吧!

立即注册