查看: 3425|回复: 4

小E天气预报

[复制链接]
  • TA的每日心情
    奋斗
    2016-4-18 10:19
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2016-4-24 20:34:36 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 zhenlinuaa 于 2016-4-24 20:47 编辑

    将小E联网后,从openweathermap网站抓取天气数据显示在屏幕上。由于小e屏幕太小只能显示有限数据,
    否则可以做未来5天的天气预报。
    运行效果如下图所示:

    002.jpg

    03.JPG
    源代码:
    1. #include <ESP8266WiFi.h> //Include the library ESP8266
    2. #include <Wire.h>
    3. #include "SSD1306.h"

    4. // Initialize the oled display for address 0x3c
    5. //I2C-ADD=0X3C, sda-pin=2 and sdc-pin=14
    6. SSD1306 oled(0x3c, 2, 14);

    7. const char* network = "xxx"; //wifi name
    8. const char* password = "xxxx";//wifi password
    9. String site = "api.openweathermap.org"; //Create a string named site

    10. int cityID = 1790923;

    11. WiFiClient client; //Create the client object

    12. String answer;
    13. int indexa, indexb;


    14. void setup()
    15. {
    16.         Serial.begin(115200);// Starts the serial port with 9600 baud

    17.         oled.init();
    18.         oled.flipScreenVertically();
    19.         oled.setFont(ArialMT_Plain_24);
    20.         oled.setTextAlignment(TEXT_ALIGN_CENTER);
    21.         oled.drawString(64, 20, "boot...");
    22.         oled.display();
    23.         oled.clear();


    24.         Serial.println();
    25.         Serial.print("Conecting to network of ");
    26.         Serial.println(network);

    27.         WiFi.begin(network, password);//Connect to the network

    28.         Serial.println("Waiting connection to the router...");
    29.         while (WiFi.status() != WL_CONNECTED)//Print on the serial “-“ while the module doesn′t connect to the network
    30.         {
    31.                 delay(500);
    32.                 Serial.print("-");
    33.         }
    34.         Serial.print("\nWi-Fi connected. IP: ");
    35.         Serial.println(WiFi.localIP());//Print IP

    36. }

    37. void loop()
    38. {
    39.         Serial.println("\nWaiting connection with the server...");
    40.         while (!client.connect(site.c_str(), 80))//Wait the connection printing |
    41.         {
    42.                 delay(500);
    43.                 Serial.print('|');
    44.         }
    45.         if(client.connected())
    46.         {
    47.                 Serial.println();
    48.                 Serial.println("Sending request HTTP "); // Request from the server information about the weather
    49.                 //api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111
    50.                 client.print("GET /data/2.5/weather?");
    51.                 client.print("id=");
    52.                 client.print(cityID);
    53.                 client.print("&APPID=xxxxxxxx ");//APPID from the site OWP
    54.                 client.print("HTTP/1.1\r\n");
    55.                 client.print("Host: " + site + "\r\n\r\n");
    56.                 client.print("Connection: close");

    57.                 Serial.println("Waiting server answer...");
    58.                 while (!client.available())
    59.                 {
    60.                         Serial.print('*');
    61.                         delay(500);
    62.                 }
    63.                 while (client.available())//Read the answer of the API and keep the data on the string answer
    64.                 {
    65.                         char c = client.read();
    66.                         answer += c;
    67.                 }
    68.                 Serial.println(answer);
    69.         }

    70.         if(answer != "")
    71.         {
    72.                 Serial.print("\n\n");

    73.                 String overview = "";
    74.                 indexa = answer.indexOf("name");
    75.                 indexb = answer.indexOf("cod");
    76.                 overview += answer.substring(indexa + 7, indexb - 3);
    77.                 overview += ", ";
    78.                 indexa = answer.indexOf("description");
    79.                 indexb = answer.indexOf("icon");
    80.                 overview += answer.substring(indexa + 14, indexb - 3);
    81.                 Serial.println(overview);


    82.                 String wind = "wind: ";
    83.                 indexa = answer.indexOf("speed");
    84.                 indexb = answer.indexOf("deg");
    85.                 wind += answer.substring(indexa + 7, indexb - 3);
    86.                 wind +="m/s,deg ";
    87.                 indexa = answer.indexOf("deg");
    88.                 indexb = answer.indexOf("rain");
    89.                 wind += answer.substring(indexa + 5, indexb - 3);
    90.                 Serial.println(wind);


    91.                 String temperature = "Temperature: ";
    92.                 indexa = answer.indexOf("temp");//Find on answer an índice that starts with temp
    93.                 String stempk = answer.substring(indexa + 6, indexa + 13);//Receive from answer a String with the temperature value in Kelvin
    94.                 int tempk = stempk.toInt();//Change the stempk to integer
    95.                 int tempc = tempk - 273;//Transform the temperature to Celsius
    96.                 temperature += tempc;
    97.                 temperature += " oC";
    98.                 Serial.println(temperature);

    99.                 String humidity = "humidity: ";
    100.                 indexa = answer.indexOf("humidity");
    101.                 indexb = answer.indexOf("temp_min");
    102.                 humidity += answer.substring(indexa + 10, indexb - 2);
    103.                 humidity += " %";
    104.                 Serial.println(humidity);

    105.                 //Serial.print("rain: ");
    106.                 //indexa = answer.indexOf("rain");
    107.                 //String rain = answer.substring(indexa + 12, indexa + 17);
    108.                 //Serial.println(rain);

    109.                 Serial.print("\n\n\n\n");

    110.                 oled.setFont(ArialMT_Plain_10);
    111.                 oled.setTextAlignment(TEXT_ALIGN_LEFT);
    112.                 oled.drawString(0, 0, overview);
    113.                 oled.drawString(0, 12, wind);
    114.                 oled.drawString(0, 24, temperature);
    115.                 oled.drawString(0, 36, humidity);

    116.                 oled.display();
    117.                 oled.clear();

    118.         }

    119.         delay(60000);

    120.         answer = "";

    121. }
    复制代码
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2019-11-30 19:48
  • 签到天数: 981 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2016-4-25 09:01:58 | 显示全部楼层
    感谢分享!!!!!!!!1
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-12-1 19:29
  • 签到天数: 114 天

    连续签到: 1 天

    [LV.6]常住居民II

    发表于 2016-4-25 10:25:35 | 显示全部楼层
    这是小e的代码吗,兼容arduino?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-7-6 08:48
  • 签到天数: 169 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2016-4-25 14:23:31 | 显示全部楼层
    是要跟天气服务器通讯么?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-11-19 16:39
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2016-5-9 15:48:00 | 显示全部楼层
    文章不错,请内容去经验频道一并发一下,可以有双重奖励哟http://jingyan.eeboard.com/
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 16:46 , Processed in 0.154546 second(s), 23 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.