简体   繁体   English

Arduino以太网屏蔽未连接到WebServer

[英]Arduino Ethernet Shield Not Connecting to WebServer

I have a problem making my Arduino Ethernet shield to communicate with the server, the result on the serial monitor is always: 我在使用Arduino以太网屏蔽与服务器通信时遇到问题,串行监视器上的结果总是如下:

my arduino code is 我的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;                                        
    }
}

the server is an Apache server running on a pc, the server ip address in the code is the pc ip address. 服务器是在PC上运行的Apache服务器,代码中的服务器ip地址是pc ip地址。 For testing purposes I work at my homes network, there's no proxy or firewall, and I also turned of the antivirus and firewall on my pc. 出于测试目的,我在我的家庭网络工作,没有代理或防火墙,我也打开了我的电脑上的防病毒和防火墙。

the result in the serial monitor is always: 串行监视器的结果总是:

Not connected
Cannot connect to Server

Any thoughts?? 有什么想法吗??

它工作,问题是在Ethernet.begin(mac, ip , gateway , subnet) ,我删除了这一行,并使用DHCP, Ethernet.begin(mac)配置以太网盾

Have you verified the MAC address is correct? 您验证了MAC地址是否正确?

If it still doesn't work, try using 如果仍然无效,请尝试使用

Client client(server, 80);

in stead of 代替

EthernetClient client

And change 并改变

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

to

if (client.connect()) {  

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM