我是 Arduino 编程的新手,在让我的 arduino yù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>
最佳答案
IPAddress server(127,0,0,1);
这是您服务器的本地主机地址。将其更改为您的服务器的 IPv4 地址。
关于php - 无法将 Arduino Yùn 连接到本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22561461/