目录
1.ESP32简介
2.项目构思
3.项目所需材料
4.代码示例
5.实物运行
1.ESP32简介
ESP32是一个集成天线和射频巴伦、功率放大器、低噪声放大器、滤波器和电源管理模块。整个解决方案占用的印刷电路板面积最少。该板采用台积电40nm低功耗技术的2.4GHz双模Wi-Fi和蓝牙芯片,功率和射频性能最佳,安全可靠,可扩展到各种应用。
2.项目构思
使用esp32连接到本地局域网,通过控制高低电信号的输入和输出,实现对马达前进后退与停止的控制。
3.项目所需材料
ESP32开发板*1
130电机DC3V*2
L298N电机驱动板*1
导线若干
输出功率为5V3A的移动电源
4.代码示例
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
// 马达A
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;
// 马达B
int motor2Pin1 = 33;
int motor2Pin2 = 32;
int enable2Pin = 25;
// 替换成你的WiFi信息
const char* ssid = "名称";
const char* password = "密码";
// 开放80端口
AsyncWebServer server(80);
//web界面的格式和信息
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>ESP IOT DASHBOARD</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="icon" type="image/png" href="favicon.png">
<script src="https://kit.fontawesome.com/0294e3a09e.js" crossorigin="anonymous"></script>
<style>
html {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}
h1 {
font-size: 1.8rem;
color: white;
}
.topnav {
overflow: hidden;
background-color: #0A1128;
}
body {
margin: 0;
}
.content {
padding: 50px;
}
.card-grid {
max-width: 800px;
margin: 0 auto;
display: grid;
grid-gap: 2rem;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.card {
background-color: white;
box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5);
}
.card-title {
font-size: 1.2rem;
font-weight: bold;
color: #034078
}
.state {
font-size: 1.2rem;
color:#1282A2;
}
button {
border: none;
color: #FEFCFB;
padding: 15px 32px;
text-align: center;
font-size: 16px;
width: 150px;
border-radius: 4px;
transition-duration: 0.4s;
}
.button-on {
background-color:#034078;
}
.button-on:hover {
background-color: #1282A2;
}
.button-off {
background-color:#858585;
}
.button-off:hover {
background-color: #252524;
}
.button-stop {
background-color:#5e0f0f;
width: 100%;
}
.button-stop:hover {
background-color: #9b332c;
}
</style>
</head>
<body>
<div class="topnav">
<h1>CONTROL DC MOTORS</h1>
</div>
<div class="content">
<div class="card-grid">
<div class="card">
<p class="card-title"><i class="fa-solid fa-gear"></i> DC Motor A</p>
<p>
<a href="forwardA"><button class="button-on"><i class="fa-solid fa-arrow-up"></i> FORWARD</button></a>
<a href="backwardA"><button class="button-off"><i class="fa-solid fa-arrow-down"></i> BACKWARD</button></a>
</p>
<p>
<a href="stopA"><button class="button-stop"><i class="fa-solid fa-stop"></i> STOP</button></a>
</p>
</div>
<div class="card">
<p class="card-title"><i class="fa-solid fa-gear"></i> DC Motor B</p>
<p>
<a href="forwardB"><button class="button-on"><i class="fa-solid fa-arrow-up"></i> FORWARD</button></a>
<a href="backwardB"><button class="button-off"><i class="fa-solid fa-arrow-down"></i> BACKWARD</button></a>
</p>
<p>
<a href="stopB"><button class="button-stop"><i class="fa-solid fa-stop"></i> STOP</button></a>
</p>
</div>
</div>
</div>
</body>
</html>
)rawliteral";
//连接WiFi
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
//A马达前进
void moveForwardA(){
Serial.println("Motor A Moving Forward");
digitalWrite(enable1Pin, HIGH);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
}
//A马达后退
void moveBackwardA(){
Serial.println("Motor A Moving Backward");
digitalWrite(enable1Pin, HIGH);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
}
//A马达停止
void stopMotorA(){
Serial.println("Motor A Stopped");
digitalWrite(enable1Pin, LOW);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
}
//B马达前进
void moveForwardB(){
Serial.println("Motor B Moving Forward");
digitalWrite(enable2Pin, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
//B马达后退
void moveBackwardB(){
Serial.println("Motor B Moving Backward");
digitalWrite(enable2Pin, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
//B马达停止
void stopMotorB(){
Serial.println("Motor B Stopped");
digitalWrite(enable2Pin, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void setup() {
Serial.begin(115200);
// Set motor pins as outputs
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
initWiFi();
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", index_html);
});
server.on("/forwardA", HTTP_GET, [](AsyncWebServerRequest *request) {
moveForwardA();
request->send(200, "text/html", index_html);
});
server.on("/backwardA", HTTP_GET, [](AsyncWebServerRequest *request) {
moveBackwardA();
request->send(200, "text/html", index_html);
});
// Route to set GPIO state to LOW (inverted logic for ESP8266)
server.on("/stopA", HTTP_GET, [](AsyncWebServerRequest *request) {
stopMotorA();
request->send(200, "text/html", index_html);
});
server.on("/forwardB", HTTP_GET, [](AsyncWebServerRequest *request) {
moveForwardB();
request->send(200, "text/html", index_html);
});
server.on("/backwardB", HTTP_GET, [](AsyncWebServerRequest *request) {
moveBackwardB();
request->send(200, "text/html", index_html);
});
// Route to set GPIO state to LOW (inverted logic for ESP8266)
server.on("/stopB", HTTP_GET, [](AsyncWebServerRequest *request) {
stopMotorB();
request->send(200, "text/html", index_html);
});
server.begin();
}
void loop() {
}