ESP8266,4针oled,I2C协议网络天气时钟代码
时间: 2023-10-09 15:10:09 浏览: 77
以下是一个ESP8266控制的4针OLED,使用I2C协议连接到网络,显示天气和时钟的代码示例:
```c++
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TimeLib.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char *ssid = "your_wifi_ssid";
const char *password = "your_wifi_password";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
const char *host = "api.openweathermap.org";
const char *apiKey = "your_openweathermap_api_key";
const char *city = "your_city";
const char *country = "your_country_code";
void setup() {
Serial.begin(115200);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
// Initialize time client
timeClient.begin();
timeClient.setTimeOffset(3600); // Set time zone offset to GMT+1
// Clear display
display.clearDisplay();
}
void loop() {
// Update time
timeClient.update();
int hour = timeClient.getHours();
int minute = timeClient.getMinutes();
int second = timeClient.getSeconds();
// Get weather data
WiFiClient client;
if (!client.connect(host, 80)) {
Serial.println("Connection failed");
return;
}
String url = "/data/2.5/weather?q=" + String(city) + "," + String(country) + "&appid=" + String(apiKey);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
String response = "";
while (client.available()) {
char c = client.read();
response += c;
}
// Parse weather data
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
String description = doc["weather"][0]["description"].as<String>();
int temperature = doc["main"]["temp"].as<int>() - 273; // Convert to Celsius
int humidity = doc["main"]["humidity"].as<int>();
// Display time
display.setCursor(0, 0);
display.setTextColor(WHITE);
display.setTextSize(1);
display.print(hour);
display.print(":");
if (minute < 10) {
display.print("0");
}
display.print(minute);
display.print(":");
if (second < 10) {
display.print("0");
}
display.println(second);
// Display weather
display.setCursor(0, 10);
display.print(description);
display.setCursor(0, 20);
display.print(temperature);
display.print("C ");
display.print(humidity);
display.println("%");
// Update display
display.display();
// Wait for next update
delay(1000);
}
```
在这个示例中,我们使用了一个名为```Adafruit_SSD1306```的库来控制OLED显示器。我们还使用了```TimeLib```库来获取当前时间,并使用```NTPClient```库来获取网络时间。我们通过```WiFiClient```连接到OpenWeatherMap API来获取天气数据,并使用```ArduinoJson```库解析JSON响应。最后我们将时间和天气信息显示在OLED上。
请注意,您需要将```your_wifi_ssid```和```your_wifi_password```替换为您的WiFi凭据,将```your_openweathermap_api_key```替换为您的OpenWeatherMap API密钥,将```your_city```替换为您所在的城市,将```your_country_code```替换为您所在的国家代码。