【问题标题】:Arduino Ethernet Shield Not Connecting to WebServerArduino Ethernet Shield 未连接到 WebServer
【发布时间】:2012-10-29 13:51:58
【问题描述】:

我的 Arduino Ethernet shield 无法与服务器通信, 串行监视器上的结果总是:

我的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

有什么想法吗??

【问题讨论】:

  • 服务器是否正在运行? telnet 192.168.0.53 80 返回什么?您是否通过代理或防火墙进行连接(似乎并非如此,但必须询问)?也许尝试更简单的Ethernet.begin( mac , ip ) 来排除网关问题。
  • 服务器是一台运行在pc上的Apache服务器,服务器ip地址就是pc的ip地址。 telnet 192.168.0.53 80 工作成功。出于测试目的,我在我的家庭网络中工作,没有代理或防火墙,我还关闭了我电脑上的防病毒和防火墙。

标签: connection webserver client arduino ethernet


【解决方案1】:

成功了,问题出在Ethernet.begin(mac, ip , gateway , subnet),我删除了这一行并使用DHCP配置了Ethernet Shield,Ethernet.begin(mac)

【讨论】:

  • 我遇到了同样的问题,但 DHCP 并没有解决我的问题。我确实获得了分配的 IP,但是在连接时我得到了 Not connected Cannot/connect to Server 错误还有其他建议吗?
【解决方案2】:

您确认 MAC 地址正确吗?

如果还是不行,试试

Client client(server, 80);

代替

EthernetClient client

然后改变

if (client.connect(server, 80)) {  

if (client.connect()) {  

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多