找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1271|回复: 0
打印 上一主题 下一主题
收起左侧

esp8266天气系统程序

[复制链接]
跳转到指定楼层
楼主
最近闲的没事看到我的8266开发板
突发奇想做了个迷你天气预报
用的是0.96寸oled屏幕
源码在下面 欢迎讨论修改

Arduino源程序如下:
  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoJson.h>
  3. #include <Wire.h>
  4. #include "OLED.h"
  5. static const char logo[] PROGMEM={
  6. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  7. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  8. 0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0x80,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  9. 0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  10. 0xff,0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x80,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  11. 0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0x7f,
  12. 0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0x7f,
  13. 0xff,0xff,0xff,0xff,0xff,0xe0,0xff,0x03,0x00,0xe0,0xff,0xff,0xff,0xff,0xff,0x7f,
  14. 0xff,0xff,0xff,0xff,0x1f,0x1c,0xf0,0x1f,0x00,0x80,0xff,0xff,0xff,0xff,0xff,0x7f,
  15. 0xff,0xff,0xff,0xff,0x0f,0x00,0x80,0x7f,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0x7f,
  16. 0xff,0xff,0xff,0xff,0x0f,0x00,0x00,0xff,0x01,0x00,0xfc,0xff,0xff,0xff,0xff,0x7f,
  17. 0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfe,0x03,0x00,0xf8,0xff,0xff,0xff,0xff,0x7f,
  18. 0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfc,0x07,0x00,0xf8,0xff,0xff,0xff,0xff,0x7f,
  19. 0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xf8,0x0f,0x00,0xf0,0xff,0xff,0xff,0xff,0x7f,
  20. 0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xf0,0x1f,0x00,0xf0,0xff,0xff,0xff,0xff,0x7f,
  21. 0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0xf0,0x1f,0x00,0xf0,0xff,0xff,0xff,0xff,0x7f,
  22. 0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0xf0,0x1f,0x00,0xf0,0xff,0xff,0xff,0xff,0x7f,
  23. 0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0xf0,0x1f,0x00,0xf0,0xff,0xff,0xff,0xff,0x7f,
  24. 0xff,0xff,0xff,0x0f,0x00,0x00,0x00,0xf0,0x3f,0x00,0xf0,0xff,0xff,0xff,0xff,0x7f,
  25. 0xff,0xff,0xff,0x07,0x00,0x00,0x00,0xf0,0x1f,0x00,0xf8,0xff,0xff,0xff,0xff,0x7f,
  26. 0xff,0xff,0xff,0x07,0x00,0x00,0x00,0xf0,0x0f,0x00,0xf8,0xff,0xff,0xff,0xff,0x7f,
  27. 0xff,0xff,0xff,0x03,0x00,0x00,0x00,0xf8,0x03,0x00,0xfc,0xff,0xff,0xff,0xff,0x7f,
  28. 0xff,0xff,0xff,0x01,0x00,0x00,0x00,0xfc,0x00,0x00,0x7e,0xff,0xff,0xff,0xff,0x7f,
  29. 0xff,0xff,0xff,0x01,0x00,0x00,0x00,0x7f,0x00,0x00,0x7f,0xf8,0xff,0xff,0xff,0x7f,
  30. 0xff,0xff,0xff,0x00,0x00,0x00,0x80,0x1f,0x00,0x80,0x7f,0xf8,0xff,0xff,0xff,0x7f,
  31. 0xff,0xff,0xff,0x00,0x00,0x00,0xe0,0x07,0x00,0xc0,0x3f,0xf0,0xff,0xff,0xff,0x7f,
  32. 0xff,0xff,0xff,0x00,0x00,0x00,0xf8,0x01,0x00,0xe0,0x3f,0xf0,0xff,0xff,0xff,0x7f,
  33. 0xff,0xff,0x7f,0x00,0x00,0x00,0x7f,0x00,0x00,0xf8,0x3f,0xf0,0xff,0xff,0xff,0x7f,
  34. 0xff,0xff,0x7f,0x00,0x00,0xe0,0x1f,0x00,0x00,0xfe,0x0f,0xf0,0xff,0xff,0xff,0x7f,
  35. 0xff,0xff,0x7f,0x00,0x00,0xf8,0x03,0x00,0x80,0xff,0x0f,0xf8,0xff,0xff,0xff,0x7f,
  36. 0xff,0xff,0x7f,0x00,0x00,0x7e,0x00,0x00,0xe0,0xff,0x01,0xf0,0xff,0xff,0xff,0x7f,
  37. 0xff,0xff,0x7f,0x00,0x00,0x1f,0x00,0x00,0xf8,0xff,0x00,0xf8,0xff,0xff,0xff,0x7f,
  38. 0xff,0xff,0x7f,0x00,0xc0,0x0f,0x00,0x00,0xfe,0x7f,0x00,0xfc,0xff,0xff,0xff,0x7f,
  39. 0xff,0xff,0x7f,0x00,0xe0,0x03,0x00,0x80,0xff,0x1f,0x00,0xfe,0xff,0xff,0xff,0x7f,
  40. 0xff,0xff,0x7f,0x00,0xf8,0x01,0x00,0xe0,0xff,0x07,0x00,0xfe,0xff,0xff,0xff,0x7f,
  41. 0xff,0xff,0x7f,0x00,0x7c,0x00,0x00,0xfc,0xff,0x00,0x00,0xfc,0xff,0xff,0xff,0x7f,
  42. 0xff,0xff,0x7f,0x00,0x3e,0x00,0x00,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0x7f,
  43. 0xff,0xff,0x7f,0x00,0x0f,0x00,0xc0,0xff,0x07,0x00,0x00,0xff,0xff,0xff,0xff,0x7f,
  44. 0xff,0xff,0xff,0x80,0x07,0x00,0xf0,0xff,0xff,0x0f,0xc0,0xff,0xff,0xff,0xff,0x7f,
  45. 0xff,0xff,0xff,0xc0,0x03,0x00,0xf8,0xff,0xff,0x3f,0xf8,0xff,0xff,0xff,0xff,0x7f,
  46. 0xff,0xff,0xff,0xc0,0x01,0x00,0xfe,0xf8,0xc7,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  47. 0xff,0xff,0xff,0xe1,0x01,0x00,0x7f,0xc0,0x03,0x87,0x00,0xfe,0xff,0xff,0xff,0x7f,
  48. 0xff,0xff,0xff,0xe1,0x00,0xc0,0x7f,0x80,0x01,0x86,0x00,0xfe,0xff,0xff,0xff,0x7f,
  49. 0xff,0xff,0xff,0x63,0x00,0xe0,0x7f,0x80,0x01,0x86,0x00,0xfe,0xff,0xff,0xff,0x7f,
  50. 0xff,0xff,0xff,0x63,0x00,0xe0,0x7f,0x8c,0x31,0x86,0xc3,0xff,0xff,0xff,0xff,0x7f,
  51. 0xff,0xff,0xff,0x67,0x00,0xc0,0x7f,0x8c,0x31,0x86,0x83,0xff,0xff,0xff,0xff,0x7f,
  52. 0xff,0xff,0xff,0x6f,0x00,0xc0,0x7f,0x8c,0x31,0x86,0x83,0xff,0xff,0xff,0xff,0x7f,
  53. 0xff,0xff,0xff,0x7f,0x00,0xc0,0x7f,0x8c,0x31,0x86,0x83,0xff,0xff,0xff,0xff,0x7f,
  54. 0xff,0xff,0xff,0x7f,0x00,0xc0,0x7f,0x8c,0x31,0x86,0x83,0xff,0xff,0xff,0xff,0x7f,
  55. 0xff,0xff,0xff,0xff,0x00,0x80,0x7f,0x80,0x01,0x86,0x83,0xff,0xff,0xff,0xff,0x7f,
  56. 0xff,0xff,0xff,0xff,0x01,0x00,0x7f,0x80,0x01,0x86,0x83,0xff,0xff,0xff,0xff,0x7f,
  57. 0xff,0xff,0xff,0xff,0x03,0x00,0x7c,0xc0,0x03,0x87,0x83,0xff,0xff,0xff,0xff,0x7f,
  58. 0xff,0xff,0xff,0xff,0x07,0x00,0xf8,0xff,0xff,0xf1,0xff,0xff,0xff,0xff,0xff,0x7f,
  59. 0xff,0xff,0xff,0xff,0x0f,0x00,0xe0,0xff,0x7f,0xf0,0xff,0xff,0xff,0xff,0xff,0x7f,
  60. 0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0xfe,0x07,0xfc,0xff,0xff,0xff,0xff,0xff,0x7f,
  61. 0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  62. 0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xe0,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  63. 0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  64. 0xff,0xff,0xff,0xff,0xff,0x7f,0x00,0x80,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  65. 0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  66. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  67. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  68. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
  69. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f
  70. };
  71. //GPIO4(SDA) GPIO5(SCL)
  72. OLED display(4, 5);
  73.   
  74. const char* ssid     = "xh003";         // XXXXXX -- 使用时请修改为当前你的 wifi ssid
  75. const char* password = "zxd+19901120=";         // XXXXXX -- 使用时请修改为当前你的 wifi 密码
  76. const char* host = "api.seniversecom";
  77. const char* APIKEY = "wcmquevztdy1jpca";        //API KEY
  78. const char* city = "shenzhen";
  79. const char* language = "en";//zh-Hans 简体中文  会显示乱码
  80.   
  81. const unsigned long BAUD_RATE = 115200;                   // serial connection speed
  82. const unsigned long HTTP_TIMEOUT = 5000;               // max respone time from server
  83. const size_t MAX_CONTENT_SIZE = 1000;                   // max size of the HTTP response
  84.   
  85. // 我们要从此网页中提取的数据的类型
  86. struct UserData {
  87.   char city[16];//城市名称
  88.   char weather[32];//天气介绍(多云...)
  89.   char temp[16];//温度
  90.   char udate[32];//更新时间
  91. };
  92.   
  93. WiFiClient client;
  94. char response[MAX_CONTENT_SIZE];
  95. char endOfHeaders[] = "\r\n\r\n";
  96.   
  97. /**
  98. * @Desc 初始化操作
  99. */
  100. void setup() {
  101.   WiFi.mode(WIFI_STA);     //设置esp8266 工作模式
  102.   Serial.begin(BAUD_RATE);
  103.   delay(10);
  104.   Serial.println();
  105.   Serial.println();
  106.   Serial.print("Connecting to ");//写几句提示,哈哈
  107.   Serial.println(ssid);
  108.   WiFi.begin(ssid, password);   //连接wifi
  109.   while (WiFi.status() != WL_CONNECTED) {
  110.     //这个函数是wifi连接状态,返回wifi链接状态
  111.     delay(100);
  112.     Serial.print(".");
  113.   }
  114.   Serial.println("");
  115.   Serial.println("WiFi connected");
  116.   delay(100);
  117.   Serial.println("IP address: ");
  118.   Serial.println(WiFi.localIP());//WiFi.localIP()返回8266获得的ip地址
  119.   client.setTimeout(HTTP_TIMEOUT);

  120. }
  121.   
  122. /**
  123. * @Desc  主函数
  124. */

  125. void loop() {
  126.    //初始化oled
  127.   display.begin();

  128.   //测试显示信息
  129.   display.print("Hello DOIT",2,3);
  130.   display.print("doitam",4,3);
  131.   delay(2*1000);
  132.   display.clear();

  133.   while (!client.connected()){
  134.      if (!client.connect(host, 80)){
  135.          Serial.println("connection....");
  136.          delay(100);
  137.      }
  138.   }
  139.   if (sendRequest(host, city, APIKEY) && skipResponseHeaders()) {
  140.     clrEsp8266ResponseBuffer();
  141.     readReponseContent(response, sizeof(response));
  142.     UserData userData;
  143.     if (parseUserData(response, &userData)) {
  144.       printUserData(&userData);
  145.     }
  146.   }
  147.   delay(300);//每5s调用一次
  148. }
  149.   
  150. /**
  151. * @发送请求指令
  152. */
  153. bool sendRequest(const char* host, const char* cityid, const char* apiKey) {
  154.   // We now create a URI for the request
  155.   //心知天气
  156.   String GetUrl = "/v3/weather/now.json?key=";
  157.   GetUrl += apiKey;
  158.   GetUrl += "&location=";
  159.   GetUrl += city;
  160.   GetUrl += "&language=";
  161.   GetUrl += language;
  162.   // This will send the request to the server
  163.   client.print(String("GET ") + GetUrl + " HTTP/1.1\r\n" +
  164.                "Host: " + host + "\r\n" +
  165.                "Connection: close\r\n\r\n");
  166.   Serial.println("create a request:");
  167.   Serial.println(String("GET ") + GetUrl + " HTTP/1.1\r\n" +
  168.                "Host: " + host + "\r\n" +
  169.                "Connection: close\r\n");
  170.   delay(100);
  171.   return true;
  172. }
  173.   
  174. /**
  175. * @Desc 跳过 HTTP 头,使我们在响应正文的开头
  176. */
  177. bool skipResponseHeaders() {
  178.   // HTTP headers end with an empty line
  179.   bool ok = client.find(endOfHeaders);
  180.   if (!ok) {
  181.     Serial.println("No response or invalid response!");
  182.   }
  183.   return ok;
  184. }
  185.   
  186. /**
  187. * @Desc 从HTTP服务器响应中读取正文
  188. */
  189. void readReponseContent(char* content, size_t maxSize) {
  190.   size_t length = client.peekBytes(content, maxSize);
  191.   delay(100);
  192.   Serial.println("Get the data from Internet!");
  193.   content[length] = 0;
  194.   Serial.println(content);
  195.   Serial.println("Read data Over!");
  196.   client.flush();//这句代码需要加上  不然会发现每隔一次client.find会失败
  197. }
  198.   
  199. /**
  200. * @Desc 解析数据
  201. * 数据格式如下:
  202. * {
  203. *    "results": [
  204. *        {
  205. *            "location": {
  206. *                "id": "WX4FBXXFKE4F",
  207. *                "name": "北京",
  208. *                "country": "CN",
  209. *                "path": "北京,北京,中国",
  210. *                "timezone": "Asia/Shanghai",
  211. *                "timezone_offset": "+08:00"
  212. *            },
  213. *            "now": {
  214. *                "text": "多云",
  215. *                "code": "4",
  216. *                "temperature": "23"
  217. *            },
  218. *            "last_update": "2017-09-13T09:51:00+08:00"
  219. *        }
  220. *    ]
  221. *}
  222. */
  223. bool parseUserData(char* content, struct UserData* userData) {
  224. //    -- 根据我们需要解析的数据来计算JSON缓冲区最佳大小
  225. //   如果你使用StaticJsonBuffer时才需要
  226. //    const size_t BUFFER_SIZE = 1024;
  227. //   在堆栈上分配一个临时内存池
  228. //    StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  229. //    -- 如果堆栈的内存池太大,使用 DynamicJsonBuffer jsonBuffer 代替
  230.   DynamicJsonBuffer jsonBuffer;
  231.    
  232.   JsonObject& root = jsonBuffer.parseObject(content);
  233.    
  234.   if (!root.success()) {
  235.     Serial.println("JSON parsing failed!");
  236.     return false;
  237.   }
  238.    
  239.   //复制我们感兴趣的字符串
  240.   strcpy(userData->city, root["results"][0]["location"]["name"]);
  241.   strcpy(userData->weather, root["results"][0]["now"]["text"]);
  242.   strcpy(userData->temp, root["results"][0]["now"]["temperature"]);
  243.   strcpy(userData->udate, root["results"][0]["last_update"]);
  244.   //  -- 这不是强制复制,你可以使用指针,因为他们是指向“内容”缓冲区内,所以你需要确保
  245.   //   当你读取字符串时它仍在内存中
  246.   return true;
  247. }
  248.    
  249. // 打印从JSON中提取的数据
  250. void printUserData( struct UserData* userData) {
  251.   Serial.println("Print parsed data :");
  252.   display.print("City: ",0,0);
  253.   display.print(userData->city,0,6);
  254.   display.print("Weat: ",2,0);
  255.   display.print(userData->weather,2,6);
  256.   display.print("Temp: ",4,0);
  257.   display.print(userData->temp,4,6);
  258. // display.print("Last Updata : ",7,2);
  259. display.print(userData->udate,6,0);
  260. // display.print("\r\n");
  261. delay(4*1000);
  262. }
  263.    
  264. // 关闭与HTTP服务器连接
  265. void stopConnect() {
  266.   Serial.println("Disconnect");
  267.   client.stop();
  268. }
  269.   
  270. void clrEsp8266ResponseBuffer(void){
  271.     memset(response, 0, MAX_CONTENT_SIZE);      //清空
  272. }
复制代码


bin文件下载: ESP8266获取天气文件.zip (191.1 KB, 下载次数: 39)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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