Announcement

Collapse
No announcement yet.

PHP POST method with ESP8266

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP POST method with ESP8266

    Hi! I'm trying to send a POST request but I can't add new data to the file.

    First, I connect to WIFI (I assume it's OK since it doesn't send error).
    Then I create a TCP (also sends OK) and I try to send the request as I show in the program below.
    Can you help me to find out why it doesn't work, please?
    Thanks a lot!


    String data;
    String server = "www.server.esy.es/";
    String uri = "/callback.php";

    void httppost(void)
    {
    Display.print("Creating TCP..");
    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
    Display.print("create tcp ok\r\n");
    } else {
    Display.print("create tcp err\r\n");}
    delay(1000);

    data = "temperature=temp&humidity=hum";
    String postRequest =
    "POST " + uri + " HTTP/1.1\r\n" +
    "Host: " + server + "\r\n" +
    "Accept: *" + "/" + "*\r\n" +
    "Content-Length: " + data.length() + "\r\n" +
    "Content-Type: application/x-www-form-urlencoded\r\n" +
    "\r\n" + data;
    char *hello = "";
    Request.toCharArray(Request, Request.length()+1);
    wifi.send((const uint8_t*)hello, strlen(hello));

    if (wifi.releaseTCP()) {
    Display.print("release tcp ok\r\n");
    } else {
    Display.print("release tcp err\r\n");}
    delay(5000);

    }

  • #2
    Hi! I'm still trying since I haven't been able to send data. Now I've found andexample that uses the <WiFi.h> library:

    Serial.begin(115200);
    while(!Serial){}
    WiFiClient client;
    const char* host="http://jsonplaceholder.typicode.com/";
    String PostData = "title=foo&body=bar&userId=1";

    if (client.connect(host, 80)) {

    client.println("POST /posts HTTP/1.1");
    client.println("Host: jsonplaceholder.typicode.com");
    client.println("Cache-Control: no-cache");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);

    My doubt is: how can I include this library in my project?? #include "WiFi.h" doesn't work with Workshp and I can't compile it with Arduino IDE. Any ideas, please??

    In case it may help, I already use this one.

    // Uncomment next 2 lines to use ESP8266 with ESP8266 library from https://github.com/itead/ITEADLIB_Arduino_WeeESP8266 #include "ESP8266.h" ESP8266 wifi(SerialS,19200);

    Thanks for helping!!!

    Comment


    • #3
      Hello,

      You won't be able to use the WiFi.h library due to the talking to the ESP8266 via AT type commands. It is actually a hard task to achieve but I found this link that might help. In this expample he is using POST for sensor values.

      http://www.instructables.com/id/Ardu...ta-to-Website/

      He uses AT commands from an arduino which is the same setup as the 4Duino.

      I hope this helps

      Best regards

      Paul

      Comment


      • #4
        Hello Paul,
        it helped indeed. After modifying the code, I finally managed to POST data without problems.
        Thanks for everything!

        Comment

        Working...
        X