0

I need to send data from esp8266 to 000webhostapp. 000webhostapp using https all the connection with server works fine but data sending isn't working.

I tried with several codes. with get request or post request but none of them worked fine. i have changed http versions of esp or http but that also doesn't worked.

here is the code

#include "DHTesp.h"
#include "ESP8266WiFi.h"
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

DHTesp dht;
const char* ssid = "*****";
const char* password =  "****";
const char server[] = "biracial-conspiracy.000webhostapp.com";  
WiFiClient client;
void setup()
{
  Serial.begin(9600);
  Serial.println();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { /* Wait for the Wi-Fi to 
    connect*/
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }

  Serial.println("wl connected");
  Serial.println("");
  Serial.println("Credentials accepted! Connected to wifi\n ");
  Serial.println("");



  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex 
  (C)\t(F)");
  dht.setup(2, DHTesp::DHT22); // Connect DHT sensor to GPIO 17
 }

 void loop()
 {

   if (client.connect(server, 80)) {
     Serial.println("connected to server");
     int humidity = dht.getHumidity();
     int temperature = dht.getTemperature();
     Serial.print("Humidity : ");
     Serial.print(humidity);
     Serial.print("\t");
     Serial.print(" Temperature in Celcius : ");
     Serial.print(temperature);
     Serial.print("\t");
     Serial.print(" Moisture: ");
     Serial.println(analogRead(A0));
     HTTPClient http;
     int moisture = analogRead(A0);

     String data = "temp=" + String(temperature) + "&moisture=" + 
    String(moisture) + "&humidity=" + String(humidity);
    client.println("POST firebase.php HTTP/1.1");
    client.println("Host: biracial-conspiracy.000webhostapp.com\n");
    client.println("User-Agent: ESP8266/1.0");
    client.println("connection: close");
    client.status();
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print(data.length());
    client.print("\n\n");
    client.print(data);
    delay(1000);
    Serial.println("\n");
    Serial.println("Data sending is : ");
    Serial.println(data);
    Serial.println("The byte is : ");
    Serial.println(data.length());
    client.stop();
   }
   else
   {
    Serial.println("Server Error.");
   }


 }
1
  • Have you tried sending the request using something like postMan? Commented Jun 7, 2019 at 12:29

1 Answer 1

1

You cannot use WiFiClient to connect to HTTPS. You need to use a client library with TLS such as WiFiClientSecure to connect to an HTTPS server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.