Post example
/*
* HTTPS Secured Client POST Request
* Copyright (c) 2019, circuits4you.com
* All rights reserved.
* https://circuits4you.com
* Connects to WiFi HotSpot. */
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
/* Set these to your desired credentials. */
const char *ssid = "your_ssid"; //ENTER YOUR WIFI SETTINGS
const char *password = "your_pass";
//Link to read data from https://jsonplaceholder.typicode.com/comments?postId=7
//Web/Server address to read/write from
const char *host = "postman-echo.com";
const int httpsPort = 443; //HTTPS= 443 and HTTP = 80
//SHA1 finger print of certificate use web browser to view and copy
const char fingerprint[] PROGMEM = "A6 5A 41 2C 0E DC FF C3 16 E8 57 E9 F2 C3 11 D2 71 58 DF D9";
//=======================================================================
// Power on setup
//=======================================================================
void setup() {
delay(1000);
Serial.begin(115200);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //Only Station No AP, This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
//=======================================================================
// Main Program Loop
//=======================================================================
void loop() {
WiFiClientSecure httpsClient; //Declare object of class WiFiClient
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
httpsClient.setFingerprint(fingerprint);
httpsClient.setTimeout(15000); // 15 Seconds
delay(1000);
Serial.print("HTTPS Connecting");
int r=0; //retry counter
while((!httpsClient.connect(host, httpsPort)) && (r < 30)){
delay(100);
Serial.print(".");
r++;
}
if(r==30) {
Serial.println("Connection failed");
}
else {
Serial.println("Connected to web");
}
String getData, Link;
//POST Data
Link = "/post";
Serial.print("requesting URL: ");
Serial.println(host);
/*
POST /post HTTP/1.1
Host: postman-echo.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 13
say=Hi&to=Mom
*/
httpsClient.print(String("POST ") + Link + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded"+ "\r\n" +
"Content-Length: 13" + "\r\n\r\n" +
"say=Hi&to=Mom" + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (httpsClient.connected()) {
String line = httpsClient.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
String line;
while(httpsClient.available()){
line = httpsClient.readStringUntil('\n'); //Read Line by Line
Serial.println(line); //Print response
}
Serial.println("==========");
Serial.println("closing connection");
delay(2000); //POST Data at every 2 seconds
}
//=======================================================================
|
Upload the code and open serial monitor, in
client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n"+ "Content-Length: " + data.length() + "\r\n" + "Content-Type: application/json;charset=UTF-8\r\n\r\n"+ data +"\r\n");
String postRequest =
"POST " + uri + " HTTP/1.0\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;
#include <ESP8266HTTPClient.h> // head of program
const char *host = "http://URL to page";
// Below in the Loop
HTTPClient http; //Declare object of class HTTPClient
//Post Data
String postData = "data=" + data ;
http.begin(host); //Specify request destination
delay(1000); // See if this prevents the problm with connection refused and deep sleep
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
1
2
3
4
5
6
7
8
9
|
POST /esppost.php HTTP/1.0
Host: serverconnect.site88.net
Accept: */*
Content-Length: "name1=value1&name2=value2".Length
Content-Type: application/x-www-form-urlencoded
name1=value1&name2=value2
Code
1) Include the
ESP8266WiFi library and initialize variables for hardware and data collection. Edit the network information and write API key in your code.
2) In the
setup function, start the serial monitor, connect to the wireless network, and initialize the device pins that you use.
3) In the main loop, read the soil monitor and store it in the
data array. POST the data to ThingSpeak, and then put the device in low-power mode.
4) Use the
readSoil function to provide power to the sensor, and then read the voltage at the output using the ADC. Turn off the power after measurement.
5) Connect your device to the wireless network using the
connectWiFi function.
6) Build the data string to post to your channel. Connect to ThingSpeak, and use the Wi-Fi client to complete an HTTP POST.
7) Wait for and receive the response from the server using
getResponse . |
Comments