1

I am using Twilio for sending SMS in my project. I am measuring temperatures and if the temperature is too high, an SMS is sent. In my code I also have a read and a push to Firebase Realtime Database. In this case (where there is only one read and one push) everything works fine and the message is sent. However, after adding another set to Firebase the SMS is not sent. The error I get is  "Last SSL error was: Unable to allocate memory for SSL structures and buffers". I also added the command, which prints the free heap just before the message is sent and it prints that I still have 24000 free heap. In other cases, where I did not set values to Firebase, my free heap was also 24000 and the message was sent properly. Do you have any suggestions how to solve this problem?

Here's my code:

#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include "OneWire.h"
#include "DallasTemperature.h"
#include "twilio.hpp"
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

#define FIREBASE_HOST "***" 
#define FIREBASE_AUTH "***"
const char* account_sid = "***";
const char* auth_token = "***";
FirebaseData firebaseData1;
FirebaseData firebaseData2;
FirebaseJson tms;
FirebaseJsonArray arr;
String path = "/Test/Stream";
int sms = 0;
String c;
int b;

#define WIFI_SSID "***"
#define WIFI_PASSWORD "***"

const char fingerprint[] = "BC B0 1A 32 80 5D E6 E4 A2 29 66 2B 08 C8 E0 4C 45 29 3F D0";
String to_number    = "***";
String from_number = "***";
String message_body    = "Hello from Twilio and the ESP8266!";
String master_number    = "***";
String media_url = "";
Twilio *twilio;
ESP8266WebServer twilio_server(8000);
const char* host = "api.twilio.com";
const int   httpsPort = 443;
WiFiClientSecure client;

#if USE_SOFTWARE_SERIAL == 1
#include <SoftwareSerial.h>
extern SoftwareSerial swSer(14, 4, false, 256);
#else
#define swSer Serial
#endif

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp1 = 0;

unsigned long sendDataPrevMillis = 0;

