用ESP8266连接到0.96英寸OLED屏幕,并显示来自OpenWeatherMap API的天气数据

#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屏幕连接的步骤如下:

  1. 将OLED屏幕的VCC引脚连接到ESP8266-NodeMCU的3.3V引脚。
  2. 将OLED屏幕的GND引脚连接到ESP8266-NodeMCU的GND引脚。
  3. 将OLED屏幕的SDA引脚连接到ESP8266-NodeMCU的D2引脚。
  4. 将OLED屏幕的SCL引脚连接到ESP8266-NodeMCU的D1引脚。

注意,这里我们使用了ESP8266-NodeMCU的D1和D2引脚,这是因为它们已预定义为I2C总线的SDA和SCL引脚。如果您使用不同的引脚,请相应地更改代码。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值