1

I have a nodejs application deployed and have been trying to send data to the api using the following sketch on my nodemcu:


#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>


void setup() {

Serial.begin(115200);
WiFi.begin("mySSID", "myPassword");

while (WiFi.status() != WL_CONNECTED)

delay(500);
Serial.println("Waiting for connection");

}

void loop() {

if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status

  HTTPClient http;
  http.begin("https://www.livesessions.host/api/v1/readings" , "cc2707645d0222700161a174671cb8b0258eaa91‎");
  http.addHeader("Content-Type", "application/json");
  String postMessage = "{'deviceMAC':'ddd eee fff', 'humidity':'99', 'soilTemp':'88'}";
  int httpCode = http.POST(postMessage);
  Serial.print("http result:");
  Serial.println(httpCode);
  http.writeToStream(&Serial);
  String payload = http.getString();
  http.end();
  
  }else{

    Serial.print("Error in Wifi connection");

}

delay(30000); //Send a request every 30 seconds

}

Although I am able to make data entries using the api and postman, the below code returns -1 (which I think is connection refused), the website is an https site.

Please advise on what I am doing wrong.

Thanks.

3
  • supply a WiFiSSLClient object to the HttpClient – Juraj Mar 10 at 5:33
  • @Juraj...Thanks, but can you elaborate with a bit of example code? Would really appreciate it. – Amin Baig Mar 10 at 5:41
  • 1
    see the BasicHttpsClient example – Juraj Mar 10 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.