-->
Page 1 of 1

Arduino NodeMCU POST not working

PostPosted: Sun Apr 30, 2017 8:37 pm
by DotCodes
I have a `NodeMCU 1.0` with `ESP-12E` on it and using Arduino IDE for writing code on it.

I want to send a `POST` message to a remote `php` page on my webserer containing values from my module, but firstly I need to send something...

I tried many different examples from different sources and code snippets, but none seem to work. The method it's being sent
The `GET` method it's working, I can retrieve data from php pages, but I cannot send to them.

My code:
Code: Select all    #include <ESP8266WiFi.h>
    #include <ESP8266HTTPClient.h>
    #include <WiFiClient.h>

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

    String server = "www.example.net"; // http://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);
    }

My php:
Code: Select all    $test = $_POST['title'];
    echo $test;

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

I want to mention that the web server can accept POST messages and also that I have a reseller type of account, so I don't have full control of the server.

Any advice on this matter, it's worth it. I've been beating my head with it, for 2 days.
Thanks!