1
我正在一個項目中,我嘗試使用HTTP和POST方法從我的Arduino Mega2560發送數據到我在本地主機上託管的網站。我住在學生宿舍,因此我無法使用路由器。但是,我購買了一臺路由器並通過WAN將其連接到了牆上。此外,Arduino通過以太網屏蔽層連接到路由器的以太網插頭之一,並通過筆記本電腦連接到另一個以太網插頭。我試圖從網站上獲取數據,但似乎無法加載它們。從Arduino發送數據到網站
Arduino的代碼
#include <dht.h>
#include <SPI.h>
#include <Ethernet.h>
dht DHT1;
int temperatureSensorPin1 = 25;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
EthernetClient client;
EthernetServer server(80);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000;
String data;
long lastUpdate = 0;
void setup() {
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
data = "";
pinMode(temperatureSensorPin1, INPUT);
}
void loop() {
int temp1 = DHT1.read11(temperatureSensorPin1);
switch (temp1)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
}
String data="temp1="+String(DHT1.temperature, 0)+"&temp2="+String(DHT2.temperature, 0)+"&temp3="+String(DHT3.temperature, 0)+"&temp4="+String(DHT4.temperature, 0)+"&temp5="+String(DHT5.temperature, 0)
+"&hum1="+String(DHT1.humidity, 0)+"&hum2="+String(DHT2.humidity, 0)+"&hum3="+String(DHT3.humidity, 0)+"&hum4="+String(DHT4.humidity, 0)+"&hum5="+String(DHT5.humidity, 0)+"&gas="+gasValue;
Serial.println(Ethernet.localIP());
if(client.connect("192.168.0.1", 80)) {
client.println("POST /home/visualisation.php HTTP/1.1");
client.println("Host: 192.168.0.1");
//client.println("User-Agent: Arduino/1.0");
//client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
//Serial.println(data);
Serial.println("We have connection");
}
if(client.connected()) {
Serial.println("We have abc");
client.stop();
}
delay(4000);
}
PHP代碼
<?php
$temp1 = "";
if(isset($_POST['temp1'])) {
$temp1 = $_POST['temp1'];
}
echo $temp1;
?>
爲T他說我已經做出了適當的連接?我應該怎麼做才能工作?
P.S .:我想澄清一點,我沒有關於網絡的擴展知識。
你會得到什麼錯誤? –
我沒有收到任何錯誤。只是當我刷新頁面時,我看不到值的迴應。 在串行監控我得到: 192.168.0.101 我們有連接 我們有ABC – Vasilis
的'如果首先檢查輸出(client.connect( 「192.168.0.1」,80))' 請參閱:https://開頭www.arduino.cc/en/Reference/EthernetClient –