一乐电子

一乐电子百科

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

快捷登录

手机号码,快捷登录

搜索
查看: 8783|回复: 26
收起左侧

[其他DIY] ESP8266天气显示器_国内可用的修改版_增加NTP对时

[复制链接]
发表于 2016-6-10 21:59 | 显示全部楼层 |阅读模式
本帖最后由 litchiate 于 2016-6-16 22:54 编辑

修改记录:
20160616 增加NTP对时功能,上传新的附件和库文件。
weather-station-string_20160616_增加NTP.rar (13.82 KB, 下载次数: 375) NTPClient.rar (6.45 KB, 下载次数: 328)

static/image/hrline/5.gif


去年看了reiyawea短信铃+ESP8266改造为天气显示器帖子,

https://www.yleee.com.cn/forum.php?mod=viewthread&tid=45863 IMG_20150830_181435_HDR.jpg

用esp8266能做天气预报。
照着做了,但是原作者的方案将天气信息的解码部分放在另外的服务器上来实现,国内连不上,只能显示一堆错误的数据。
随后在网上搜索了,没有找到完整的能在国内方便使用的完整的代码。

只好自己动手了。

首先确定天气预报的源,
参考链接 有哪些免费开放且收录城市较完整的天气 API 接口?http://www.zhihu.com/question/20521716/answer/21247376
我找了方便注册,开放接口,免费的和风天气http://www.heweather.com/
(P.S忘了当初不选国家气象局源的原因了,可能是注册麻烦。希望现在这个不要用着就挂了)
(做好放了3个月,突然发现之前的程序无法正常使用了,3月返回的get请求,第9波是是JSON代码,到了6月变成第7波是JSON代码了,只好改程序,使用字符串长度来判断)


