无法将ArduinoYùn连接到本地服务器

时间:2014-03-21 14:23:40

标签: php mysql arduino

我是Arduino编程的新手,并且在让我的arduinoyùn连接到我的本地服务器时遇到了很多问题。我的目标是能够将arduino的温度数据发送到phpmyadmin,但到目前为止我甚至都无法连接。这是我尝试使用以下代码测试连接的代码:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,240,126);
IPAddress gateway(192, 168, 240, 1);
IPAddress subnet(255, 255, 255, 0);

IPAddress server(127,0,0,1); // Change to your server ip

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

void setup() {
  Serial.begin(9600);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready");
}

char pageAdd[32];
void loop()
{
  if(loopCount < 30)
  {
    delay(1000);
  }
  else
  {
    loopCount = 0;
    sprintf(pageAdd,"/arduino.php?temp1=%d",totalCount);
    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  } 
  else
  {
    Serial.println("failed");
    return 0;
  }

  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      connectLoop = 0;
    }

    delay(10);
    connectLoop++;
        if(connectLoop > 1000)
        {
          Serial.println();
          Serial.println("Timeout");
          client.stop();
        }

      }

      Serial.println();

      Serial.println("disconnecting.");
      client.stop();

      return 1;
    }

这是.php文件:

<?
    $temp1 = $HTTP_GET_VARS['temp1'];
?>
<html>
<body>
test server page<br>
<? echo('temp1 = ' . $temp1 . ' '); ?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

  

IPAddress服务器(127,0,0,1);

这是您服务器的本地主机地址。将其更改为您服务器的IPv4地址。