在本篇文章中,我们将学习如何使用ESP8266 NodeMCU(ESP-12E)Wi-Fi开发板和SSD1306 OLED显示屏(128×64像素)制作基于物联网的简单实时气象站。对于想了解有关从在线Internet服务器到NodeMCU板中检索数据的物联网初学者而言,该项目是一个很棒的项目。
NodeMCU从openweathermap.org的天气网站中提取温度、湿度、压力、风速和风向等天气数据,并将其显示在SSD1306液晶显示屏上。
所需的组件 主题很明确,我们只需要该项目的OLED显示屏和NodeMCU wifi模块即可。 ● NodeMCU ESP8266 12E模块 ● 0.96英寸OLED显示屏
物联网气象站的功能框图 以下是物联网气象站的简单框图,仅说明了系统的工作原理。
openweathermap.org网站提供了有关天气数据和天气预报的在线服务。数据上传到云服务器上。 NodeMCU ESP8266 Wifi模块在线收集数据信息,并下载/检索温度、湿度、压力、风速和风向等信息。
这些数据显示在0.96英寸SSD1306 OLED显示屏上,同时显示了所在的城市名称。
连接电路图 使用NodeMCU和OpenWeatherMap制作的物联网气象站连接电路图:
I2C总线的SDA和SCL线分别连接NodeMCU板的GPIO4(D2)和GPIO0(D3),然后连接到SSD1306显示模块的SDA和SCL(SCK)引脚。 OLED显示模块由NodeMCU板的Vin引脚提供5V电压。
从OpenWeatherMap获取API OpenWeatherMap访问任何位置的当前天气数据,包括全球200,000多个城市。超过40,000个气象站更新当前天气。数据以JSON、XML或HTML格式提供。本文将使用JSON格式的数据。
访问https://openweathermap.org,并使用您的帐户登录。
根据检索数据的需要创建或生成您的API密钥。
通过输入城市名称选择城市和国家/地区代码。示例:Jaipur,IN,Jaipur是一个城市,IN是印度(India)的国家代码。必须在代码中输入城市名称和国家/地区代码。
源代码/程序 以下是使用NodeMCU和OpenWeatherMap制作的物联网气象站的代码。为此,您需要3个不同的库,即Adafruit_GFX.h、Adafruit_SSD1306.h和ArduinoJson.h。您可以从“库管理器”中获取所有这些库。
输入WIFI SSID、密码和国家/地区代码、城市名称。现在,您可以将此代码上传到NodeMCU开发板。 - #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h> // http web access library
- #include <ArduinoJson.h> // JSON decoding library
- // 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 = "Your Wifi SSID";
- const char *password = "Your Wifi Password";
- // set location and API key
- String Location = "City Name, Country Code";
- String API_Key = "Your API Key";
- void setup(void)
- {
- Serial.begin(9600);
- delay(1000);
- Wire.begin(4, 0); // set I2C pins [SDA = GPIO4 (D2), SCL = GPIO0 (D3)], default clock is 100kHz
- // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
- // init done
- Wire.setClock(400000L); // set I2C clock to 400kHz
- display.clearDisplay(); // clear the display buffer
- display.setTextColor(WHITE, BLACK);
- display.setTextSize(1);
- display.setCursor(0, 0);
- display.println(" Internet Weather");
- display.print(" Station - Jaipur");
- display.display();
- WiFi.begin(ssid, password);
- Serial.print("Connecting.");
- display.setCursor(0, 24);
- display.println("Connecting...");
- display.display();
- while ( WiFi.status() != WL_CONNECTED )
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("connected");
- display.print("connected");
- display.display();
- delay(1000);
- }
- void loop()
- {
- if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
- {
- HTTPClient http; //Declare an object of class HTTPClient
- // specify request destination
- http.begin("http://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.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
- }
- // End of code.
复制代码
|