通过Wifi将数据从Arduino发送到PHP Web服务器

时间:2017-12-08 11:00:14

标签: php arduino

我正在尝试将数据从Arduino发送到PHP服务器。我使用的是临时主机:harshpalan14.000webhostapp.com(服务器1)

从那里我迁移到artcylens.com(服务器2)

我已将相同的PHP文件上传到服务器。但我只收到服务器1的响应。

我尝试直接从网络浏览器输入网址,在这种情况下,两台服务器都在响应。

Arduino代码:

#include "ESP8266WiFi.h"
#include "DHT.h"
#define DHTPIN 2    // what digital pin we're connected to  pin2 to D4 on esp board

//#define DHTTYPE DHT11  // DHT 11
//#define DHTTYPE DHT21  // DHT 21
#define DHTTYPE DHT22  // DHT 22

DHT dht(DHTPIN,DHTTYPE);

const char server[] = "artcylens.com"; 

const char* MY_SSID = "moto";
const char* MY_PWD =  "12345678";

WiFiClient client;


void setup()
{
  Serial.begin(115200);
  dht.begin();
  Serial.print("Connecting to "+*MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  Serial.println("going into wl connect");

  while (WiFi.status() != WL_CONNECTED) //not connected,  ...waiting to connect
    {
      delay(1000);
      Serial.print(".");
    }
  Serial.println("wl connected");
  Serial.println("");
  Serial.println("Credentials accepted! Connected to wifi\n ");
  Serial.println("");
}

void loop() {

   // Wait a few seconds between measurements.
  delay(2000);


 int Humidity = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int Temperature_Cel = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int Temperature_Fehr = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(Humidity) || isnan(Temperature_Cel) || isnan(Temperature_Fehr))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  int HeatIndex_Fehr = dht.computeHeatIndex(Temperature_Fehr, Humidity);
  // Compute heat index in Celsius (isFahreheit = false)
  int HeatIndex_Cel = dht.computeHeatIndex(Temperature_Cel, Humidity, false);

  Serial.print("Humidity: ");
  Serial.print(Humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(Temperature_Cel);
  Serial.print(" *C ");
  Serial.print(Temperature_Fehr);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(HeatIndex_Cel);
  Serial.print(" *C ");
  Serial.print(HeatIndex_Fehr);
  Serial.println(" *F\n");

    Serial.println("\nStarting connection to server..."); 
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    WiFi.printDiag(Serial);

    String data = "Humidity="
          +                        (String) Humidity
          +  "&Temperature_Cel="  +(String) Temperature_Cel
          +  "&Temperature_Fehr=" +(String) Temperature_Fehr;
     //change URL below if using your Sub-Domain
     client.println("POST dataPost.php HTTP/1.1"); 
     //change URL below if using your Domain
     client.print("Host: artcylens.com\n");                 
     client.println("User-Agent: ESP8266/1.0");
     client.println("Connection: close"); 
     client.println("Content-Type: application/x-www-form-urlencoded");
     client.print("Content-Length: ");
     client.print(data.length());
     client.print("\n\n");
     client.print(data);
     client.stop(); 

     Serial.println("\n");
     Serial.println("My data string im POSTing looks like this: ");
     Serial.println(data);
     Serial.println("And it is this many bytes: ");
     Serial.println(data.length());       
     delay(15000);
    } 

}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

PHP代码:

<?php

date_default_timezone_set("Asia/Kolkata");
$TimeStamp = "The current time is " . date("h:i:sa");
file_put_contents('dataDisplay.html', $TimeStamp, FILE_APPEND);


$var1 = $_REQUEST['Humidity'];
$var2 = $_REQUEST['Temperature_Cel'];
$var3 = $_REQUEST['Temperature_Fehr'];


if( $_REQUEST["Humidity"] ||  $_REQUEST["Temperature_Cel"] ||
                      $_REQUEST["Temperature_Fehr"]) 
   {
   echo " The Humidity is: ". $_REQUEST['Humidity']. "%<br />";
   echo " The Temperature is: ". $_REQUEST['Temperature_Cel']. " Celcius<br />";
   echo " The Temperature is: ". $_REQUEST['Temperature_Fehr']. " Fehr<br />";
   }

$WriteMyRequest=
"<p> The Humidity is : "       . $var1 . "% </p>".
"<p>And the Temperature is : " . $var2 . " Celcius </p>".
"<p>And the Temperature is : " . $var3 . " Fehreinheit</p><br/>";


file_put_contents('dataDisplay.html', $WriteMyRequest, FILE_APPEND);


?>

我发送数据的网址:

服务器1:harshpalan14.000webhostapp.com/dataPost.php

服务器2:artcylens.com/dataPost.php

我的问题:不知何故,我的服务器2无法接收数据。该怎么办 ?

0 个答案:

没有答案