http://www.universalcard.byethost7.com是我的服務器。我在哪裏保存index.php文件。代碼如下:ESP8266發送GET請求到遠程服務器
<?php
if(isset($_GET['username']) && isset($_GET['pin']) && isset($_GET['cost'])) {
$username = $_GET['username'];
$pin = $_GET['pin'];
$cost = $_GET['cost'];
$filecontent = "Username is: ".$username." and PIN is: ".$pin." and cost is: ".$cost."\n";
$filestatus = file_put_contents('uc.txt',$filecontent,FILE_APPEND);
if($filestatus != false)
{
echo "Data written to file..";
}else{
echo "Ohh sorry..";
}
} else {
echo "Something went wrong..";
}
?>
我想用Arduino IDE發送ESP8266的GET請求。 在這個GET請求中,我用一些值(數據類型是String)發送了3個變量'username','pin'和'cost'。這些值附加到文件「uc.txt」。因此,當我使用瀏覽器發送請求時,值將附加到文本文件。
但是,當我嘗試使用ESP8266送它不附加
Arduino的代碼如下
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "rainbow";
const char* password = "12345678";
const char* host = "universalcard.byethost7.com";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
//const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/index.php?username=2bv14is114&pin=5555&cost=1111";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
void loop() {
}
,串行監視器的輸出低於
你的代碼說'byethehost7.com',但你的評論說'byethehost5.com'你發送你的請求到錯誤的網站? – leetibbett
是的,一個有問題的小錯誤應該是universalcard.byethost7.com。我現在將編輯問題。 –