這是源碼 視頻可以去我B站看
IOT-Based-Weather-Station-NodeMCU-with-OLED-OpenWeatherMap-master.zip
(345.8 KB, 下載次數: 147)
2019-10-16 21:19 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
- /**********************************************************************
- // 項目:簡易氣象臺
- // 硬件:適用于NodeMCU ESP8266 + SSD1306
- // 功能:連接WiFi后獲取相關數據并在OLED屏上顯示
- // 原版作者:How To Electronics
- // 魔改:油管機器貓 bilibili UID:16872024
- // 日期:2019/10/04
- // 硬件連接說明:
- // OLED --- ESP8266
- // VCC --- 3V(3.3V)
- // GND --- G (GND)
- // SDA --- D2(GPIO4)
- // SCL --- D3(GPIO0)
- **********************************************************************/
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h> // http web access library
- #include <ArduinoJson.h> // JSON decoding library 注意:ArduinoJson庫5.13.5測試有效
- // Libraries for SSD1306 OLED display
- #include <Wire.h> // include wire library (for I2C devices such as the SSD1306 display)
- #include <Adafruit_GFX.h> // include Adafruit graphics library
- #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver
-
- #define OLED_RESET 5 // define SSD1306 OLED reset at ESP8266 GPIO5 (NodeMCU D1)
- Adafruit_SSD1306 display(OLED_RESET);
-
- // set Wi-Fi SSID and password
- const char *ssid = "*******";
- const char *password = "*******";
-
- // 設置當地城市和API
- String Location = "Chengdu, CN";//這里我以成都為例
- String API_Key = "*************";// 獲取API密匙網址:openweathermap點org/price
- //注冊,登陸 獲取密匙 搜索城市 操作簡單
- void setup(void)
- {
- Serial.begin(115200);
- delay(1000);
-
- Wire.begin(4, 0); // set I2C pins [SDA = GPIO4 (D2), SCL = GPIO0 (D3)], default clock is 100kHz
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
- Wire.setClock(400000L); // set I2C clock to 400kHz
-
- display.clearDisplay();
- display.setTextColor(WHITE, BLACK);
- display.setTextSize(0.1);
- display.setCursor(0, 0);
- display.println(" Weather Station ");
- display.print("bilibili-Doraemon ");
- display.display();
-
- WiFi.begin(ssid, password);
-
- Serial.print("Connecting.");
- display.setCursor(0, 36);
- display.println("Connecting...");
- display.display();
- while ( WiFi.status() != WL_CONNECTED )
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("connected");
- display.print("connected");
- display.display();
- delay(1000);
-
- display.clearDisplay();
- display.display();
- display.drawCircle(60,30,30,WHITE);
- display.fillCircle(50,20,5,WHITE);
- display.fillCircle(70,20,5,WHITE);
- display.setTextSize(1);
- display.setCursor(38,40);
- display.print("Doraemon");
- display.display();
- delay(1000);
- display.clearDisplay();
- display.setTextSize(2);
- display.setCursor(0, 0);
- display.println("Doraemon");
-
- }
-
- void loop()
- {
- if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
- {
- HTTPClient http; //Declare an object of class HTTPClient
-
- // specify request destination
- http.begin("api.openweathermap點org/data/2.5/weather?q=" + Location + "&APPID=" + API_Key); // !!
-
- int httpCode = http.GET(); // send the request
-
- if (httpCode > 0) // check the returning code
- {
- String payload = http.getString(); //Get the request response payload
-
- DynamicJsonBuffer jsonBuffer(512);
-
- // Parse JSON object
- JsonObject& root = jsonBuffer.parseObject(payload);
- if (!root.success()) {
- Serial.println(F("Parsing failed!"));
- return;
- }
-
- float temp = (float)(root["main"]["temp"]) - 273.15; // get temperature in °C
- int humidity = root["main"]["humidity"]; // get humidity in %
- float pressure = (float)(root["main"]["pressure"]) / 1000; // get pressure in bar
- float wind_speed = root["wind"]["speed"]; // get wind speed in m/s
- int wind_degree = root["wind"]["deg"]; // get wind degree in °
-
- // print data
- Serial.printf("Temperature = %.2f°C\r\n", temp);
- Serial.printf("Humidity = %d %%\r\n", humidity);
- Serial.printf("Pressure = %.3f bar\r\n", pressure);
- Serial.printf("Wind speed = %.1f m/s\r\n", wind_speed);
- Serial.printf("Wind degree = %d°\r\n\r\n", wind_degree);
-
- display.setTextSize(0.1);
- display.setCursor(0, 24);
- display.printf("Temperature: %5.2f C\r\n", temp);
- display.printf("Humidity : %d %%\r\n", humidity);
- display.printf("Pressure : %.3fbar\r\n", pressure);
- display.printf("Wind speed : %.1f m/s\r\n", wind_speed);
- display.printf("Wind degree: %d", wind_degree);
- display.drawRect(109, 24, 3, 3, WHITE); // put degree symbol ( ° )
- display.drawRect(97, 56, 3, 3, WHITE);
- display.display();
-
- }
-
- http.end(); //Close connection
-
- }
-
- delay(60000); // wait 1 minute 每分鐘刷新數據 一次
-
- }
- //結束
復制代碼
|