Arduino ESP32 Web服务器控制舵机

Arduino ESP32 Web服务器控制舵机


利用ESP32的WIFI模块,创建一个简单的网络服务器来控制伺服电机。

实例代码


	#include <WiFi.h>
	#include "Servo.h"
	 
	Servo myservo;  // create servo object to control a servo
	// twelve servo objects can be created on most boards
	 
	// GPIO the servo is attached to
	static const int servoPin = 13;
	 
	// Replace with your network credentials
	const char* ssid     = "";
	const char* password = "";
	 
	// Set web server port number to 80
	WiFiServer server(80);
	 
	// Variable to store the HTTP request
	String header;
	 
	// Decode HTTP GET value
	String valueString = String(5);
	int pos1 = 0;
	int pos2 = 0;
	 
	void setup() {
	    Serial.begin(9600);
	    delay(1000);
	    myservo.attach(servoPin);  // attaches the servo on the servoPin to the servo object
	 
	    // 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();
	}
	 
	void loop() {
	    WiFiClient client = server.available();   // Listen for incoming clients
	 
	    if (client) {                             // If a new client connects,
	        Serial.println("New Client.");          // print a message out in the serial port
	        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();
	 
	                        // Display the HTML web page
	                        client.println("<!DOCTYPE html><html>");
	                        client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
	                        client.println("<link rel=\"icon\" href=\"data:,\">");
	                        // CSS to style the on/off buttons
	                        // Feel free to change the background-color and font-size attributes to fit your preferences
	                        client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial; margin-left:auto; margin-right:auto;}");
	                        client.println(".slider { width: 300px; }</style>");
	                        client.println("<script src=\"http://www.lingzhilab.com/home/js/jquery.min.js\"></script>");
	 
	                        // Web Page
	                        client.println("</head><body><h1>ESP32 with Servo</h1>");
	                        client.println("<p>Position: <span id=\"servoPos\"></span></p>");
	                        client.println("<input type=\"range\" min=\"0\" max=\"180\" class=\"slider\" id=\"servoSlider\" οnchange=\"servo(this.value)\" value=\"" + valueString + "\"/>");
	 
	                        client.println("<script>var slider = document.getElementById(\"servoSlider\");");
	                        client.println("var servoP = document.getElementById(\"servoPos\"); servoP.innerHTML = slider.value;");
	                        client.println("slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }");
	                        client.println("$.ajaxSetup({timeout:1000}); function servo(pos) { ");
	                        client.println("$.get(\"/?value=\" + pos + \"&\"); {Connection: close};}</script>");
	 
	                        client.println("</body></html>");
	 
	                        //GET /?value=180& HTTP/1.1
	                        if (header.indexOf("?value=") >= 0) {
	                            pos1 = header.indexOf('=');
	                            pos2 = header.indexOf('&');
	                            valueString = header.substring(pos1 + 1, pos2);
	 
	                            //Rotate the servo
	                            myservo.write(valueString.toInt());
	                            Serial.println(valueString);
	                        }
	                        // 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("");
	    }
	}
							
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ESP8266 WiFi模块可以用于控制舵机。该模块集成了业界领先的Tensilica L106超低功耗32位微型MCU和Wi-Fi功能,支持标准的IEEE802.11b/g/n协议和完整的TCP/IP协议栈。为了控制舵机,你需要使用特定的GPIO引脚来连接ESP8266模块和舵机。可以使用模块上的IO口或者扩展板上的IO口。然后,你可以使用ESP8266的GPIO库函数来控制所连接的引脚,以实现舵机的控制。舵机通常使用PWM信号进行控制,你可以使用ESP8266的定时器模块和PWM波模块来生成PWM信号,并将其输出到舵机的控制引脚上,以实现舵机的角度控制。详细的具体步骤可以参考ESP8266的相关文档和教程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [基于ESP8266远程舵机的控制与实现](https://blog.csdn.net/weixin_41114301/article/details/126441216)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [2023年电赛E题代码-stm32部分源码、jetson nano上的OpenCV源码及电路板PCB原理图设计](https://download.csdn.net/download/qq_32971095/88226738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值