esp8266天气时钟arduino代码
时间: 2023-09-28 17:13:27 浏览: 47
以下是一个简单的 ESP8266 天气时钟的 Arduino 代码:
```
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Replace with your own API key, obtained from https://openweathermap.org/api
const char* apiKey = "your_API_key";
const char* city = "your_city_name";
const int UPDATE_INTERVAL = 60000; // Update interval in milliseconds
// OLED display settings
#define OLED_RESET 0
Adafruit_SSD1306 display(OLED_RESET);
String weatherDescription;
int currentTemp = 0;
int minTemp = 0;
int maxTemp = 0;
int humidity = 0;
void setup() {
Serial.begin(115200);
// Initialize the OLED display
display.begin(SSD1306_SWITCHCAPVCC, 128, 64);
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to Wi-Fi");
// Set up HTTP client
HTTPClient http;
// Make API request
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "&units=metric&appid=" + String(apiKey);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
// Parse JSON response
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
weatherDescription = doc["weather"][0]["description"];
currentTemp = doc["main"]["temp"];
minTemp = doc["main"]["temp_min"];
maxTemp = doc["main"]["temp_max"];
humidity = doc["main"]["humidity"];
} else {
Serial.println("Error getting weather data");
}
// Set up update timer
timer.setInterval(UPDATE_INTERVAL, updateWeather);
}
void loop() {
// Update the display
display.clearDisplay();
display.setCursor(0,0);
display.print("Weather in " + String(city));
display.setCursor(0,10);
display.print("Current temp: " + String(currentTemp) + "C");
display.setCursor(0,20);
display.print("Min temp: " + String(minTemp) + "C");
display.setCursor(0,30);
display.print("Max temp: " + String(maxTemp) + "C");
display.setCursor(0,40);
display.print("Humidity: " + String(humidity) + "%");
display.display();
// Update the weather data
timer.run();
}
void updateWeather() {
// Set up HTTP client
HTTPClient http;
// Make API request
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "&units=metric&appid=" + String(apiKey);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
// Parse JSON response
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
weatherDescription = doc["weather"][0]["description"];
currentTemp = doc["main"]["temp"];
minTemp = doc["main"]["temp_min"];
maxTemp = doc["main"]["temp_max"];
humidity = doc["main"]["humidity"];
} else {
Serial.println("Error getting weather data");
}
}
```
这个代码使用了一个 OLED 显示屏来显示天气信息,使用了 OpenWeatherMap API 来获取天气数据。请注意,你需要将代码中的网络凭据和 API 密钥替换为你自己的凭据。也可以根据需要更改更新时间间隔。