2017-05-01 4 views
0

私はNodeMCU 1.0ESP-12EとArduino IDEを使ってコードを書いています。私は、さまざまなソースとコードスニペットからの多くの異なる例を試してみましたArduino NodeMCU POSTが動作しません

私は私のモジュールから値を含む私のwebserer上のリモートphpページへPOSTメッセージを送信したいのですが、最初に私が何かを送信する必要があります...しかし、どれもうまくいかないようです。それが送信されているメソッド それは働いているGETメソッド、私はPHPページからデータを取得することができますが、私はそれらに送信することはできません。

マイコード:

#include <ESP8266WiFi.h> 
#include <ESP8266HTTPClient.h> 
#include <WiFiClient.h> 

const char* ssid  = "SomeWireless"; 
const char* password = "12345"; 

String server = "www.example.net"; // www.example.com 

void setup() { 
Serial.begin(115200); 
    delay(10); 
// We start by connecting to a WiFi network 
    Serial.println(); 
    Serial.println(); 
    Serial.print("Connecting to "); 
    Serial.println(ssid); 

    WiFi.mode(WIFI_STA); 
    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()); 

} 

void loop() { 
    jsonPOST(); 
    myPOST(); 
    myGET(); 
} 


void myPOST() { 
    HTTPClient http; 

    http.begin("http://example.net/asd/recv.php"); 
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    http.POST("title=foo"); 
    http.writeToStream(&Serial); 
    http.end(); 

    delay(1000); 
} 

void jsonPOST() { 
    WiFiClient client; 

    if (client.connect("example.net", 80)) { 
     Serial.println("Connected to server"); 
    // Make the HTTP request 
     int value = 2.5; // an arbitrary value for testing 
     String content = "{\"JSON_key\": " + String(value) + "}"; 
     client.println("POST /asd/recv.php HTTP/1.1"); 
     client.println("Host: example.net"); 
     client.println("Accept: */*"); 
     client.println("Content-Length: " + String(content.length())); 
     client.println("Content-Type: application/json"); 
     client.println(); 
     client.println(content); 
    } 
    delay(1000); 
} 

void myGET() { 
    if (WiFi.status() == WL_CONNECTED) { //Check  WiFi connection status 
    HTTPClient http; //Declare an object of class HTTPClient 
    http.begin("http://www.exmaple.net/asd/btnCheck.php"); //Specify request destination 
    int httpCode = http.GET(); //Send the request 

    if (httpCode > 0) { //Check the returning code 
     String payload = http.getString(); //Get the request response payload 
     Serial.println(payload);      //Print the response payload 
    } 
    http.end(); //Close connection 
    } 
    delay(1000); 
} 

マイPHP:

$test = $_POST['title']; 
echo $test; 

//JSON 
$json = file_get_contents('php://input'); 
$obj = json_decode($json); 
echo $obj; 

EDIT:は、私は、WebサーバーがPOSTメッセージを受け入れることができると私は、アカウントの再販業者の種類を持っているにもいることを言及したいと思います、私はサーバーを完全に制御することはできません。

この問題に関するアドバイスは、それだけの価値があります。私は2日間、頭を打っていました。
ありがとうございます!

答えて

0

ANSWER:

は私がリクエストを適切に行うために管理します。問題は投稿内容からでした。
ESP8266HTTPClient.hライブラリと私のために動作するコードは次のとおりです。

void jPOST() { 
    HTTPClient http; 

    http.begin("http://example.net/asd/urlen.php"); 
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    http.POST("{\"y\":6,\"x\":11}"); 
    http.writeToStream(&Serial); 
    http.end(); 

    delay(2000); 
} 

とPHPページ用:

$req1 = file_get_contents("php://input"); 
$req2 = json_decode($req1); 

$val= $req2 ->x; 
echo $val; 
//echo $req2 -> x; 

mysqli_query($conn,"INSERT INTO tbl_ard(field1) VALUES($val)"); 
+0

あなたが危険である '$のval'、中にSQLインジェクションを持っています。 –

+0

ありがとう、私はそれを知っていた、ちょうど^ ^メッセージを受け取りたいと思った。 –

関連する問題