STM32按键实验入门:GPIO输入模式与软件消抖实战解析
2026/10/15 17:40:49
在智能家居设备日益普及的今天,一个能够实时显示时间、天气和提醒事项的信息中心成为了许多科技爱好者的DIY首选。本文将带你从硬件选型到软件配置,完整构建一个基于ESP8266和OLED显示屏的智能信息中心。
ESP8266开发板是项目的核心大脑,推荐使用NodeMCU或D1 mini版本,它们内置了USB转串口芯片,方便编程调试。ESP8266具有以下优势:
OLED显示屏建议选择0.96寸I2C接口的SSD1306型号,它具有:
除了核心组件,还需要准备:
按照以下方式连接ESP8266与OLED:
| ESP8266引脚 | OLED引脚 | 备注 |
|---|---|---|
| 3.3V | VCC | 电源正极 |
| GND | GND | 电源地线 |
| D1 (GPIO5) | SCL | I2C时钟线 |
| D2 (GPIO4) | SDA | I2C数据线 |
注意:不同型号的ESP8266开发板引脚定义可能略有差异,建议查阅具体开发板的引脚图确认连接。
http://arduino.esp8266.com/stable/package_esp8266com_index.json通过库管理器安装以下关键库:
安装完成后,重启Arduino IDE使更改生效。
首先实现基本的网络连接和时间同步功能:
#include <ESP8266WiFi.h> #include <NTPClient.h> #include <WiFiUdp.h> const char* ssid = "你的WiFi名称"; const char* password = "你的WiFi密码"; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org", 8*3600, 60000); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } timeClient.begin(); } void loop() { timeClient.update(); Serial.print("当前时间: "); Serial.println(timeClient.getFormattedTime()); delay(1000); }实现OLED屏幕的基本文本显示:
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Hello, OLED!"); display.display(); } void loop() { // 显示内容更新放在这里 }使用心知天气API获取实时天气信息:
#include <ESP8266HTTPClient.h> #include <ArduinoJson.h> const String apiKey = "你的API密钥"; const String location = "城市名称"; void getWeather() { HTTPClient http; String url = "http://api.seniverse.com/v3/weather/now.json?key=" + apiKey + "&location=" + location + "&language=zh-Hans"; http.begin(url); int httpCode = http.GET(); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); DynamicJsonDocument doc(1024); deserializeJson(doc, payload); String weatherText = doc["results"][0]["now"]["text"]; String temperature = doc["results"][0]["now"]["temperature"]; Serial.print("天气: "); Serial.println(weatherText); Serial.print("温度: "); Serial.println(temperature); } http.end(); }设计一个包含多个信息页面的显示系统:
使用按钮或定时切换这些页面:
#define BUTTON_PIN D3 int currentPage = 0; unsigned long lastPageChange = 0; void checkButton() { if(digitalRead(BUTTON_PIN) == LOW) { if(millis() - lastPageChange > 200) { // 防抖 currentPage = (currentPage + 1) % 3; lastPageChange = millis(); } } } void displayPage() { display.clearDisplay(); switch(currentPage) { case 0: // 时钟页面 displayTime(); break; case 1: // 天气页面 displayWeather(); break; case 2: // 提醒页面 displayReminders(); break; } display.display(); }为延长设备续航时间,可实施以下优化:
#define SLEEP_DURATION 300e6 // 5分钟 void enterDeepSleep() { Serial.println("进入深度睡眠"); ESP.deepSleep(SLEEP_DURATION); }添加DHT11传感器监测环境数据:
连接DHT11到ESP8266:
代码实现:
#include <DHT.h> #define DHTPIN D5 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void readDHT() { float h = dht.readHumidity(); float t = dht.readTemperature(); if (!isnan(h) && !isnan(t)) { Serial.print("湿度: "); Serial.print(h); Serial.print("%"); Serial.print(" 温度: "); Serial.print(t); Serial.println("°C"); } }实现通过WiFi进行固件更新:
#include <ESP8266mDNS.h> #include <WiFiUdp.h> #include <ArduinoOTA.h> void setupOTA() { ArduinoOTA.setHostname("ESP8266_InfoCenter"); ArduinoOTA.onStart([]() { Serial.println("开始OTA更新"); }); ArduinoOTA.onEnd([]() { Serial.println("\n更新完成"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("进度: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("错误[%u]: ", error); }); ArduinoOTA.begin(); } void loop() { ArduinoOTA.handle(); // 其他循环代码 }设计一个简洁的外壳需要考虑:
可以使用免费工具如Tinkercad或Fusion 360进行设计,然后通过3D打印机实现。
根据使用场景选择合适的供电方式:
完成组装后,进行以下检查:
通过串口监视器观察系统运行状态,及时调整参数优化性能。