ESP8266+oled连接心知天气显示时间和实时天气

因为时间比较仓促,目前存在许多问题没有解决。

首先进行aduino环境配置,具体方法可参考以下链接,若选错开发板型号编译会报错配置esp8266开发板的Arduino开发环境_交通运输怪味张的博客-CSDN博客_esp8266arduino

心知天气网址:https://www.seniverse.com/

进入心知天气注册账号,之后进入产品文档,便可以查看API接口,也就是我们需要请求访问的网址。

 查看自己的私钥:

 

 

 获取信息后可尝试浏览器输入API网址查看网址内容。

接下来是esp8266程序。

 

 

需要安装Arduino_JSON库和u8g2库,分别用来解析JSON数据和oled显示,具体方法点击菜单栏 工具->管理库... 搜索框搜索对应库便可以安装。头文

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

檀苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值