'POST request using a nodeMCU arduino board is not successfull
Other similar questions appear to be for older boards using slightly different code. I'm also very new to playing around with ESP8266 boards, so I might be missunderstanding tutorials and what the functions do.
I am trying to send data from and nodemcu board using a post request, my code is as follows:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//SSID and Password of your WiFi router
const char* ssid = "MYSSID"; // Enter SSID here
const char* password = "MYPASSWORD"; //Enter Password here
const char *URL = "MYDOMAINNAME.com/endpoint.php";
WiFiClient client;
HTTPClient httpClient;
const int trigPin = 12;
const int echoPin = 14;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
long duration;
float distanceCm;
float distanceInch;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
WiFi.begin(ssid, password);
Serial.println("Connecting to WIFI…");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_VELOCITY/2;
delay(1000);
// Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
String data = "data="+String(distanceCm);
httpClient.begin(client,URL);
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpClient.POST(data);
String content = httpClient.getString();
httpClient.end();
Serial.println(content);
delay(1000);
}
On the server side, I have a very simple php script that I'm using for debugging at the moment:
<?php
$field1=$_POST["data"];
$myfile = fopen($field1.".txt", "w") or die("Unable to open file!");
fclose($myfile);
echo $field1;
Simply I'm using this to see if the post request is a success, if it is then a txt file will be generated.
I've tried various combinations or the const char *URL, either starting my domain with https://, https://wwww, www, http://, http://www. All fail to give a result, the only one that gives something interesting or of note in the serial monitor is http://www. which returns a 301 error, all other combinations print nothing into the serial monitor.
I'm hosting my endpoint on a shared server (Namecheap) with no htaccess rules set up (just as is).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
