ESP8266,4针0.96寸oled,I2C协议,我所在的城市是四川德阳,WIFI名称是MERCURY_AB66,WIFI密码是qwertyuiop123网络天气时钟代码
时间: 2023-10-24 12:05:37 浏览: 157
天气播报oled网络时钟0.96寸oled显示esp8266
很好,以下是你需要的ESP8266 + 0.96寸OLED I2C网络天气时钟代码。你只需要将WIFI名称和密码替换为你的即可。
```
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <JsonListener.h>
#include <JsonStreamingParser.h>
// Replace with your network credentials
const char* ssid = "MERCURY_AB66";
const char* password = "qwertyuiop123";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Initialize the OLED display using Adafruit SSD1306 library
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);
// Weather API information
String apiKey = "YOUR_API_KEY";
String city = "YOUR_CITY";
// Variables to hold weather data
float temp;
float humidity;
float pressure;
String weatherDescription;
// Helper function to get weather data from OpenWeatherMap API
void getWeatherData() {
WiFiClient client;
const int httpPort = 80;
if (!client.connect("api.openweathermap.org", httpPort)) {
Serial.println("connection failed");
return;
}
client.println("GET /data/2.5/weather?q=" + city + "&units=metric&appid=" + apiKey);
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
String line;
while (client.connected()) {
line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String jsonPayload = "";
while (client.available()) {
jsonPayload += (char)client.read();
}
client.stop();
// Parse JSON payload and extract weather data
JsonStreamingParser parser;
JsonListener *listener = new JsonListener();
parser.setListener(listener);
char jsonChar[jsonPayload.length()+1];
jsonPayload.toCharArray(jsonChar, jsonPayload.length()+1);
parser.parse(jsonChar, strlen(jsonChar));
temp = listener->temp;
humidity = listener->humidity;
pressure = listener->pressure;
weatherDescription = listener->weatherDescription;
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
// Initialize and clear the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
// Initialize NTP client
timeClient.begin();
timeClient.setTimeOffset(7200);
// Get current time from NTP server
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// Get initial weather data
getWeatherData();
}
void loop() {
// Update time from NTP server every 10 minutes
if (timeClient.getMinutes() % 10 == 0) {
timeClient.update();
}
// Get weather data every hour
if (timeClient.getMinutes() == 0) {
getWeatherData();
}
// Set up display
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,0);
// Print time and date
String timeString = String(hour()) + ":" + String(minute()) + ":" + String(second());
String dateString = String(day()) + "/" + String(month()) + "/" + String(year());
display.setTextSize(2);
display.print(timeString);
display.setTextSize(1);
display.print(" ");
display.println(dateString);
// Print weather data
display.setCursor(0, 30);
display.setTextSize(1);
display.print("Temperature: ");
display.println(temp, 1);
display.print("Humidity: ");
display.println(humidity, 1);
display.print("Pressure: ");
display.println(pressure, 1);
display.print("Weather: ");
display.println(weatherDescription);
// Display everything
display.display();
// Wait for one second before updating display again
delay(1000);
}
```
请注意,你需要将YOUR_API_KEY和YOUR_CITY替换为你的OpenWeatherMap API密钥和城市。另外,代码中的时间偏移量设置为7200秒,适用于UTC + 2时区,如果你所在的时区不同,请相应地更改。
阅读全文