和风天气的数据是 JSON格式的,就是你发个请求过去,它会回你一大坨数据。
{"HeWeather data service 3.0":[{"aqi":{"city":{"aqi":"87","pm10":"82","pm25":"64"}},"basic":{"city":"xx","cnty":"中国","id":"xxx","lat":"xx","lon":"xxx","update":{"loc":"2016-06-01 23:53","utc":"2016-06-01 15:53"}},"daily_forecast":[{"astro":{"sr":"04:52","ss":"18:53"},"cond":{"code_d":"309","code_n":"101","txt_d":"毛毛雨/细雨","txt_n":"多云"},"date":"2016-06-01","hum":"77","pcpn":"15.6","pop":"99","pres":"1005","tmp":{"max":"23","min":"18"},"vis":"2","wind":{"deg":"63","dir":"东北风","sc":"微风","spd":"4"}},{"astro":{"sr":"04:52","ss":"18:54"},"cond":{"code_d":"101","code_n":"305","txt_d":"多云","txt_n":"小雨"},"date":"2016-06-02","hum":"58","pcpn":"0.0","pop":"0","pres":"1010","tmp":{"max":"24","min":"19"},"vis":"10","wind":{"deg":"108","dir":"东风","sc":"微风","spd":"0"}},{"astro":{"sr":"04:52","ss":"18:54"},"cond":{"code_d":"306","code_n":"306","txt_d":"中雨","txt_n":"中雨"},"date":"2016-06-03","hum":"91","pcpn":"9.5","pop":"84","pres":"1007","tmp":{"max":"22","min":"19"},"vis":"9","wind":{"deg":"86","dir":"东北风","sc":"微风","spd":"1"}},{"astro":{"sr":"04:51","ss":"18:55"},"cond":{"code_d":"305","code_n":"104","txt_d":"小雨","txt_n":"阴"},"date":"2016-06-04","hum":"82","pcpn":"3.9","pop":"79","pres":"1010","tmp":{"max":"23","min":"17"},"vis":"2","wind":{"deg":"62","dir":"东北风","sc":"微风","spd":"3"}},{"astro":{"sr":"04:51","ss":"18:55"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2016-06-05","hum":"50","pcpn":"0.0","pop":"6","pres":"1015","tmp":{"max":"25","min":"18"},"vis":"10","wind":{"deg":"90","dir":"东北风","sc":"微风","spd":"3"}},{"astro":{"sr":"04:51","ss":"18:56"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2016-06-06","hum":"61","pcpn":"0.0","pop":"0","pres":"1014","tmp":{"max":"25","min":"19"},"vis":"10","wind":{"deg":"122","dir":"东风","sc":"微风","spd":"1"}},{"astro":{"sr":"04:51","ss":"18:56"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2016-06-07","hum":"65","pcpn":"0.0","pop":"0","pres":"1011","tmp":{"max":"25","min":"20"},"vis":"10","wind":{"deg":"146","dir":"东风","sc":"微风","spd":"3"}}],"hourly_forecast":[{"date":"2016-06-02 01:00","hum":"89","pop":"0","pres":"1009","tmp":"19","wind":{"deg":"14","dir":"东北风","sc":"微风","spd":"6"}},{"date":"2016-06-02 04:00","hum":"94","pop":"0","pres":"1009","tmp":"18","wind":{"deg":"23","dir":"东北风","sc":"微风","spd":"6"}},{"date":"2016-06-02 07:00","hum":"88","pop":"0","pres":"1010","tmp":"21","wind":{"deg":"57","dir":"东北风","sc":"微风","spd":"9"}},{"date":"2016-06-02 10:00","hum":"68","pop":"0","pres":"1010","tmp":"26","wind":{"deg":"86","dir":"东风","sc":"微风","spd":"15"}},{"date":"2016-06-02 13:00","hum":"59","pop":"0","pres":"1010","tmp":"27","wind":{"deg":"103","dir":"东南风","sc":"3-4","spd":"21"}},{"date":"2016-06-02 16:00","hum":"62","pop":"0","pres":"1009","tmp":"25","wind":{"deg":"108","dir":"东南风","sc":"3-4","spd":"21"}},{"date":"2016-06-02 19:00","hum":"72","pop":"0","pres":"1010","tmp":"22","wind":{"deg":"95","dir":"东风","sc":"微风","spd":"15"}},{"date":"2016-06-02 22:00","hum":"79","pop":"0","pres":"1011","tmp":"20","wind":{"deg":"97","dir":"东风","sc":"微风","spd":"13"}}],"now":{"cond":{"code":"101","txt":"多云"},"fl":"19","hum":"89","pcpn":"0","pres":"1009","tmp":"18","vis":"10","wind":{"deg":"360","dir":"北风","sc":"微风","spd":"1"}},"status":"ok","suggestion":{"comf":{"brf":"舒适","txt":"白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。"},"cw":{"brf":"不宜","txt":"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"},"drsg":{"brf":"较舒适","txt":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"},"flu":{"brf":"易发","txt":"相对于今天将会出现大幅度降温,风力较大,易发生感冒,请注意适当增加衣服。"},"sport":{"brf":"较不宜","txt":"有降水,且风力较强,气压较低,推荐您在室内进行低强度运动;若坚持户外运动,须注意避雨防风。"},"trav":{"brf":"适宜","txt":"有降水,虽然风稍大,但温度适宜,适宜旅游,可不要错过机会呦!"},"uv":{"brf":"最弱","txt":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}}}]}


这么一大坨,普通的arduino板肯定无法处理了。

网上找了个arduino 的JSON库,可以从JSON中提取出所需要的数据。经过反复测试,数据量太大,无法解析。(砍掉一半数据是可以解析的)
后来使用了字符串查找函数,取出了所需的数据,修改原程序,任务完成。
做完放了三个月,现在有空整理下发出来。

(P.S 后来原作者又更新了NTP获取时间的功能,等哪天有空再给抄上。)



  • 准备元件
esp8266 我选的是ESP-12
OLED 用1306控制芯片,IIC接口
LDO之前选过XC6206,但是电流太小,初始化的时候老是会复位
ca2b71df-83f6-4bbe-bd49-5bde6fc786d8.png
  • 安装 arduino 1.6.5
我用的是1.6.5 其它版本未测试,太老的不支持新功能。


  • 安装 esp8266 扩展
Installing the ESP8266 Arduino Addon
There are a variety of development environments that can be equipped to program the ESP8266. You can go with a simple Notepad/gcc setup, or fine-tune an Eclipse environment, use a virtual machine provided by Espressif, or come up with something of your own.
Fortunately, the amazing ESP8266 community recently took the IDE selection a step further by creating an Arduino addon. If you’re just getting started programming the ESP8266, this is the environment we recommend beginning with, and the one we’ll document in this tutorial.
This ESP8266 addon for Arduino is based on the amazing work by Ivan Grokhotkov and the rest of the ESP8266 community. Check out the ESP8266 Arduino GitHub repository for more information.
Installing the Addon With the Arduino Boards Manager
With the release of Arduino 1.6.4, adding third party boards to the Arduino IDE is easily achieved through the new board manager. If you’re running an older version of Arduino (1.6.3 or earlier), we recommend upgrading now. As always, you can download the latest version of Arduino fromarduino.cc.
To begin, we’ll need to update the board manager with a custom URL. Open up Arduino, then go to the Preferences (File > Preferences). Then, towards the bottom of the window, copy this URL into the “Additional Board Manager URLs” text box:
COPY CODE
http://arduino.esp8266.com/stable/package_esp8266com_index.json
If you already have a URL in there, and want to keep it, you can separate multiple URLs by placing a comma between them. (Arduino 1.6.5 added an expanded text box, separate links in here by line.)
arduino-board-manager-link_3.png
Hit OK. Then navigate to the Board Manager by going to Tools > Boards > Boards Manager. There should be a couple new entries in addition to the standard Arduino boards. Look for esp8266. Click on that entry, then select Install.
arduino-board-install_3.png
The board definitions and tools for the ESP8266 Thing include a whole new set of gcc, g++, and other reasonably large, compiled binaries, so it may take a few minutes to download and install (the archived file is ~110MB). Once the installation has completed, an Arduino-blue “INSTALLED” will appear next to the entry.
Selecting the ESP8266 Thing Board
With the Board addon installed, all that’s left to do is select “ESP8266 Thing” from the Tools >Boards menu.
arduino-board-select_3.png
Then select your FTDI’s port number under the Tools > Port menu.
Upload Blink
To verify that everything works, try uploading the old standard: Blink. Instead of blinking pin 13, like you may be used to though, toggle pin 5, which is attached to the onboard LED.
COPY CODE
#define ESP8266_LED 5void setup() {  pinMode(ESP8266_LED, OUTPUT);}void loop() {  digitalWrite(ESP8266_LED, HIGH);  delay(500);  digitalWrite(ESP8266_LED, LOW);  delay(500);}
If the upload fails, first make sure the ESP8266 Thing is turned on – the red “PWR” LED should be illuminated.
Faster Uploads! The serial upload speed defaults to 115200 bps, which is reliable, but can feel a bit slow. You can increase the upload speed by a factor of about 8 by selecting 921600 under the Tools > Upload Speed menu.
arduino-upload-speed_3.png
This faster upload speed can be slightly less reliable, but will save you loads of time!

There are still some bugs to be fleshed out of the esptool, sometimes it may take a couple tries to successfully upload a sketch. If you continue to fail, try turning the ESP8266 Thing on then off, or unplug then replug the FTDI in. If you still have trouble, get in touch with our amazing tech support team.

来源: https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon


  • 注册帐号

http://www.heweather.com/注册,获取API KEY

  • 修改arduino程序


下载附件
weather-station-string_20160610.rar (16.21 KB, 下载次数: 227)

修改 无线路由的SSID
修改 无线路由的Password
修改 城市名称/城市代码
         参考 http://www.heweather.com/documents/cn-city-list
修改 API KEY

  • 编译

编译时有可能会出现下图错误,原因是某个库文件有问题,把它替换掉就行了。

50018f1e-5c96-4860-aefd-11b25e0c5b43.PNG
使用附件中的文件替换
C:\Documents and Settings\Administrator\Application Data\Arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9\xtensa-lx106-elf\lib\libm.a
再次编译就不会报错了。
下面是附件。
libm.rar (155.4 KB, 下载次数: 183)


  • 下载
将GPIO0和GND短接,选择UART Download模式。
接好串口线。
重新上电。
点击upload按钮。




评分

参与人数 2一乐金币 +112 收起 理由
无锡风 + 100 赞一个!
huojunan + 12 很给力!

查看全部评分

发表于 2016-6-10 22:26 | 显示全部楼层
这个牛!刚好有ESP8266模块,可以测试以下.非常感谢楼主分享!
发表于 2016-6-10 23:24 | 显示全部楼层
来个效果看看呗,我等小白不知所云
 楼主| 发表于 2016-6-10 23:34 | 显示全部楼层
kingtyj 发表于 2016-6-10 23:24
来个效果看看呗,我等小白不知所云

看帖子里第一个链接


发表于 2016-6-11 12:40 | 显示全部楼层
返回的数据格式,总会变, 不实际 。。。。。。。。。
发表于 2016-6-11 14:30 | 显示全部楼层
这就奇怪了,我这里还是能连的。可能是不同地区连接海外服务器有不同的限制吧。
作者开的私服会把原始的超长数据精简掉,以便于8266处理。看起来应该众筹在国内开个同样的私服。作者的私服也是开源的。
感觉天气不太实用,每天就看一次,用手机也足够了,还是NTP更实用一点,加个刷新闻功能或许不错,随时了解时事。
发表于 2016-6-12 10:12 | 显示全部楼层
我碰到个问题,就是arduino的ESP8266扩展无法安装,速度特别慢,有手动安装的方法吗?版本 1.6.5
发表于 2016-6-16 15:08 | 显示全部楼层
如果直接从预报网站搜刮HTML文件放入SD卡,然后用单片机解析呢?
 楼主| 发表于 2016-6-16 15:48 | 显示全部楼层
leifeng 发表于 2016-6-12 10:12
我碰到个问题,就是arduino的ESP8266扩展无法安装,速度特别慢,有手动安装的方法吗?版本 1.6.5

墙。 。 。
 楼主| 发表于 2016-6-16 15:51 | 显示全部楼层
hefanghua 发表于 2016-6-16 15:08
如果直接从预报网站搜刮HTML文件放入SD卡,然后用单片机解析呢?

既然都能搜刮html了,搜刮JSON格式的数据应该会比较省力。



本版积分规则

QQ|一淘宝店|手机版|商店|电子DIY套件|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2024-4-27 12:51 , Processed in 0.094518 second(s), 51 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表