I got a humidity and temp sensor connected to an esp8266 and now I want to send the data to the cloud. What I can't get to work is a POST call. The POST request below, in the Auth() function, returns an error message that tells me that the body hasn't been sent over as it should.
I got the api to work fine in Postman, so I'm sure the credentials are ok etc.
Does anyone have an example on how to do a POST request and sending this body properly? I've rearched the documentation, and I can only find a GET example...
Thanks in advanced!
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include "credentials.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#define DHTPIN 2
#define DHTTYPE DHT21
const char* host = "login.salesforce.com";
const int httpsPort = 443;
// SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "0B 33 19 AC 6D 9E C1 5F 08 AB 93 17 2A FE F9 E0 90 69 C7 9A"; //login.salesforce.com
DHT_Unified dht(DHTPIN, DHTTYPE);
int delayMS = 3000;
String requestBody;
void setup() {
Serial.begin(115200); // Initialize device.
dht.begin(); // Initialize sensors
Serial.println();
Serial.printf("Connecting to %s", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // Connect to Wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
auth();
}
void loop() {
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println("°C");
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
// Delay between measurements.
delay(delayMS);
}
void auth() {
WiFiClientSecure client;
requestBody = "grant_type=password&client_id=" + consumerKey + "&client_secret=" + consumerSecret + "&username=" + userUsername + "&password=" + userPassword;
Serial.printf("connecting to %s using fingerprint %s\n", host, fingerprint);
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
} else {
Serial.println("Connected");
}
String url = "/services/oauth2/token";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
requestBody + "\r\n\r\n");
Serial.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
requestBody + "\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readString();
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("Authentication successfull!");
} else {
Serial.println("Authentication failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
I'd really appreciate any help!