#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Replace with your own OpenWeatherMap API key
const char* apiKey = "your_API_KEY";
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
String city = "Beijing"; // Replace with your city name or ID
int refreshInterval = 300; // Refresh interval in seconds
unsigned long lastRefreshTime = 0;
struct WeatherData {
String description;
int temperature;
int humidity;
int pressure;
};
WeatherData weatherData;
void setup() {
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
// Connect to Wi-Fi
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Get initial weather data
getWeatherData();
}
void loop() {
unsigned long currentTime = millis() / 1000;
if (currentTime - lastRefreshTime >= refreshInterval) {
// Refresh weather data
getWeatherData();
}
// Clear the display buffer
display.clearDisplay();
// Draw weather data on the screen
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("City: ");
display.println(city);
display.print("Temperature: ");
display.print(weatherData.temperature);
display.println("C");
display.print("Humidity: ");
display.print(weatherData.humidity);
display.println("%");
display.print("Pressure: ");
display.print(weatherData.pressure);
display.println("hPa");
display.print("Description: ");
display.println(weatherData.description);
// Update the display
display.display();
}
void getWeatherData() {
WiFiClientSecure client;
client.setInsecure(); // Don't verify SSL certificate (for demo purposes only)
if (!client.connect("api.openweathermap.org", 443)) {
Serial.println("Failed to connect to OpenWeatherMap!");
return;
}
String url = "/data/2.5/weather?q=" + city + "&units=metric&appid=" + apiKey;
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: api.openweathermap.org\r\n" +
"Connection: close\r\n\r\n");
while (!client.available()) {
delay(10);
}
String response = "";
while (client.available()) {
char c = client.read();
response += c;
}
int start = response.indexOf('{');
int end = response.lastIndexOf('}');
if (start == -1 || end == -1) {
Serial.println("Invalid response from OpenWeatherMap!");
return;
}
response = response.substring(start, end+1);
// Parse JSON data
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
JsonObject main = doc["main"];
weatherData.temperature = main["temp"];
weatherData.humidity = main["humidity"];
weatherData.pressure = main["pressure"];
JsonArray weather = doc["weather"];
JsonObject firstWeather = weather[0];
weatherData.description = firstWeather["description"];
lastRefreshTime = millis() / 1000;
}
这个代码片段将在OLED屏幕上显示来自OpenWeatherMap API的天气数据,包括城市名称、温度、湿度、气压和天气描述。它使用getWeatherData()
函数来获取JSON格式的天气数据,并使用ArduinoJson库解析数据。请注意,在使用此代码之前,您需要将代码中的Wi-Fi网络凭据和OpenWeatherMap API密钥替换为实际值。
将ESP8266-NodeMCU与OLED屏幕连接的步骤如下:
- 将OLED屏幕的VCC引脚连接到ESP8266-NodeMCU的3.3V引脚。
- 将OLED屏幕的GND引脚连接到ESP8266-NodeMCU的GND引脚。
- 将OLED屏幕的SDA引脚连接到ESP8266-NodeMCU的D2引脚。
- 将OLED屏幕的SCL引脚连接到ESP8266-NodeMCU的D1引脚。
注意,这里我们使用了ESP8266-NodeMCU的D1和D2引脚,这是因为它们已预定义为I2C总线的SDA和SCL引脚。如果您使用不同的引脚,请相应地更改代码。