我正在尝试将一些数据发送到我的NodeMCU中受保护(HTTPS)的网页,但是网页上没有显示任何数据。
我通过url和cURL检查了php端,两者都起作用。下面是我的PHP代码,保存在服务器端。
<?php
if($_SERVER['HTTPS']!="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}
if( $_REQUEST["d1"] || $_REQUEST["d2"])
{
echo " Data1: ". $_REQUEST['d1']. "<br />";
echo " Data2: ". $_REQUEST['d2']. "<br />";
}
$var1 = $_REQUEST['d1'];
$var2 = $_REQUEST['d2'];
$WriteMyRequest=
"<p> Data1 : " . $var1 . " </p>".
"<p> Data2 : " . $var2 . " </p><br/>";
file_put_contents('dataDisplayer.html', $WriteMyRequest, FILE_APPEND);
?>
下面也给出了我的Arduino代码,其中我存储了两个随机整数,并试图发送到php页面,该页面将数据存储在html页面中,我们可以通过该html页面看到输出
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char server[] = "shounaks.cf";
WiFiClient client;
int i;
String postData;
const char* MY_SSID = "Redmi";
const char* MY_PWD = "hello1234t";
void setup()
{
i=0;
Serial.begin(115200);
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()
{
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 443)) {
Serial.println("connected to server");
WiFi.printDiag(Serial);
if(i==20){
i=0;
}
postData = "d1=" + String(i) + "&d2=" + String(analogRead(A0)) ;
i++;
//change URL below if using your Sub-Domain
client.println("POST /test2.php HTTP/1.1");
//change URL below if using your Domain
client.print("Host: shounaks.cf");
client.print("\n");
client.println("User-Agent: NodeMCU/1.0");
client.print("\n");
client.print("Content-Length: ");
client.print(postData.length());
client.print("\n");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("\n\n");
client.print(postData);
client.stop();
Serial.println("\n");
Serial.println("My data string im POSTing looks like this: ");
Serial.println(postData);
Serial.println("And it is this many bytes: ");
Serial.println(postData.length());
delay(2000);
}
}