洪嵐峰 發表於 2023-4-30 09:54:23

ESP32-D2WD

所需零件:

ESP32-D2WD 開發板
電源供應器
LED 顯示器
按鈕開關
杜邦線
電阻器

連結腳位:

以下是ESP32-D2WD開發板與其他零件的連接腳位:

LED 顯示器:正極連接到 ESP32 的 D4 腳位,負極接地。

按鈕開關:一端連接到 ESP32 的 D3 腳位,另一端連接到 ESP32 的 GND 腳位。

此外,為了穩定按鈕訊號,需要再連接一個 10K 的電阻,將其一端接到 D3 腳位,另一端接到 ESP32 的 3.3V 腳位。

電源供應器:將其正極接到 ESP32 的 VIN 腳位,負極接地。

程式碼:

以下是ESP32-D2WD 定時開關附顯示即時時間的程式碼:


#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WebServer.h>
#include <WiFiManager.h>
#include <time.h>

// WiFiManager
WiFiManager wifiManager;

// WebServer
WebServer server(80);

// Time variables
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 28800;
const int   daylightOffset_sec = 0;
struct tm timeinfo;

// Pin definitions
const int LED_PIN = 4;
const int BUTTON_PIN = 3;

// State variables
int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

// Schedule variables
bool isEnabled = true;
int onHour = 8;
int onMinute = 0;
int offHour = 18;
int offMinute = 0;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Configure LED and button pins
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);

  // Set up WiFiManager
  wifiManager.autoConnect("AutoConnectAP");

  // Initialize time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
  }

  // Set up web server
  server.on("/", handleRoot);
  server.on("/schedule", handleSchedule);
  server.begin();

  // Turn on LED if current time is within schedule
  if (isEnabled && isWithinSchedule(timeinfo)) {
    ledState = HIGH;
  }
}

void loop() {
  // Update time
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
  }

  // Handle button
  int reading = digitalRead(BUTTON_PIN);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() -

lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
// Toggle schedule on/off
isEnabled = !isEnabled;
if (isEnabled) {
// Turn on LED if current time is within schedule
if (isWithinSchedule(timeinfo)) {
ledState = HIGH;
}
} else {
// Turn off LED
ledState = LOW;
}
}
}
}
lastButtonState = reading;

// Update LED state if necessary
if (isEnabled && isWithinSchedule(timeinfo)) {
ledState = HIGH;
} else {
ledState = LOW;
}

// Update LED
digitalWrite(LED_PIN, ledState);

// Handle web server requests
server.handleClient();
}

bool isWithinSchedule(struct tm t) {
int h = t.tm_hour;
int m = t.tm_min;
if ((h > onHour || (h == onHour && m >= onMinute)) && (h < offHour || (h == offHour && m < offMinute))) {
return true;
} else {
return false;
}
}

void handleRoot() {
String html = "<html><body>";
html += "<h1>ESP32-D2WD Timer</h1>";
html += "<p>Current time: " + getTimeString(timeinfo) + "</p>";
html += "<p>Schedule is " + (isEnabled ? "enabled" : "disabled") + "</p>";
html += "<p>LED is " + (ledState == HIGH ? "on" : "off") + "</p>";
html += "<p><a href='/schedule'>Edit schedule</a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}

void handleSchedule() {
String html = "<html><body>";
html += "<h1>Edit Schedule</h1>";
html += "<form method='post' action='/schedule'>";
html += "<p>On time: <input type='text' name='onHour' value='" + String(onHour) + "'>:<input type='text' name='onMinute' value='" + String(onMinute) + "'></p>";
html += "<p>Off time: <input type='text' name='offHour' value='" + String(offHour) + "'>:<input type='text' name='offMinute' value='" + String(offMinute) + "'></p>";
html += "<p><input type='submit' value='Save'></p>";
html += "</form>";
html += "</body></html>";
if (server.method() == HTTP_POST) {
onHour = server.arg("onHour").toInt();
onMinute = server.arg("onMinute").toInt();
offHour = server.arg("offHour").toInt();
offMinute = server.arg("offMinute").toInt();
}
server.send(200, "text/html", html);
}

String getTimeString(struct tm t) {
String s = "";
s += (t.tm_hour < 10 ? "0" : "") + String(t.tm_hour);
s += ":";
s += (t.tm_min < 10 ? "0" : "") + String(t.tm_min);
s += ":";
s += (t.tm_sec < 10 ? "0" : "") + String(t.tm_sec);
return s;
}



程式碼中定義了用於控制LED的輸出引腳、按鈕的輸入引腳、開啟和關閉時間、以及緩衝和去彈跳的變數。

它還定義了處理HTTP請求的函數,以顯示當前時間和編輯時間表的HTML頁面。

程式碼的主要部分是setup()函數和loop()函數。setup()函數在啟動時執行,初始化串口、輸出引腳和Web服務器。

它還從NTP服務器獲取當前時間,並設置按鈕的初始狀態。

loop()函數在每個迴圈中執行,處理按鈕和LED的狀態,並檢查是否處於開啟或關閉時間內。

如果按鈕被按下,它會切換時間表的啟用狀態,並根據當前時間和時間表狀態更新LED的狀態。

如果啟用時間表,它會檢查當前時間是否在開啟和關閉時間內,並相應地更新LED的狀態。

最後,它處理Web服務器請求,並顯示當前時間和時間表狀態的HTML頁面。

這個程式碼實現了定時開關系統,它可以通過按鈕或Web界面來啟用或禁用時間表,並顯示當前時間和LED的狀態。

如果您想要實現類似的系統,請確保您已經理解了程式碼的細節,以及所使用的硬件和庫的功能和限制。
頁: [1]
查看完整版本: ESP32-D2WD