0
我有一個NodeMCU 1.0
與ESP-12E
上,並使用Arduino IDE編寫的代碼。Arduino NodeMCU開機不工作
我想發出一個POST
消息到遠程php
頁的從我的模塊包含值我webserer,但首先我需要送東西......
我試圖從不同的來源和代碼片斷許多不同的例子,但似乎沒有工作。它正在發送的方法 它的工作方式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;
編輯:我想提一提的是,Web服務器可以接受POST消息,並說我有帳戶的經銷商類型,所以我沒有完全控制服務器。
對這個問題的任何建議,這是值得的。我一直在用它敲打我的頭,爲期2天。
謝謝!
你有'$ val',這是很危險的SQL注入。 –
謝謝,我意識到這一點,只是想收到消息^^。 –