ESP8266 Arduino 代码:控制两个舵机和红外 LED
ESP8266 Arduino 代码:控制两个舵机和红外 LED
本代码示例使用 Arduino IDE 为 ESP8266 编写代码,实现控制两个舵机和一个红外 LED 的功能。代码包含完整的硬件连接和软件配置,并附带前端控制面板,可通过网页便捷地控制硬件动作。
硬件连接
- ESP8266 开发板
- 两个舵机
- 红外发射器
- 电阻
- 跳线
代码
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
const char* ssid = 'your_SSID'; // 你的WiFi名称
const char* password = 'your_PASSWORD'; // 你的WiFi密码
IRsend irsend(4); // 红外发射器连接到GPIO4
Servo servo1; // 第一个舵机
Servo servo2; // 第二个舵机
void setup() {
Serial.begin(9600);
servo1.attach(5); // 第一个舵机连接到GPIO5
servo2.attach(14); // 第二个舵机连接到GPIO14
pinMode(4, OUTPUT); // 将GPIO4设置为输出模式
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println('Connecting to WiFi...');
}
Serial.println('Connected to WiFi');
}
void loop() {
// 控制第一个舵机旋转
servo1.write(90);
delay(1000);
servo1.write(0);
delay(1000);
// 控制第二个舵机旋转
servo2.write(90);
delay(1000);
servo2.write(0);
delay(1000);
// 发射红外信号
irsend.sendNEC(0x20DF10EF, 32); // 发射NEC格式的红外信号,数据码为0x20DF10EF,共32位
delay(3000);
}
前端控制面板
<!DOCTYPE html>
<html>
<head>
<title>ESP8266 Control Panel</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
h1 {
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.card {
background-color: #f1f1f1;
border-radius: 5px;
padding: 20px;
margin: 20px;
text-align: center;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px;
cursor: pointer;
border-radius: 5px;
box-shadow: 0 2px 4px 0 rgba(0,0,0,0.2);
}
.button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>ESP8266 Control Panel</h1>
<div class="container">
<div class="card">
<h2>Control Servo 1</h2>
<button class="button" onclick="rotateServo1()">Rotate</button>
</div>
<div class="card">
<h2>Control Servo 2</h2>
<button class="button" onclick="rotateServo2()">Rotate</button>
</div>
<div class="card">
<h2>Control IR LED</h2>
<button class="button" onclick="sendIR()">Send</button>
</div>
</div>
<script>
function rotateServo1() {
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://your_esp8266_ip_address/servo1', true);
xhr.send();
}
function rotateServo2() {
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://your_esp8266_ip_address/servo2', true);
xhr.send();
}
function sendIR() {
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://your_esp8266_ip_address/ir', true);
xhr.send();
}
</script>
</body>
</html>
使用方法
- 将代码上传至 ESP8266 开发板。
- 连接 ESP8266 到 WiFi 网络。
- 找到 ESP8266 的 IP 地址。
- 将前端代码中的
your_esp8266_ip_address替换为你的 ESP8266 的 IP 地址。 - 在浏览器中打开前端控制面板网页。
- 点击按钮即可控制舵机和红外 LED。
注意事项
- 确保将代码中的
your_SSID和your_PASSWORD替换为你的 WiFi 网络名称和密码。 - 确保将代码中的 GPIO 引脚号替换为实际连接的引脚号。
- 确保将前端代码中的
your_esp8266_ip_address替换为你的 ESP8266 的 IP 地址。
扩展
- 可以根据需要修改代码,实现更复杂的功能,例如通过网页控制舵机的旋转角度和红外信号的数据码。
- 可以使用 WebSockets 替代 HTTP 请求,实现实时控制。
- 可以使用第三方库,例如
ThingSpeak或Blynk,将数据上传到云平台,实现远程控制。
总结
本代码示例展示了如何使用 Arduino IDE 为 ESP8266 编写代码,实现控制两个舵机和一个红外 LED 的功能。同时,还提供了前端控制面板,可通过网页便捷地控制硬件动作。通过本示例,可以学习 ESP8266 的基础编程和网络通信,以及如何将硬件与网页连接起来。
原文地址: https://www.cveoy.top/t/topic/mUMF 著作权归作者所有。请勿转载和采集!