TA的每日心情 | 奋斗 2016-4-18 10:19 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
本帖最后由 zhenlinuaa 于 2016-4-24 20:47 编辑
将小E联网后,从openweathermap网站抓取天气数据显示在屏幕上。由于小e屏幕太小只能显示有限数据,
否则可以做未来5天的天气预报。
运行效果如下图所示:
源代码:- #include <ESP8266WiFi.h> //Include the library ESP8266
- #include <Wire.h>
- #include "SSD1306.h"
- // Initialize the oled display for address 0x3c
- //I2C-ADD=0X3C, sda-pin=2 and sdc-pin=14
- SSD1306 oled(0x3c, 2, 14);
- const char* network = "xxx"; //wifi name
- const char* password = "xxxx";//wifi password
- String site = "api.openweathermap.org"; //Create a string named site
- int cityID = 1790923;
- WiFiClient client; //Create the client object
- String answer;
- int indexa, indexb;
- void setup()
- {
- Serial.begin(115200);// Starts the serial port with 9600 baud
- oled.init();
- oled.flipScreenVertically();
- oled.setFont(ArialMT_Plain_24);
- oled.setTextAlignment(TEXT_ALIGN_CENTER);
- oled.drawString(64, 20, "boot...");
- oled.display();
- oled.clear();
- Serial.println();
- Serial.print("Conecting to network of ");
- Serial.println(network);
- WiFi.begin(network, password);//Connect to the network
- Serial.println("Waiting connection to the router...");
- while (WiFi.status() != WL_CONNECTED)//Print on the serial “-“ while the module doesn′t connect to the network
- {
- delay(500);
- Serial.print("-");
- }
- Serial.print("\nWi-Fi connected. IP: ");
- Serial.println(WiFi.localIP());//Print IP
- }
- void loop()
- {
- Serial.println("\nWaiting connection with the server...");
- while (!client.connect(site.c_str(), 80))//Wait the connection printing |
- {
- delay(500);
- Serial.print('|');
- }
- if(client.connected())
- {
- Serial.println();
- Serial.println("Sending request HTTP "); // Request from the server information about the weather
- //api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111
- client.print("GET /data/2.5/weather?");
- client.print("id=");
- client.print(cityID);
- client.print("&APPID=xxxxxxxx ");//APPID from the site OWP
- client.print("HTTP/1.1\r\n");
- client.print("Host: " + site + "\r\n\r\n");
- client.print("Connection: close");
- Serial.println("Waiting server answer...");
- while (!client.available())
- {
- Serial.print('*');
- delay(500);
- }
- while (client.available())//Read the answer of the API and keep the data on the string answer
- {
- char c = client.read();
- answer += c;
- }
- Serial.println(answer);
- }
- if(answer != "")
- {
- Serial.print("\n\n");
- String overview = "";
- indexa = answer.indexOf("name");
- indexb = answer.indexOf("cod");
- overview += answer.substring(indexa + 7, indexb - 3);
- overview += ", ";
- indexa = answer.indexOf("description");
- indexb = answer.indexOf("icon");
- overview += answer.substring(indexa + 14, indexb - 3);
- Serial.println(overview);
- String wind = "wind: ";
- indexa = answer.indexOf("speed");
- indexb = answer.indexOf("deg");
- wind += answer.substring(indexa + 7, indexb - 3);
- wind +="m/s,deg ";
- indexa = answer.indexOf("deg");
- indexb = answer.indexOf("rain");
- wind += answer.substring(indexa + 5, indexb - 3);
- Serial.println(wind);
- String temperature = "Temperature: ";
- indexa = answer.indexOf("temp");//Find on answer an índice that starts with temp
- String stempk = answer.substring(indexa + 6, indexa + 13);//Receive from answer a String with the temperature value in Kelvin
- int tempk = stempk.toInt();//Change the stempk to integer
- int tempc = tempk - 273;//Transform the temperature to Celsius
- temperature += tempc;
- temperature += " oC";
- Serial.println(temperature);
- String humidity = "humidity: ";
- indexa = answer.indexOf("humidity");
- indexb = answer.indexOf("temp_min");
- humidity += answer.substring(indexa + 10, indexb - 2);
- humidity += " %";
- Serial.println(humidity);
- //Serial.print("rain: ");
- //indexa = answer.indexOf("rain");
- //String rain = answer.substring(indexa + 12, indexa + 17);
- //Serial.println(rain);
- Serial.print("\n\n\n\n");
- oled.setFont(ArialMT_Plain_10);
- oled.setTextAlignment(TEXT_ALIGN_LEFT);
- oled.drawString(0, 0, overview);
- oled.drawString(0, 12, wind);
- oled.drawString(0, 24, temperature);
- oled.drawString(0, 36, humidity);
- oled.display();
- oled.clear();
- }
- delay(60000);
- answer = "";
- }
复制代码 |
|