搜索
bottom↓
回复: 1

【零知ESP32教程】进阶3:创建Web服务器控制伺服电机

[复制链接]

出0入0汤圆

发表于 2019-10-30 11:18:43 | 显示全部楼层 |阅读模式
介绍:利用ESP32的WIFI模块,创建一个简单的网络服务器来控制伺服电机。

可以使用任何具有浏览器功能的设备访问要构建的网络服务器:本地网络上的智能手机,平板电脑,笔记本电脑等。

硬件:
1、零知ESP32
2、伺服电机

接线:


软件:
零知开发工具

代码:


注意:
1、代码导入之后需要根据自己的本地网络在下面的两行代码中填写无线网名称和密码!

  1. //找到下面两行填写WiFi名称和密码
  2. const char* ssid     = "";
  3. const char* password = "";
复制代码
2、需要导入伺服电机的库文件,见附件。

  1. #include <WiFi.h>
  2. #include "Servo.h"

  3. Servo myservo;  // create servo object to control a servo
  4. // twelve servo objects can be created on most boards

  5. // GPIO the servo is attached to
  6. static const int servoPin = 13;

  7. // Replace with your network credentials
  8. const char* ssid     = "";
  9. const char* password = "";

  10. // Set web server port number to 80
  11. WiFiServer server(80);

  12. // Variable to store the HTTP request
  13. String header;

  14. // Decode HTTP GET value
  15. String valueString = String(5);
  16. int pos1 = 0;
  17. int pos2 = 0;

  18. void setup() {
  19.     Serial.begin(9600);
  20.     delay(1000);
  21.     myservo.attach(servoPin);  // attaches the servo on the servoPin to the servo object

  22.     // Connect to Wi-Fi network with SSID and password
  23.     Serial.print("Connecting to ");
  24.     Serial.println(ssid);
  25.     WiFi.begin(ssid, password);
  26.     while (WiFi.status() != WL_CONNECTED) {
  27.         delay(500);
  28.         Serial.print(".");
  29.     }
  30.     // Print local IP address and start web server
  31.     Serial.println("");
  32.     Serial.println("WiFi connected.");
  33.     Serial.println("IP address: ");
  34.     Serial.println(WiFi.localIP());
  35.     server.begin();
  36. }

  37. void loop() {
  38.     WiFiClient client = server.available();   // Listen for incoming clients

  39.     if (client) {                             // If a new client connects,
  40.         Serial.println("New Client.");          // print a message out in the serial port
  41.         String currentLine = "";                // make a String to hold incoming data from the client
  42.         while (client.connected()) {            // loop while the client's connected
  43.             if (client.available()) {             // if there's bytes to read from the client,
  44.                 char c = client.read();             // read a byte, then
  45.                 Serial.write(c);                    // print it out the serial monitor
  46.                 header += c;
  47.                 if (c == '\n') {                    // if the byte is a newline character
  48.                     // if the current line is blank, you got two newline characters in a row.
  49.                     // that's the end of the client HTTP request, so send a response:
  50.                     if (currentLine.length() == 0) {
  51.                         // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  52.                         // and a content-type so the client knows what's coming, then a blank line:
  53.                         client.println("HTTP/1.1 200 OK");
  54.                         client.println("Content-type:text/html");
  55.                         client.println("Connection: close");
  56.                         client.println();

  57.                         // Display the HTML web page
  58.                         client.println("<!DOCTYPE html><html>");
  59.                         client.println("<head><meta name="viewport" content="width=device-width, initial-scale=1">");
  60.                         client.println("<link rel="icon" href="data:,">");
  61.                         // CSS to style the on/off buttons
  62.                         // Feel free to change the background-color and font-size attributes to fit your preferences
  63.                         client.println("<style>body { text-align: center; font-family: "Trebuchet MS", Arial; margin-left:auto; margin-right:auto;}");
  64.                         client.println(".slider { width: 300px; }</style>");
  65.                         client.println("<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>");

  66.                         // Web Page
  67.                         client.println("</head><body><h1>ESP32 with Servo</h1>");
  68.                         client.println("<p>Position: <span id="servoPos"></span></p>");
  69.                         client.println("<input type="range" min="0" max="180" class="slider" id="servoSlider" onchange="servo(this.value)" value="" + valueString + ""/>");

  70.                         client.println("<script>var slider = document.getElementById("servoSlider");");
  71.                         client.println("var servoP = document.getElementById("servoPos"); servoP.innerHTML = slider.value;");
  72.                         client.println("slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }");
  73.                         client.println("$.ajaxSetup({timeout:1000}); function servo(pos) { ");
  74.                         client.println("$.get("/?value=" + pos + "&"); {Connection: close};}</script>");

  75.                         client.println("</body></html>");

  76.                         //GET /?value=180& HTTP/1.1
  77.                         if (header.indexOf("?value=") >= 0) {
  78.                             pos1 = header.indexOf('=');
  79.                             pos2 = header.indexOf('&');
  80.                             valueString = header.substring(pos1 + 1, pos2);

  81.                             //Rotate the servo
  82.                             myservo.write(valueString.toInt());
  83.                             Serial.println(valueString);
  84.                         }
  85.                         // The HTTP response ends with another blank line
  86.                         client.println();
  87.                         // Break out of the while loop
  88.                         break;
  89.                     } else { // if you got a newline, then clear currentLine
  90.                         currentLine = "";
  91.                     }
  92.                 } else if (c != '\r') {  // if you got anything else but a carriage return character,
  93.                     currentLine += c;      // add it to the end of the currentLine
  94.                 }
  95.             }
  96.         }
  97.         // Clear the header variable
  98.         header = "";
  99.         // Close the connection
  100.         client.stop();
  101.         Serial.println("Client disconnected.");
  102.         Serial.println("");
  103.     }
  104. }
复制代码


导入程序代码,在右侧选择ESP32开发板,然后验证程序,如图:



然后上传程序到开发板,打开串口调试窗口,可以看到WiFi已连接,且服务器创建成功,输出本地服务器地址:



使用手机或者电脑的浏览器连接服务器,就能够控制使用滑块控制伺服电机的转动角度了,如图所示:





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2019-10-31 09:20:09 | 显示全部楼层
不怎么懂web,学习下。
顺便请教下
一、
       1 .本帖的https://code.jquery.com/jquery-3.1.1.min.js
       2. web控彩灯帖子中:https://stackpath.bootstrapcdn.c ... s/bootstrap.min.css
                                 以及https://cdnjs.cloudflare.com/aja ... .0.4/jscolor.min.js
       这些CSS,javascript代码是自己写的还是或怎么来的,做什么用的?
二、另外看css这个名字包含bootstrap,是否是说使用了bootstrap前端框架?
三、请教零知的ESP32(或者我们电工常用的STM32)是否也是可以使用互联网行业流行的前端三大框架Angular Vue React?如何使用?如果可以使用,我相信ESP32的价值将会大增
四、后端框架是否也可以使用某些?

回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 06:31

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表