切换到宽版
  • 14601阅读
  • 38回复

[ARM]仿制ESP8266+OLED Weather Station_修改国内可用的天气源。 [复制链接]

上一主题 下一主题
离线litchiate
 

发帖
76
M币
352
专家
4
粉丝
6
只看楼主 倒序阅读 我要置顶 楼主  发表于: 2016-06-18
基本和 我爱苹果的帖子是一样的。

[ARM]仿制ESP8266+OLED Weather Station成功--已更新修改好的源码和库文件


http://bbs.mydigit.cn/read.php?tid=1691649
改自国外作者。
因为国外的天气源不好用,我改为了国内的天气源。


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


只好自己动手了。


首先确定天气预报的源,
参考链接 有哪些免费开放且收录城市较完整的天气 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":"金山","cnty":"中国","id":"CN101020700","lat":"30.824000","lon":"121.211000","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获取时间的功能,等哪天有空再给抄上。)




  • [size=; font-size: 1rem,1rem]准备元件

esp8266 我选的是ESP-12
OLED 用1306控制芯片,IIC接口
LDO之前选过XC6206,但是电流太小,初始化的时候老是会复位
仿制ESP8266+OLED Weather Station_修改国内可用的天气源。
  • [size=font-size: 1rem,1rem]安装 arduino 1.6.5



我用的是1.6.5 其它版本未测试,太老的不支持新功能。




  • [size=font-size: 1rem,1rem]安装 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 CODEhttp://arduino.esp8266.com/stable/package_esp8266com_index.jsonIf 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.)


仿制ESP8266+OLED Weather Station_修改国内可用的天气源。
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.


仿制ESP8266+OLED Weather Station_修改国内可用的天气源。
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.


仿制ESP8266+OLED Weather Station_修改国内可用的天气源。
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.
仿制ESP8266+OLED Weather Station_修改国内可用的天气源。

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程序




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



  • [size=font-size: 1rem,1rem]编译

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


仿制ESP8266+OLED Weather Station_修改国内可用的天气源。

使用附件中的文件替换
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
再次编译就不会报错了。









  • [size=font-size: 1rem,1rem]下载



将GPIO0和GND短接,选择UART Download模式。
接好串口线。
重新上电。
点击upload按钮。
本文内容包含图片或附件,获取更多资讯,请 登录 后查看;或者 注册 成为会员获得更多权限
本帖最近打赏记录:共4条打赏M币+10
cao57508 M币 +3 这个楼主原创的? 01-07
sharehej M币 +1 優秀文章 2016-08-02
maidoo M币 +3 我仿制LZ的,成功,赶来报个喜。 2016-08-01
hongo M币 +3 優秀文章 2016-06-21
我爱苹果 M币 +3 国内天气源连接快,也容易注册 2016-06-19
离线jpdd521

发帖
18971
M币
413
专家
15
粉丝
310
只看该作者 1楼 发表于: 2016-06-18
数天前一*论坛那就有了这个帖子内容。
离线ssis909

发帖
146
M币
5489
专家
13
粉丝
109
只看该作者 2楼 发表于: 2016-06-19
这个和风天气还是不够强大,应该是有很多不同的请求才可以,这样自己只是请求所需的数据,而不是一股脑全部丢给用户,全部给用户:对单片机而言就需要至少准备1K的内存接收数据,太不划算
离线我爱苹果

发帖
797
M币
258
专家
1
粉丝
41
只看该作者 3楼 发表于: 2016-06-19
编译通过,上传成功。
国内天气源连接快,注册容易。

这个版本是原作者15年的老版,界面稍显粗糙。


另:请教楼主老兄一个问题,同样是iic接口的LCD屏OLED,可以通用吗?驱动芯片不一样
离线litchiate

发帖
76
M币
352
专家
4
粉丝
6
只看该作者 4楼 发表于: 2016-06-19
所以用esp8266,ram用不完,随便开buff。
离线litchiate

发帖
76
M币
352
专家
4
粉丝
6
只看该作者 5楼 发表于: 2016-06-19
恩,年初的时候改的,界面确实不方便。
驱动芯片不一样的话,用u8glib的库试试?看看你的芯片是否在支持列表里?
离线信徒1102

发帖
231
M币
5267
专家
5
粉丝
31
只看该作者 6楼 发表于: 2016-07-28
weather-station-string.ino: In function 'void UpdateWeatherData()':
weather-station-string:167: error: 'WeatherString' was not declared in this scope
'WeatherString' was not declared in this scope

你好我的报这个错,不知道怎么处理呢?麻烦了
离线yanxue11

发帖
487
M币
4763
专家
8
粉丝
52
只看该作者 7楼 发表于: 2016-07-29
国家气象局源的要收费。


我一般用百度的天气API。


这个可以设置返回json或者xml
反正都一样。


代码里面设置一个128个字节长的缓存基本就可以了


固定获取某几个字节范围的字符,显示出来就OK了


不过这个东西很鸡肋。因为手机都自带天气的。。。。。。
离线litchiate

发帖
76
M币
352
专家
4
粉丝
6
只看该作者 8楼 发表于: 2016-07-29
回 信徒1102 的帖子
信徒1102:
weather-station-string.ino: In function 'void UpdateWeatherData()':
weather-station-string:167: error: 'WeatherString' was not declared in this scope
'WeatherString' was not declared in this scope
你好我的报这个错,不知道怎么处理呢?麻烦了 回 信徒1102 的帖子

  1. const char WeatherString[][64]=
  2. {
  3. "晴",
  4. "多云",
  5. "少云",
  6. "晴间多云",
  7. "阴",
  8. "有风",
  9. "平静",
  10. "微风",
  11. "和风",
  12. "清风",
  13. "强风/劲风",
  14. "疾风",
  15. "大风",
  16. "烈风",
  17. "风暴",
  18. "狂爆风",
  19. "飓风",
  20. "龙卷风",
  21. "热带风暴",
  22. "阵雨",
  23. "强阵雨",
  24. "雷阵雨",
  25. "强雷阵雨",
  26. "雷阵雨伴有冰雹",
  27. "小雨",
  28. "中雨",
  29. "大雨",
  30. "极端降雨",
  31. "毛毛雨/细雨",
  32. "暴雨",
  33. "大暴雨",
  34. "特大暴雨",
  35. "冻雨",
  36. "小雪",
  37. "中雪",
  38. "大雪",
  39. "暴雪",
  40. "雨夹雪",
  41. "雨雪天气",
  42. "阵雨夹雪",
  43. "阵雪",
  44. "薄雾",
  45. "雾",
  46. "霾",
  47. "扬沙",
  48. "浮尘",
  49. "火山灰",
  50. "沙尘暴",
  51. "强沙尘暴",
  52. "热",
  53. "冷",
  54. "未知",
  55. };

另存为 c:/string.h
离线maidoo

发帖
566
M币
6887
专家
19
粉丝
60
只看该作者 9楼 发表于: 2016-08-01
我仿制LZ的,成功,赶来报个喜。
快速回复
限80 字节
温馨提示:欢迎交流讨论,请勿发布纯表情、纯引用等灌水帖子;以免被删除
 
上一个 下一个