ESP8266+OLED屏实现cube3D效果展示

438 篇文章 1621 订阅 ¥19.90 ¥99.00
253 篇文章 511 订阅 ¥19.90 ¥99.00
49 篇文章 23 订阅
本文展示了如何使用ESP8266和1.3寸OLED屏幕创建Cube3D效果。编程环境基于Arduino,屏幕通过IIC接口连接,开发板为ESP8266。文章附带了0.96寸屏幕的代码选项及3D立方体动画的演示。
摘要由CSDN通过智能技术生成

ESP8266+OLED屏实现cube3D效果展示


  • 使用的是esp8266,
  • 编程环境:Arduino IDE
  • 使用的oled屏幕为1.3寸的,代码中有0.96寸的代码可以选择,屏幕接口为IIC接口
  • 选择的开发板是:NodeMCU1.0
    效果展示

ESP8266+OLED屏cube3D效果展示

程序源码:

#include "SH1106Wire.h" //1.3寸用这个
//#include "SSD1306W
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值