Arduino 以太网扩展板未连接到 Web 服务器

2024-01-05

我在让 Arduino 以太网扩展板与服务器通信时遇到问题, 串行监视器上的结果始终是:

我的arduino代码是

#include <Ethernet.h>           //library for ethernet functions
#include <SPI.h>
#include <Dns.h>
#include <Client.h>             //library for client functions
#include <DallasTemperature.h>  //library for temperature sensors

// Ethernet settings
byte mac[] = {0x09,0xA2,0xDA,0x00,0x01,0x26};  //Replace with your Ethernet shield MAC
byte ip[] = { 192,168,0,54};  //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(192,168,0,53);                  // IP-adress of server arduino sends data to

EthernetClient client;

bool connected = false;                                

void setup(void) {                                     

    Serial.begin(9600);                                
    Serial.println("Initializing Ethernet.");
    delay(1000);
    Ethernet.begin(mac, ip , gateway , subnet);    

}

void loop(void) {                                      

    if(!connected)   {                                 
      Serial.println("Not connected");
      if (client.connect(server, 80)) {                
          connected = true;
          int temp =analogRead(A1);                    
          Serial.print("Temp is ");                              
          Serial.println(temp);
          Serial.println();
          Serial.println("Sending to Server: ");                 
          client.print("GET /formSubmit.php?t0=");            
          Serial.print("GET /formSubmit.php?t0=");            
          client.print(temp);
          Serial.print(temp);
          client.println(" HTTP/1.1");                  
          Serial.println(" HTTP/1.1");                  
          client.println("Host: http://localhost/PhpProject1/");    
          Serial.println("Host: http://localhost/PhpProject1/");    
          client.println("User-Agent: Arduino");        
          Serial.println("User-Agent: Arduino");        
          client.println("Accept: text/html");          
          Serial.println("Accept: text/html");          
          //client.println("Connection: close");        
          //Serial.println("Connection: close");        
          client.println();                             
          Serial.println();
          delay(10000);                                            
      }
      else{
        Serial.println("Cannot connect to Server");               
      }
    }  
    else {
      delay(1000);                                              
      while (client.connected() && client.available()) {        
        char c = client.read();                                 
        Serial.print(c);                                        
      }                                                         
      Serial.println();                                         
      client.stop();                                            
      connected = false;                                        
    }
}

服务器是运行在PC上的Apache服务器,代码中的服务器IP地址是PC的IP地址。出于测试目的,我在家庭网络上工作,没有代理或防火墙,而且我还关闭了电脑上的防病毒软件和防火墙。

串行监视器中的结果始终是:

Not connected
Cannot connect to Server

有什么想法吗??


成功了,问题出在Ethernet.begin(mac, ip , gateway , subnet),我删除了这条线并使用 DHCP 配置以太网屏蔽,Ethernet.begin(mac)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Arduino 以太网扩展板未连接到 Web 服务器 的相关文章

随机推荐