Arduino + 以太网扩展板制作 Web 服务器 – Arduino教程

释放双眼,带上耳机,听听看~!

这个项目是关于使用带有以太网扩展板的Arduino。我将控制一个 LED 和一个伺服器,但您可以应用此方法来控制您想要的任何电子设备。(如直流电机、蜂鸣器、继电器、步进电机等)

Arduino + 以太网扩展板制作 Web 服务器 - Arduino教程

 介绍

上传并连接到互联网时提供的代码会在您的局域网中创建一个网络服务器,您只需使用该 IP 即可通过浏览器访问该网络服务器。之后,它会显示一个类似于下面的网页。当您按下“打开 LED”按钮时,您的 url 将更改为:“http://192.168.1.178/?button1on”,arduino 将读取该信息并打开 LED。

默认情况下,IP 为“192.168.1.178”。这也可以在提供的arduino代码中找到。

Arduino + 以太网扩展板制作 Web 服务器 - Arduino教程

 所需零件

Arduino + 以太网扩展板制作 Web 服务器 - Arduino教程

  • Arduino UNO
  •  1x 以太网扩展板
  • 1x 220 欧姆电阻
  •  1 个 LED
  • 1x 微型伺服电机
  •  1x 面包板
  •  杜邦线

 连接图

上传以下代码

/*
 https://www.qutaojiao.com 更多Arduino项目

带以太网屏的Arduino
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
int led = 4;
Servo microservo; 
int pos = 0; 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == 'n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='https://www.qutaojiao.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href="/?button1on"">Turn On LED</a>");
           client.println("<a href="/?button1off"">Turn Off LED</a><br />");   
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<a href="/?button2on"">Rotate Left</a>");
           client.println("<a href="/?button2off"">Rotate Right</a><br />"); 
           client.println("<p>Created by . Visit https://www.qutaojiao.com for more projects!</p>");  
           client.println("<br />"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button2on") >0){
                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
           if (readString.indexOf("?button2off") >0){
                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees 
                {                                
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

 

注意:如果你尝试这个项目。您只能从家中访问该 IP 地址。这意味着您必须连接到以太网扩展板所连接的同一路由器。下面的图片是我用iPad访问我的网络服务器。

Arduino + 以太网扩展板制作 Web 服务器 - Arduino教程

给TA打赏
共{{data.count}}人
人已打赏
ArduinoArduino-进阶动态

诺基亚5110 LCD - Arduino使用教程

2023-12-9 0:01:15

ArduinoArduino-进阶动态

带中继的Arduino以太网Web服务器

2023-12-9 11:40:49

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'); })();