A simple post to PHP script
Posted: Tue Nov 22, 2016 3:42 am
Hi all
I just want to send a simple data to PHP script from my NODEMCU V1 "ESP12"
Test:
http://192.168.1.44/arduino/test.php?name=miguel&age=21
Result:
Hi miguel. You're old enough to have a drink, but do so responsibly.
It work fine from web-browser¡¡¡¡¡
If someone know a simple way to send a post please share whit me or tell me what are doing bad.
I made this sketch:
#include <ESP8266WiFi.h>
#include <ArduinoHttpClient.h>
const char* ssid = "****************";
const char* password = "******************";
char serverAddress[] = "192.168.1.44"; // server address
int port = 80;
const char* host = "http://192.168.1.44";
WiFiClient wifi;
HttpClient client = HttpClient(wifi, host, port);
int status = WL_IDLE_STATUS;
String response;
int statusCode = 0;
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);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Signal Strenght : " + String(WiFi.RSSI()) + " dBm");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
Serial.println("making POST request");
String contentType = "arduino/test.php";
String postData = "name=Alice&age=12";
client.post("/", contentType, postData);
// read the status code and body of the response
statusCode = client.responseStatusCode();
response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
The printout Result is:
making POST request
Status code: -2
Response:
Wait five seconds
PHP script:
I just want to send a simple data to PHP script from my NODEMCU V1 "ESP12"
Test:
http://192.168.1.44/arduino/test.php?name=miguel&age=21
Result:
Hi miguel. You're old enough to have a drink, but do so responsibly.
It work fine from web-browser¡¡¡¡¡
If someone know a simple way to send a post please share whit me or tell me what are doing bad.
I made this sketch:
#include <ESP8266WiFi.h>
#include <ArduinoHttpClient.h>
const char* ssid = "****************";
const char* password = "******************";
char serverAddress[] = "192.168.1.44"; // server address
int port = 80;
const char* host = "http://192.168.1.44";
WiFiClient wifi;
HttpClient client = HttpClient(wifi, host, port);
int status = WL_IDLE_STATUS;
String response;
int statusCode = 0;
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);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Signal Strenght : " + String(WiFi.RSSI()) + " dBm");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
Serial.println("making POST request");
String contentType = "arduino/test.php";
String postData = "name=Alice&age=12";
client.post("/", contentType, postData);
// read the status code and body of the response
statusCode = client.responseStatusCode();
response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
The printout Result is:
making POST request
Status code: -2
Response:
Wait five seconds
PHP script:
Code: Select all
<?php
foreach ($_REQUEST as $key => $value)
{
if ($key == "name") {
$name = $value;
}
if ($key == "age") {
$age = $value;
}
}
if ($age < 21) {
echo "<p> $name, You're not old enough to drink.</p>\n";
} else {
echo "<p> Hi $name. You're old enough to have a drink, ";
echo "but do so responsibly.</p>\n";
}
?>