ESP8266+OLED显示实时时间

话不多说上代码

放在此处方便参考,并自己保存用
调用函数代码:

void genxin(){
  int httpCode = http.GET();
  if (httpCode > 0){
  if (httpCode == HTTP_CODE_OK){
        response = http.getString();
        day = response.substring(13,23);
        date = response.substring(24,32);
        date0 = response.substring(24,26);
        date1 = response.substring(27,29);
        date2 = response.substring(30,32);
        shi  = date0.toInt();
        fen  = date1.toInt();
        miao = date2.toInt();
    }
  }
http.end();
}

接线:
SCL接D2、SDA接D1、VCC接3.3V、GND接地。
全部代码:ESP8266获取网络时间显示到OLED

运行效果:请添加图片描述

请添加图片描述

字库链接:字库1 字库2

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴听到您对ESP8266 OLED屏幕的应用感兴趣。下面是一个简单的程序示例,它可以实现天气预报、温度显示、NTP时间同步和中文显示。 首先,您需要准备以下材料: - ESP8266开发板(如NodeMCU) - OLED屏幕(如0.96英寸128x64 OLED) - DHT11温湿度传感器 - 一些杜邦线 - Arduino IDE(或其他适当的IDE) 接下来,让我们开始编写程序: 1. 引入所需库文件 ```c++ #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> ``` 2. 定义OLED屏幕对象和温湿度传感器对象 ```c++ #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); #define DHTPIN 5 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); ``` 3. 定义WiFi连接信息和NTP服务器信息 ```c++ const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* ntpServerName = "cn.pool.ntp.org"; const int timeZone = 8; WiFiUDP udp; IPAddress localIP; ``` 4. 连接WiFi和获取本地IP地址 ```c++ void connectWiFi() { Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); localIP = WiFi.localIP(); Serial.println(localIP); } ``` 5. 从NTP服务器获取当前时间 ```c++ void getNTPTime() { byte packetBuffer[NTP_PACKET_SIZE]; memset(packetBuffer, 0, NTP_PACKET_SIZE); packetBuffer[0] = 0b11100011; packetBuffer[1] = 0; packetBuffer[2] = 6; packetBuffer[3] = 0xEC; packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; udp.beginPacket(ntpServerName, 123); udp.write(packetBuffer, NTP_PACKET_SIZE); udp.endPacket(); delay(1000); int cb = udp.parsePacket(); if (cb) { udp.read(packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long secsSince1900 = highWord << 16 | lowWord; const unsigned long seventyYears = 2208988800UL; unsigned long epoch = secsSince1900 - seventyYears; epoch += timeZone * SECS_PER_HOUR; setTime(epoch); } } ``` 6. 定义获取天气信息的函数 ```c++ void getWeather() { WiFiClient client; const int httpPort = 80; if (!client.connect("api.openweathermap.org", httpPort)) { Serial.println("connection failed"); return; } String url = "/data/2.5/weather?q=Beijing,cn&lang=zh_cn&units=metric&appid=your_APPID"; Serial.print("Requesting URL: "); Serial.println(url); 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(100); } String line; while (client.available()) { line = client.readStringUntil('\r'); if (line.indexOf("\"temp\":") >= 0) { String temp = line.substring(line.indexOf("\"temp\":") + 7, line.indexOf(",")); Serial.println("Temperature: " + temp + "°C"); display.setCursor(0, 10); display.print("Temperature: "); display.print(temp); display.print("C"); } if (line.indexOf("\"description\":") >= 0) { String desc = line.substring(line.indexOf("\"description\":") + 15, line.indexOf("\",")); Serial.println("Description: " + desc); display.setCursor(0, 20); display.print("Description: "); display.print(desc); } } } ``` 7. 在setup函数中初始化各组件 ```c++ void setup() { Serial.begin(115200); dht.begin(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.display(); delay(2000); display.clearDisplay(); connectWiFi(); udp.begin(2390); setSyncProvider(getNTPTime); display.setCursor(0, 0); display.print("IP address: "); display.println(localIP); display.display(); delay(2000); display.clearDisplay(); display.setCursor(0, 0); display.print("NTP time: "); display.println(getFormattedTime()); display.display(); delay(2000); display.clearDisplay(); getWeather(); } ``` 8. 在loop函数中更新温湿度和时间 ```c++ void loop() { delay(2000); float t = dht.readTemperature(); float h = dht.readHumidity(); display.setCursor(0, 30); display.print("Temp: "); display.print(t); display.print("C"); display.setCursor(0, 40); display.print("Humidity: "); display.print(h); display.print("%"); display.setCursor(0, 50); display.print("Time: "); display.println(getFormattedTime()); display.display(); } ``` 9. 定义一个辅助函数,将当前时间格式化为字符串 ```c++ String getFormattedTime() { char buffer[20]; sprintf(buffer, "%02d:%02d:%02d", hour(), minute(), second()); return String(buffer); } ``` 这只是一个简单的程序示例,您可以根据自己的需要进行修改和扩展。希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值