冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 680|回覆: 0
打印 上一主題 下一主題

[討論] ESP32-D2WD

[複製鏈接]

2609

主題

0

好友

945

積分

高級會員

Rank: 4

UID
373967
帖子
7386
主題
2609
精華
0
積分
945
楓幣
946
威望
925
存款
26000
贊助金額
0
推廣
0
GP
1205
閱讀權限
50
在線時間
405 小時
註冊時間
2023-1-12
最後登入
2024-4-28

2023端午節紀念勳章 2023中秋節紀念勳章 2023聖誕節紀念勳章

跳轉到指定樓層
1
發表於 2023-4-30 09:54:23 |只看該作者 |倒序瀏覽
所需零件:

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的狀態。

如果您想要實現類似的系統,請確保您已經理解了程式碼的細節,以及所使用的硬件和庫的功能和限制。
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-28 12:31

回頂部