HTTPS POST with 2 variables
Posted: Wed Mar 08, 2017 4:58 pm
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);
}