ESP32(MicroPython) 网页控制五自由度机械臂
本程序通过网页控制五自由度机械臂,驱动方案改用PCA9685。
代码如下
#导入Pin模块
from machine import Pin
import time
from machine import SoftI2C
from servo import Servos
import network
import socket
#定义LED控制对象
led1=Pin(15,Pin.OUT,Pin.PULL_DOWN)
i2c=SoftI2C(sda=Pin(26),scl=Pin(27),freq=10000)
servos=Servos(i2c,address=0x40)
#连接的WIFI账号和密码
ssid = " "
password = " "
#舵机默认角度
servos.position(0,90)
servos.position(1,90)
servos.position(2,90)
servos.position(3,90)
servos.position(4,90)
#WIFI连接
def wifi_connect():
wlan=network.WLAN(network.STA_IF) #STA模式
wlan.active(True) #激活
if not wlan.isconnected():
print("conneting to network...")
wlan.connect(ssid,password) #输入 WIFI 账号密码
while not wlan.isconnected():
led1.value(1)
time.sleep_ms(300)
led1.value(0)
time.sleep_ms(300)
led1.value(0)
return False
else:
led1.value(0)
print("network information:", wlan.ifconfig())
return True
a0=90
a1=90
a2=90
a3=90
a4=90
#网页数据
def web_page():
global a0
global a1
global a2
global a3
global a4
html = """<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.button{display: inline-block; background-color: #971080; border: none;
border-radius: 4px; color: white; padding: 8px 15px; text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}
a:link {text-decoration:none;}a:visited {text-decoration:none;}a:hover {text-decoration:none;}
a:active {text-decoration:none;}
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 1.5rem; }
p { font-size: 1.5rem; }
.units { font-size: 1rem; }
.dht-labels{
font-size: 1rem;
vertical-align:middle;
padding-bottom: 7px;
}
</style>
</head>
<body>
<h2>ESP32 Servo Control</h2>
<p><a href="/?d0">Servo0: <strong>""" + str(a0) + """</strong></a></p>
<p><a href="/?b00"><button class="button">0</button></a>
<a href="/?b01"><button class="button">10</button></a>
<a href="/?b02"><button class="button">20</button></a>
<a href="/?b03"><button class="button">30</button></a>
<a href="/?b04"><button class="button">40</button></a>
<a href="/?b05"><button class="button">50</button></a>
<a href="/?b06"><button class="button">60</button></a>
<a href="/?b07"><button class="button">70</button></a>
<a href="/?b08"><button class="button">80</button></a>
<a href="/?b09"><button class="button">90</button></a>
<a href="/?b0a"><button class="button">100</button></a>
<a href="/?b0b"><button class="button">110</button></a>
<a href="/?b0c"><button class="button">120</button></a>
<a href="/?b0d"><button class="button">130</button></a>
<a href="/?b0e"><button class="button">140</button></a>
<a href="/?b0f"><button class="button">150</button></a>
<a href="/?b0g"><button class="button">160</button></a>
<a href="/?b0h"><button class="button">170</button></a>
<a href="/?b0i"><button class="button">180</button></a></p>
<p><a href="/?d1">Servo1: <strong>""" + str(a1) + """</strong></a></p>
<p><a href="/?b10"><button class="button">0</button></a>
<a href="/?b11"><button class="button">10</button></a>
<a href="/?b12"><button class="button