本文將介紹如何制作一款基于ESP32,并經由WiFi網絡實現的門鈴。
項目起因
今年新家裝修的時候,我忽略了門鈴的問題。原來門邊上設置有一個門鈴按鈕,但由于原來預留的線都被混凝土掩蓋了,沒辦法找到電纜頭在哪里。于是糾結怎么安裝一款門鈴。到網上一搜,目前有WiFi門鈴的解決方案,可以把電鈴放在不同的房間,但價格相當昂貴,而且還需要220v的電源供電。于是我選擇用無線門鈴來解決,但結果證明無線門鈴的運行并不是非常可靠,而且我不怎么喜歡基于電池的解決方案。于是,開始萌生了采用微控制器來解決這個問題,使舊門鈴能復活過來。
項目的思路是這樣的:設置兩片單片機,分別置于室內和室外,當第一個單片機檢測到按鈕上的按鈕動作時,通過WiFi網絡向第二個單片機發送請求。第二個單片機連接到喇叭,當它收到發送的請求時便播放預設的鈴聲。項目中我使用了集成WiFi的Espressif ESP32開發板。為了讓項目更簡單,我還添加了一個便宜的MP3播放器模塊。
原件清單
兩塊 Espressif ESP32開發板 ;
DFPlayer MP3模塊和 SD卡;
4歐姆 3W的喇叭;
2個1K的電阻;
兩個3.3V LED指示燈;
按鈕開關;
面包板。
電路組裝
這一部分比較簡單,沒什么值得注意的。基本上就是把所有的元件放在一起。最后,我把LED燈忘在揚聲器那一側了。(不過,最后代碼中對其進行了處理。)
基于ESP32制作門鈴之發送側
門鈴的外殼是采用3D打印機打印的,分為兩部分,室外部分和室內部分。附件是電路連接圖和外殼的3D打印STL文件,需要可下載:
基于ESP32制作門鈴之發送側實物
基于ESP32制作門鈴之接收側
基于ESP32制作門鈴之接收側實物
代碼
雖然發射端硬件上僅僅是稍微閃爍一下LED,功能比較單一,但接收單元提供了更多的功能,它可以啟動一個小型web服務器,為發射端提供界面,也可以通過瀏覽器直接使用它。它還允許設置揚聲器音量。
ESP32門鈴的設置界面
發送端代碼
/*
* Sources:
* https://www.arduino.cc/en/Tutorial/Button
* https://techtutorialsx.com/2017/05/19/esp32-http-get-requests/
*/
#include
#include
const char* ssid = "WiFi SSID";
const char* password = "WiFi password";
const char* bellUrl = "http://192.168.1.149/bell/on";
const int buttonPin = 21; // the number of the pushbutton pin
const int ledPin = 23; // the number of the LED pin
int buttonState = 0;
void setup() {
Serial.begin(115200);
btStop(); // turn off bluetooth
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
connectWifi();
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) {
buttonState = digitalRead(buttonPin);
delay(100);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
HTTPClient http;
http.begin(bellUrl);
Serial.print("GET ");
Serial.println(bellUrl);
int httpCode = http.GET();
if (httpCode > 0) {
//String payload = http.getString();
Serial.println(httpCode);
//Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
delay(200);
}
else {
digitalWrite(ledPin, LOW);
}
}
else {
Serial.println("WiFi not connected!");
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
connectWifi();
}
}
void connectWifi() {
boolean ledStatus = false;
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
}
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
接收端代碼
/*
Sources:
https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299
https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_DFPlayer_full/ESP32_DFPlayer_full.ino
https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_DFPlayer_full/setup.png
ESP32 Web Server – Arduino IDE
*/
#include
#include "DFRobotDFPlayerMini.h"
#include
HardwareSerial hwSerial(1);
DFRobotDFPlayerMini dfPlayer;
int volume = 5;
const char* ssid = "WiFi SSID";
const char* password = "WiFi password";
WiFiServer server(80); // Set web server port number to 80
String header;
String ledState = "";
const int ledPin = 26;
unsigned long timestamp = 0;
void setup()
{
btStop(); // turn off bluetooth
hwSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
Serial.begin(115200);
// WiFi & LED ==================================================================
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
delay(500);
dfPlayer.begin(hwSerial); //Use softwareSerial to communicate with mp3
dfPlayer.setTimeOut(500); //Set serial communication time out 500ms
dfPlayer.volume(volume); //Set volume value (0~30).
dfPlayer.EQ(DFPLAYER_EQ_NORMAL);
dfPlayer.outputDevice(DFPLAYER_DEVICE_SD);
ledState = "on";
digitalWrite(ledPin, HIGH);
timestamp = millis();
dfPlayer.play(1); //Play the first mp3
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (ledState == "on" && (millis() - timestamp) > 2000) {
ledState = "off";
digitalWrite(ledPin, LOW);
}
if (client) {
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /bell/on") >= 0) {
ledState = "on";
digitalWrite(ledPin, HIGH);
timestamp = millis();
dfPlayer.play(1); //Play the first mp3
}
else if (header.indexOf("GET /volume/") >= 0) { // yes, I know this is not RESTful
String str1 = header;
str1 = str1.substring(header.indexOf("GET /volume/") + 12);
volume = str1.substring(0, str1.indexOf(" ")).toInt();
if (volume < 0) {
volume = 0;
}
else if (volume > 30) {
volume = 30;
}
dfPlayer.volume(volume);
Serial.print("volume set to ");
Serial.println(volume);
}
// Display the HTML web page
client.println("
");
client.println("
");
client.println("
");
client.println(" html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; }");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 { background-color: #4CAF50; border: none; color: white; padding: 4px 10px; text-decoration: none; margin: 1px; cursor: pointer;} ");
client.println("
LeWe Türklingel
");
client.println("
Volume: ? " + String(volume) + " +
");
client.println("
Klingeln
");
client.println("
dfplayer status: " + printDetail(dfPlayer.readType(), dfPlayer.read()) + "
");
client.println("
LED - State " + ledState + "
");
client.println("");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
String printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
return "Time Out!";
break;
case WrongStack:
return "Stack Wrong!";
break;
case DFPlayerCardInserted:
return "Card Inserted!";
break;
case DFPlayerCardRemoved:
return "Card Removed!";
break;
case DFPlayerCardOnline:
return "Card Online!";
break;
case DFPlayerPlayFinished:
return "Play Finished!";
break;
case DFPlayerError:
switch (value) {
case Busy:
return "Error: Card not found";
break;
case Sleeping:
return "Error: Sleeping";
break;
case SerialWrongStack:
return "Error: Get Wrong Stack";
break;
case CheckSumNotMatch:
return "Error: Check Sum Not Match";
break;
case FileIndexOut:
return "Error: File Index Out of Bound";
break;
case FileMismatch:
return "Error: Cannot Find File";
break;
case Advertise:
return "Error: In Advertise";
break;
default:
break;
}
break;
default:
break;
}
}
-
門鈴
+關注
關注
1文章
118瀏覽量
35785 -
WIFI
+關注
關注
81文章
5296瀏覽量
203577 -
ESP32
+關注
關注
18文章
971瀏覽量
17202
發布評論請先 登錄
相關推薦
評論