void printResult(FirebaseData &data);
void printResult(StreamData &data);
void streamCallback(StreamData data)
{
  Serial.println("Stream Data1 available...");
  Serial.println("STREAM PATH: " + data.streamPath());
  Serial.println("EVENT PATH: " + data.dataPath());
  Serial.println("DATA TYPE: " + data.dataType());
  Serial.println("EVENT TYPE: " + data.eventType());
  Serial.print("VALUE: ");
  printResult(data);
  Serial.println();
}
void streamTimeoutCallback(bool timeout)
{
  if (timeout)
  {
    Serial.println();
    Serial.println("Stream timeout, resume streaming...");
    Serial.println();
  }
}
void setup()
{
  Serial.begin(115200);
  WiFiManager wm;
  bool res;
  res = wm.autoConnect("AutoConnectAP", "password"); 
  if (!res) {
    Serial.println("Failed to connect");
  }
  else {
    Serial.println("connected");
  }
  twilio = new Twilio(account_sid, auth_token, fingerprint);
  twilio_server.begin();
  sensors.begin();
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  
  firebaseData1.setBSSLBufferSize(1024, 1024);
  firebaseData1.setResponseSize(1024);
  firebaseData2.setBSSLBufferSize(1024, 1024);
  firebaseData2.setResponseSize(1024);
  if (!Firebase.beginStream(firebaseData1, path))
  {
    Serial.println("------------------------------------");
    Serial.println("Can't begin stream connection...");
    Serial.println("REASON: " + firebaseData1.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }
  Firebase.setStreamCallback(firebaseData1, streamCallback, streamTimeoutCallback);
}

void loop()
{
  if (millis() - sendDataPrevMillis > 5000)
  {
    sendDataPrevMillis = millis();
    
    sensors.requestTemperatures();
    temp1 = sensors.getTempCByIndex(0);
    Serial.print("");
    
    tms.set(".sv", "timestamp");
    Serial.print("FIRST HEAP");
    Serial.println(ESP.getFreeHeap());
    firebaseData2.pauseFirebase(false);
    
    if (Firebase.getString(firebaseData2, "002/alarm")) {
      if (firebaseData2.dataType() == "string") {
        Serial.println(firebaseData2.stringData());
      }
    } else {
      Serial.println(firebaseData2.errorReason());
    }

    int myInt = firebaseData2.stringData().toInt();
    firebaseData2.clear();
    
    Serial.println("HEAP BEFORE SET");
    Serial.println(ESP.getFreeHeap());
    if (temp1 < myInt) {
      if (Firebase.setInt(firebaseData2, "/serial/sms", sms))
      {
        printResult(firebaseData2);
      }
      else {
        Serial.print(firebaseData2.errorReason());
      }
    }
    firebaseData2.clear();

        
    if (temp1 > myInt) {
      firebaseData2.clear();
      Serial.print("HEAP BEFORE SENDING MESSAGE");
      Serial.println(ESP.getFreeHeap());
      String response;
      bool success = twilio->send_message(
                       to_number,
                       from_number,
                       message_body,
                       response,
                       media_url
                     );
     if (!client.connect(host, httpsPort)) {
    Serial.println("- Connection failed.");
    char buf[200];
                int err = client.getLastSSLError(buf, 199);

                buf[199] = '\0';

                Serial.println("Last SSL error was:");

                Serial.println(buf);

                Serial.print("ERRCODE: "); 
                Serial.println(err);
     }
    }
    else {
      firebaseData2.clear();
    }
    Serial.print("HEAP BEFORE PUSH");
    Serial.println(ESP.getFreeHeap());
    delay(0);
    arr.clear();
    arr.set("/[0]", temp1);
    arr.set("/[1]", tms);
    firebaseData2.pauseFirebase(false);
    if (Firebase.push(firebaseData2, path + "/Int", arr))
    {
      //      Serial.println("PASSED");
      //      Serial.println("PATH: " + firebaseData2.dataPath());
      //      Serial.println("TYPE: " + firebaseData2.dataType());
      //      Serial.print("VALUE: ");
      //      printResult(firebaseData2);
      //      Serial.println("------------------------------------");
      //      Serial.println();
    }
    else
    {
      //      Serial.println("FAILED");
      //      Serial.println("REASON: " + firebaseData2.errorReason());
      //      Serial.println("------------------------------------");
      //      Serial.println();
    }
    firebaseData2.pauseFirebase(true);
    firebaseData1.pauseFirebase(true);
    firebaseData1.clear();
    firebaseData2.clear();
    Serial.print("LAST HEAP");
    Serial.println(ESP.getFreeHeap());
  }
}

//Helper function for printing the response/received data
void printResult(FirebaseData &data)
{
  if (data.dataType() == "int")
    Serial.println(data.intData());
  else if (data.dataType() == "float")
    Serial.println(data.floatData(), 5);
  else if (data.dataType() == "double")
    printf("%.9lf\n", data.doubleData());
  else if (data.dataType() == "boolean")
    Serial.println(data.boolData() == 1 ? "true" : "false");
  else if (data.dataType() == "string")
    Serial.println(data.stringData());
  else if (data.dataType() == "json")
  {
    Serial.println();
    FirebaseJson &json = data.jsonObject();
    //Print all object data
    Serial.println("Pretty printed JSON data:");
    String jsonStr;
    json.toString(jsonStr, true);
    Serial.println(jsonStr);
    Serial.println();
    Serial.println("Iterate JSON data:");
    Serial.println();
    size_t len = json.iteratorBegin();
    String key, value = "";
    int type = 0;
    for (size_t i = 0; i < len; i++)
    {
      json.iteratorGet(i, type, key, value);
      Serial.print(i);
      Serial.print(", ");
      Serial.print("Type: ");
      Serial.print(type == FirebaseJson::JSON_OBJECT ? "object" : "array");
      if (type == FirebaseJson::JSON_OBJECT)
      {
        Serial.print(", Key: ");
        Serial.print(key);
      }
      Serial.print(", Value: ");
      Serial.println(value);
    }
    json.iteratorEnd();
  }
  else if (data.dataType() == "array")
  {
    Serial.println();
    //get array data from FirebaseData using FirebaseJsonArray object
    FirebaseJsonArray &arr = data.jsonArray();
    //Print all array values
    Serial.println("Pretty printed Array:");
    String arrStr;
    arr.toString(arrStr, true);
    Serial.println(arrStr);
    Serial.println();
    Serial.println("Iterate array values:");
    Serial.println();
    for (size_t i = 0; i < arr.size(); i++)
    {
      Serial.print(i);
      Serial.print(", Value: ");
      FirebaseJsonData &jsonData = data.jsonData();
      //Get the result data from FirebaseJsonArray object
      arr.get(jsonData, i);
      if (jsonData.typeNum == FirebaseJson::JSON_BOOL)
        Serial.println(jsonData.boolValue ? "true" : "false");
      else if (jsonData.typeNum == FirebaseJson::JSON_INT)
        Serial.println(jsonData.intValue);
      else if (jsonData.typeNum == FirebaseJson::JSON_FLOAT)
        Serial.println(jsonData.floatValue);
      else if (jsonData.typeNum == FirebaseJson::JSON_DOUBLE)
        printf("%.9lf\n", jsonData.doubleValue);
      else if (jsonData.typeNum == FirebaseJson::JSON_STRING ||
               jsonData.typeNum == FirebaseJson::JSON_NULL ||
               jsonData.typeNum == FirebaseJson::JSON_OBJECT ||
               jsonData.typeNum == FirebaseJson::JSON_ARRAY)
        Serial.println(jsonData.stringValue);
    }
  }
  else
  {
    Serial.println(data.payload());
  }
}
//Helper function for printing the response/received stream data
void printResult(StreamData &data)
{
  if (data.dataType() == "int")
    Serial.println(data.intData());
  else if (data.dataType() == "float")
    Serial.println(data.floatData(), 5);
  else if (data.dataType() == "double")
    printf("%.9lf\n", data.doubleData());
  else if (data.dataType() == "boolean")
    Serial.println(data.boolData() == 1 ? "true" : "false");
  else if (data.dataType() == "string" || data.dataType() == "null")
    Serial.println(data.stringData());
  else if (data.dataType() == "json")
  {
    Serial.println();
    FirebaseJson *json = data.jsonObjectPtr();
    //Print all object data
    Serial.println("Pretty printed JSON data:");
    String jsonStr;
    json->toString(jsonStr, true);
    Serial.println(jsonStr);
    Serial.println();
    Serial.println("Iterate JSON data:");
    Serial.println();
    size_t len = json->iteratorBegin();
    String key, value = "";
    int type = 0;
    for (size_t i = 0; i < len; i++)
    {
      json->iteratorGet(i, type, key, value);
      Serial.print(i);
      Serial.print(", ");
      Serial.print("Type: ");
      Serial.print(type == FirebaseJson::JSON_OBJECT ? "object" : "array");
      if (type == FirebaseJson::JSON_OBJECT)
      {
        Serial.print(", Key: ");
        Serial.print(key);
      }
      Serial.print(", Value: ");
      Serial.println(value);
    }
    json->iteratorEnd();
  }
  else if (data.dataType() == "array")
  {
    Serial.println();
    //get array data from FirebaseData using FirebaseJsonArray object
    FirebaseJsonArray *arr = data.jsonArrayPtr();
    //Print all array values
    Serial.println("Pretty printed Array:");
    String arrStr;
    arr->toString(arrStr, true);
    Serial.println(arrStr);
    Serial.println();
    Serial.println("Iterate array values:");
    Serial.println();
    for (size_t i = 0; i < arr->size(); i++)
    {
      Serial.print(i);
      Serial.print(", Value: ");
      FirebaseJsonData *jsonData = data.jsonDataPtr();
      //Get the result data from FirebaseJsonArray object
      arr->get(*jsonData, i);
      if (jsonData->typeNum == FirebaseJson::JSON_BOOL)
        Serial.println(jsonData->boolValue ? "true" : "false");
      else if (jsonData->typeNum == FirebaseJson::JSON_INT)
        Serial.println(jsonData->intValue);
      else if (jsonData->typeNum == FirebaseJson::JSON_FLOAT)
        Serial.println(jsonData->floatValue);
      else if (jsonData->typeNum == FirebaseJson::JSON_DOUBLE)
        printf("%.9lf\n", jsonData->doubleValue);
      else if (jsonData->typeNum == FirebaseJson::JSON_STRING ||
               jsonData->typeNum == FirebaseJson::JSON_NULL ||
               jsonData->typeNum == FirebaseJson::JSON_OBJECT ||
               jsonData->typeNum == FirebaseJson::JSON_ARRAY)
        Serial.println(jsonData->stringValue);
    }
  }
}

Thanks for your help in advance!

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.