Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By gbafamily1
#63480
Code: Select all/**
 * BasicHTTPSClientPost.ino
 *
 * See http://posttestserver.com/ for the HTTP server details.
 */

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

    USE_SERIAL.begin(115200);
   // USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFiMulti.addAP("SSID", "password");
}

void loop() {
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("[HTTPS] begin...\n");
        // Use posttestserver.com
        http.begin("https://posttestserver.com/post.php", "3D:F0:DB:04:C2:11:B4:3E:8F:E4:CA:EB:25:5A:5D:D0:96:B0:39:8C");
        http.addHeader("Content-Type", "application/x-www-form-urlencoded", false, true);
       
        USE_SERIAL.print("[HTTPS] POST...\n");
        // start connection and send HTTP header
        int httpCode = http.POST("arg1=value1&arg2=value2");

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTPS] POST... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTPS] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(10000);
}
Last edited by gbafamily1 on Tue May 30, 2017 5:08 pm, edited 1 time in total.
User avatar
By rudy
#63486 I tried this and get the following.


[HTTPS] begin...
[HTTPS] POST...
[HTTPS] POST... failed, error: connection refused


Other than ssis and password, is there anything else that needs changing?
User avatar
By gbafamily1
#63505 Yes, only the SSID and password must be changed.

To double check I copied and pasted the code from this thread into the Arduino IDE then put in my SSID and password. The sketch connected and posted 2 variables.

From a web browser does connecting to https://posttestserver.com work? If so, check the server cert SHA1 fingerprint. It must match the fingerprint shown below.

Code: Select allhttp.begin("https://posttestserver.com/post.php", "2E:FA:E4:F8:6D:8A:DE:4C:CE:07:9C:B8:22:08:AB:8F:B9:F8:8D:6D");
User avatar
By Ruandv
#66479 I am also trying to run this code.
I made 100% sure that my SSID and Password is correct. I successfully connect to the network but it keep telling me unable to Connect when it gets to the post part of the data.
this is what i get in the Serial window:
[HTTPS] begin...
[HTTPS] POST...
[HTTPS] POST... failed, error: connection refused

I also took the data and posted it through fiddler(Composer) as per below :
and it successfully returned a 200 What could be the problem?

POST https://posttestserver.com/post.php HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: posttestserver.com
Content-Length: 59

2E:FA:E4:F8:6D:8A:DE:4C:CE:07:9C:B8:22:08:AB:8F:B9:F8:8D:6D