Php 将ESP8266/NodeMCU数据发送到HTTPS网页

Php 将ESP8266/NodeMCU数据发送到HTTPS网页,php,https,arduino,esp8266,nodemcu,Php,Https,Arduino,Esp8266,Nodemcu,我正试图将一些数据发送到我的网页,该网页由我的NodeMCU提供安全保护(HTTPS),但网页上没有显示任何数据。 我通过url和cURL检查了php,两者都有效。下面是我保存在服务器端的php代码 <?php if($_SERVER['HTTPS']!="on") { $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("Location:$redirect"); } if( $_RE

我正试图将一些数据发送到我的网页,该网页由我的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);
}
}
#包括
#包括
const char server[]=“shounaks.cf”;
无线客户端;
int i;
字符串postData;
const char*MY_SSID=“Redmi”;
const char*MY_PWD=“hello1234t”;
无效设置()
{
i=0;
序列号开始(115200);
Serial.print(“连接到”+*我的SSID);
WiFi.begin(我的SSID,我的PWD);
Serial.println(“进入wl连接”);
而(WiFi.status()!=WL_CONNECTED)//未连接,…正在等待连接
{
延迟(1000);
连续打印(“.”);
}
串行打印LN(“wl连接”);
Serial.println(“”);
Serial.println(“已接受凭据!已连接到wifi\n”);
Serial.println(“”);
}
void循环()
{
Serial.println(“\n正在启动与服务器的连接…”);
//如果您获得连接,请通过串行方式报告:
if(client.connect(server,443)){
Serial.println(“连接到服务器”);
WiFi.printDiag(串行);
如果(i==20){
i=0;
}
postData=“d1=”+String(i)+“&d2=”+String(模拟读取(A0));
i++;
//如果使用您的子域,请更改下面的URL
println(“POST/test2.php HTTP/1.1”);
//如果使用您的域,请更改下面的URL
client.print(“主机:shounaks.cf”);
client.print(“\n”);
client.println(“用户代理:NodeMCU/1.0”);
client.print(“\n”);
client.print(“内容长度:”);
client.print(postData.length());
client.print(“\n”);
client.println(“内容类型:application/x-www-form-urlencoded”);
client.print(“\n\n”);
客户端打印(postData);
client.stop();
Serial.println(“\n”);
println(“我的数据字符串im POST看起来像这样:”;
Serial.println(postData);
println(“就是这么多字节:”);
Serial.println(postData.length());
延迟(2000年);
}
}

检查有关如何发出HTTPS请求的ESP8266示例。没有任何问题示例无法发送或接收数据,这只是表明服务器已启动。您是否使用
ArduinoJson
库对其进行了测试。