Hi. I'm reading DHT11 data and trying to send it to my cloud server using nodeMCU ESP32. The problem is I can not run "http.begin()" line. It always returns false. So far, I've tried adding fingerprint,
const uint8_t fingerprint[20] = "aa bb cc dd";
http.begin(fingerprint, host);
adding WiFiClient,
WiFiClient clientt;
http.begin(clientt, host);
and adding WiFiClientSecure;
WiFiClientSecure client;
client.setInsecure();
client.connect(host, 443);
http.begin(client, host);
but it won't work. Since http.begin() return false, my post request returns -1. Here is my full code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>
#include <WiFiClientSecureBearSSL.h>
#define DHTTYPE DHT11
/* Set these to your desired credentials. */
const char *ssid = "ssid"; //ENTER YOUR WIFI SETTINGS
const char *password = "password";
const char *userId = "123";
//Web/Server address to read/write from
const char *host = 'http://api.iot-ms.xyz/api/devices/data/'+userId; //website or IP address of server
//DHT pin
const int DHTPin = D4;
DHT dht(DHTPin, DHTTYPE);
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];
//=======================================================================
// Power on setup
//=======================================================================
void setup() {
delay(1000);
Serial.begin(115200);
delay(10);
dht.begin();
delay(10);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //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() {
HTTPClient http; //Declare object of class HTTPClient
String ADCDataTemp, ADCDataHum, station, postData;
float temperature = dht.readTemperature(); //Read Analog value of LDR
float humidity = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("DHT sensoru okunamadi.");
strcpy(celsiusTemp,"HATA!");
strcpy(fahrenheitTemp, "HATA!");
strcpy(humidityTemp, "HATA!");
}
//TESTING JSON CREATION
String json = "{\"mac_address\": \""+WiFi.macAddress()+"\",\"temperature\": \""+temperature+"\",\"humidity\": \""+humidity+ "\"}";
Serial.println(json);
Serial.println("MAC Address: "+WiFi.macAddress());
ADCDataTemp = String(temperature);
ADCDataHum = String(humidity); //String to interger conversion
//Post Data
//const uint8_t fingerprint[20] = {};
WiFiClient clientt;
bool isBegin = http.begin(clientt, host);
if(isBegin){
http.addHeader("Content-Type", "application/json"); //Specify content-type header
http.addHeader("Accept","application/json");
int httpCode = http.POST(json); //Send the request
String payload = http.getString(); //Get the response payload
Serial.print("HTTP Code: "); //Print HTTP return code
Serial.println(httpCode);
Serial.print("Request response payload: "); //Print request response payload
Serial.println(payload);
}else{
Serial.println("HTTP begin failed!");
}
http.end(); //Close connection
delay(5000); //Post Data at every 5 seconds
}
And this is my output:
01:35:43.596 -> .........
01:35:48.310 -> Connected to WiFi
01:35:48.667 -> Temperature: 25.50
01:35:48.667 -> Humidity: 51.00
01:35:48.667 -> HTTP begin failed!
01:35:53.357 -> Temperature: 25.60
01:35:53.357 -> Humidity: 51.00
01:35:53.402 -> HTTP begin failed!
01:35:58.410 -> Temperature: 25.60
01:35:58.410 -> Humidity: 51.00
01:35:58.410 -> HTTP begin failed!
01:36:03.454 -> Temperature: 25.70
01:36:03.454 -> Humidity: 51.00
01:36:03.454 -> HTTP begin failed!
I've checked my server url and I validated my json object. I have tried both http and https in my server url. Can you please help me? Thanks in advance...