mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
415 stars 118 forks source link

Coonection Refused #57

Closed Shahzainali closed 4 years ago

Shahzainali commented 4 years ago

Hi, here are logs, I am simple writing data to realtime, Streaming data works perfectly,, I faced this issue suddenly, can you please help.

Thanks

Connected with IP: 192.168.1.105

[V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 216348 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [V][ssl_client.cpp:195] start_ssl_client(): Performing the SSL/TLS handshake... [V][ssl_client.cpp:216] start_ssl_client(): Verifying peer X.509 certificate... [V][ssl_client.cpp:225] start_ssl_client(): Certificate verified. [V][ssl_client.cpp:240] start_ssl_client(): Free internal heap after TLS 175132 [V][ssl_client.cpp:279] send_ssl_data(): Writing HTTP request... [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 171060 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [V][ssl_client.cpp:195] start_ssl_client(): Performing the SSL/TLS handshake... [V][ssl_client.cpp:216] start_ssl_client(): Verifying peer X.509 certificate... [V][ssl_client.cpp:225] start_ssl_client(): Certificate verified. [V][ssl_client.cpp:240] start_ssl_client(): Free internal heap after TLS 131592 [V][ssl_client.cpp:279] send_ssl_data(): Writing HTTP request... [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 127516 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [V][ssl_client.cpp:195] start_ssl_client(): Performing the SSL/TLS handshake... [V][ssl_client.cpp:216] start_ssl_client(): Verifying peer X.509 certificate... [V][ssl_client.cpp:225] start_ssl_client(): Certificate verified. [V][ssl_client.cpp:240] start_ssl_client(): Free internal heap after TLS 88044 [V][ssl_client.cpp:279] send_ssl_data(): Writing HTTP request... [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 83912 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():190]: (-32512) SSL - Memory allocation failed [E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -32512 [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 85444 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():190]: (-32512) SSL - Memory allocation failed [E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -32512 [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 84940 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():190]: (-32512) SSL - Memory allocation failed [E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -32512 [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 83740 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():190]: (-32512) SSL - Memory allocation failed [E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -32512 [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 83428 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():190]: (-32512) SSL - Memory allocation failed [E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -32512 [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. [V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 83112 [V][ssl_client.cpp:58] start_ssl_client(): Starting socket [V][ssl_client.cpp:93] start_ssl_client(): Seeding the random number generator [V][ssl_client.cpp:102] start_ssl_client(): Setting up the SSL/TLS structure... [I][ssl_client.cpp:156] start_ssl_client(): WARNING: Use certificates for a more secure communication! [V][ssl_client.cpp:180] start_ssl_client(): Setting hostname for TLS session... [E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():190]: (-32512) SSL - Memory allocation failed [E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -32512 [V][ssl_client.cpp:248] stop_ssl_socket(): Cleaning SSL connection. FAILED REASON: connection refused

mobizt commented 4 years ago

[V][ssl_client.cpp:56] start_ssl_client(): Free internal heap before TLS 83740

Free heap is not enough for mbedTLS to work. It requires > 100k before start SSL handshake.

You may need to reduce ram usage of your code.

Shahzainali commented 4 years ago

Hi, I have tried but no success can you please help me here is my code..

#include <WiFi.h>
//#include "types.h"
#include <FirebaseESP32.h>
#include <HardwareSerial.h>

#include <TinyGPS++.h>"
TinyGPSPlus gps;

bool statusL1 = true;
bool statusL2 = true;
bool statusL3 = true;

bool faultL1 = false;
HardwareSerial SerialGPS(1);

#define LIGHT1 13 
#define L1INPUT 34

#define LIGHT2 12 
#define L2INPUT 35

#define LIGHT3 14 
#define L3INPUT 32

#define LDRINPUT 33
#define AMPEREINPUT 39

#define L1INPUTF 36

// setting PWM properties

const int ledChannel1 = 0;
const int ledChannel2 = 2;;
const int ledChannel3 = 3;
const int freq = 5000;
const int resolution = 10;

//Define FirebaseESP32 data object

FirebaseData firebaseData;
FirebaseData firebaseDataCallback1;
FirebaseData firebaseDataCallback2;
FirebaseData firebaseDataCallback3;

#define RXD2 16
#define TXD2 17

void setup()
{
  Serial.begin(115200);
  SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
  Serial.println("Serial Txd is on pin: " + String(TX));
  Serial.println("Serial Rxd is on pin: " + String(RX));
Serial.println(true);
  // We start by connecting to a WiFi network
  WiFi.begin("xxxx", "xxxx");
  Serial.print("Connecting to Wi - Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  Firebase.begin("xxxx, "xxxx");
  Firebase.reconnectWiFi(true);
  //Firebase.enableClassicRequest(firebaseData, true);

Firebase.setStreamCallback(firebaseDataCallback1, streamCallback1, streamTimeoutCallback);
Firebase.setStreamCallback(firebaseDataCallback2, streamCallback2, streamTimeoutCallback);
Firebase.setStreamCallback(firebaseDataCallback3, streamCallback3, streamTimeoutCallback);

if (!Firebase.beginStream(firebaseDataCallback1, "/lights/l1/led_power"))
{
  //Could not begin stream connection, then print out the error detail
  Serial.println(firebaseDataCallback1.errorReason());
}
if (!Firebase.beginStream(firebaseDataCallback2, "/lights/l2/led_power"))
{
  Serial.println(firebaseDataCallback2.errorReason());
}
if (!Firebase.beginStream(firebaseDataCallback3, "/lights/l3/led_power"))
{
  Serial.println(firebaseDataCallback3.errorReason());
}

  pushFirebase("/shahzain/isfault/", true);

//Light1 
 pinMode(LIGHT1,OUTPUT);
 pinMode(L1INPUT, INPUT);

 pinMode(LIGHT2,OUTPUT);
 pinMode(L2INPUT, INPUT);

 pinMode(LIGHT3,OUTPUT);
 pinMode(L3INPUT, INPUT);

 pinMode(LDRINPUT, INPUT);

   ledcSetup(ledChannel1, freq, resolution);
  ledcAttachPin(LIGHT1, ledChannel1);

   ledcSetup(ledChannel2, freq, resolution);
  ledcAttachPin(LIGHT2, ledChannel2);

   ledcSetup(ledChannel3, freq, resolution);
  ledcAttachPin(LIGHT3, ledChannel3);
//endLight1
  delay(500);
}

void streamTimeoutCallback(bool timeout)
{
  if(timeout){
    //Stream timeout occurred
    Serial.println("Stream timeout, resume streaming...");
  }  
}

void streamCallback1(StreamData data)
{
  Serial.println("Stream Data...");
  Serial.println(data.streamPath());
  Serial.println(data.dataPath());
  Serial.println(data.dataType());

  if(data.dataType() == "boolean")
    { 
    statusL1 = data.boolData();
    Serial.println(statusL1);
    }

}
void streamCallback2(StreamData data)
{
  Serial.println("Stream Data...");
  Serial.println(data.streamPath());
  Serial.println(data.dataPath());
  Serial.println(data.dataType());

  if(data.dataType() == "boolean")
    { 
    statusL2 = data.boolData();
    Serial.println(statusL1);
    }

}
void streamCallback3(StreamData data)
{
  Serial.println("Stream Data...");
  Serial.println(data.streamPath());
  Serial.println(data.dataPath());
  Serial.println(data.dataType());

  if(data.dataType() == "boolean")
    { 
    statusL3 = data.boolData();
    Serial.println(statusL1);
    }

}
//" / l1 / isfault"
void pushFirebase(String path, String value) {
  if (Firebase.setString(firebaseData, path, value))
  {
    Serial.println("PASSED");
    Serial.println("PATH: " + firebaseData.dataPath());
    Serial.println("TYPE: " + firebaseData.dataType());
    Serial.println("ETag: " + firebaseData.ETag());
    Serial.print("VALUE: ");
    // printResult(firebaseData);
    Serial.println("------------------------------------");
    Serial.println();
  }
  else
  {
    Serial.println("FAILED");
    Serial.println("REASON: " + firebaseData.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }
}

void pushFirebase(String path, bool value) {
  if (Firebase.setBool(firebaseData, path, value))
  {
    Serial.println("PASSED");
    Serial.println("PATH: " + firebaseData.dataPath());
    Serial.println("TYPE: " + firebaseData.dataType());
    Serial.println("ETag: " + firebaseData.ETag());
    Serial.print("VALUE: ");
    // printResult(firebaseData);
    Serial.println("------------------------------------");
    Serial.println();
  }
  else
  {
    Serial.println("FAILED");
    Serial.println("REASON: " + firebaseData.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }
}
double lat;
double lon;

int period = 60000;
unsigned long time_now = 0;

String double2string(double n, int ndec) {
  String r = "";

  int v = n;
  r += v;     // whole number part
  r += '.';   // decimal point
  int i;
  for (i=0;i<ndec;i++) {
    // iterate through each decimal digit for 0..ndec
    n -= v;
    n *= 10;
    v = n;
    r += v;
  }

  return r;
}
void loop()
{

  while (SerialGPS.available() > 0) {
    byte gpsData = SerialGPS.read();
    gps.encode(gpsData);

  }
  if (gps.location.isUpdated()){
    if(millis() >= time_now + period){
      time_now += period;
      Serial.println("Hello");

      Serial.print("D / M / A: ");
      Serial.print(gps.time.value());
      Serial.print("Latitude = ");
      Serial.print(gps.location.lat(), 6);
      lat = (gps.location.lat());
      lon = (gps.location.lng());
      String loc = (double2string(lat,6) + ", " + double2string(lon, 6));
      // Longitude in degrees (double)
      Serial.print(" Longitude = ");
      Serial.println(gps.location.lng(), 6);

      pushFirebase("/location",loc);

    }
  }

  Serial.print(".");

int ldrL1 = digitalRead(L1INPUT);
int f1 = analogRead(L1INPUTF);
Serial.print(f1);

  int LDRIN= digitalRead(LDRINPUT);
  if(LDRIN){
    if(!faultL1){
if(statusL1){

if ( ldrL1 == LOW )
{ 
  ledcWrite(ledChannel1,1024);
  delay(20);
}
else 
{
  ledcWrite(ledChannel1,32);

}
}
else{

  ledcWrite(ledChannel1,0);
  }
    }

  //light2

int ldrL2 = digitalRead(L2INPUT);

if(statusL2){

if ( ldrL2 == LOW )
{ 
  ledcWrite(ledChannel2,512);
}
else 
{
  ledcWrite(ledChannel2,32);

}
}
else{

  ledcWrite(ledChannel2,0);
  }

  //light3

int ldrL3 = digitalRead(L3INPUT);

if(statusL3){
//int L1F = analogRead(L1INPUTF);
//if(L1F==0){
//  pushFirebase("/lights/l1/isfault", true);
//}

if ( ldrL3 == LOW )
{ 
  ledcWrite(ledChannel3,512);
}
else 
{
  ledcWrite(ledChannel3,32);

}
}
else{

  ledcWrite(ledChannel3,0);
  }
  }else{
  ledcWrite(ledChannel1,0);
  ledcWrite(ledChannel2,0);
  ledcWrite(ledChannel3,0);
    }
  int amp =  analogRead(AMPEREINPUT);
  //Serial.print(amp);
  delay(200);

}
`
Shahzainali commented 4 years ago

This is method for saving data pushFirebase("/shahzain/isfault/", true);

previously it was working perfectly.

mobizt commented 4 years ago

You are using too many FirebaseData objects.

Each FirebaseData object has WiFi Client instance which it consumes large amount of ram due to memory used by SSL/TLS provider ( mbedTLS library).

mobizt commented 4 years ago

You can use only one FirebaseData object for stream parent node e.g. Firebase.beginStream(firebaseDataCallback1, "/lights")

You can check firebaseDataCallback1.dataPath() to determine which child node has changed.

Shahzainali commented 4 years ago

Thanks for quick response, Let me try

Shahzainali commented 4 years ago

Thanks :) problem is fixed.

nguyenthang621 commented 2 years ago

I need the source code, please