ESP8266与SH OLED屏幕实现天气预报、温度显示和NTP时间同步的6屏幕带中文显示V版 Arduino项目

62 篇文章 19 订阅 ¥59.90 ¥99.00
本教程详细介绍了如何利用ESP8266和SH OLED屏幕创建一个Arduino项目,该项目可以显示天气预报、实时温度,并通过NTP同步时间,还支持中文显示。通过连接硬件、编写代码并上传,即可实现功能丰富的显示屏。
摘要由CSDN通过智能技术生成

在本文中,我们将介绍如何使用ESP8266和SH OLED屏幕来创建一个功能强大的Arduino项目,该项目能够显示天气预报、温度,并通过NTP进行时间同步。我们还将添加中文显示功能,并提供相应的源代码。让我们开始吧!

材料清单:

  • 1个ESP8266开发板
  • 6个SH OLED屏幕
  • 杜邦线若干
  • 1个温度传感器(例如DS18B20)

步骤1:硬件连接
首先,将ESP8266开发板连接到计算机,并通过Arduino IDE或其他适当的软件进行编程。然后,按照以下连接方式将SH OLED屏幕连接到ESP8266开发板:

  • 将OLED屏幕的VCC引脚连接到ESP8266的3.3V引脚。
  • 将OLED屏幕的GND引脚连接到ESP8266的GND引脚。
  • 将OLED屏幕的SCL引脚连接到ESP8266的D1引脚。
  • 将OLED屏幕的SDA引脚连接到ESP8266的D2引脚。

还需将温度传感器连接到ESP8266开发板。将传感器的VCC引脚连接到ESP8266的3.3V引脚,GND引脚连接到ESP8266的GND引脚,数据引脚连接到ESP8266的D3引脚。确保所有连接正确无误后,我们可以开始编写代码。

步骤2:编写代码
以下是实现天气预报、温度显示和NTP时间同步的Arduino代码示例:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很高兴听到您对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); } ``` 这只是一个简单的程序示例,您可以根据自己的需要进行修改和扩展。希望对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值