Firebase Arduino Read Timeout

Hi, I'm trying to send the value read by an encoder to a Firebase database. However, after 100 sends its function firebaseData.errorReason() give a read timeout error. Does anyone know how to solve this?

//libraries
#include "Firebase_Arduino_WiFiNINA.h"    //DataBase
#include <SPI.h>                          //Encoder

//pins
#define BUZZER_PIN 2                      //Peripherals
#define AMT22_NOP 0x00                    //Encoder
#define AMT22_RESET 0x60                  //Encoder
#define AMT22_ZERO 0x70                   //Encoder
#define RES12 12                          //Encoder
#define RES14 14                          //Encoder
#define ENC_0 2                           //Encoder
#define SPI_MOSI MOSI                     //Encoder
#define SPI_MISO MISO                     //Encoder
#define SPI_SCLK SCK                      //Encoder

//wifi                                    //DataBase
//NetCasa
//#define WIFI_SSID "MXXXXX"
//#define WIFI_PASSWORD "CXXXXX6"
//NetTelemovel
#define WIFI_SSID "OXXXXXXs"
#define WIFI_PASSWORD "nXXXX"

//Firebase                                //DataBase
#define FIREBASE_HOST "XXXXXXX-default-rtdb.europe-west1.firebasedatabase.app"
#define FIREBASE_AUTH "tJpdUJr2cwGoI49WtLXX76E43gqlwUSyCiELfLHl"

//Define Firebase data object
FirebaseData firebaseData;
String ComplitPath = "/Exercise2/angle";  //Path da Base de Dados. 1-Simular a andar

//Sounds
int GoodSound[] = { 510, 510, 0 };         // Notas: C4, E4, G4, C5
int GoodSoundNoteDurations[] = { 10, 1 };  // Durações: 4 tempos cada
int SizeGood = sizeof(GoodSound) / sizeof(GoodSound[0]);
//-------------------------------------
int BadSound[] = { 50, 0 };
int BadSoundNoteDurations[] = { 1, 1 };
int SizeBad = sizeof(GoodSound) / sizeof(GoodSound[0]);

//variaveis auxiliares
int j = 0;
int i = 0;

uint16_t encoderPosition;                //Encoder
float encoderPositionDegree;             //Encoder
uint8_t attempts;                        //Encoder

void setup() {
  //Peripherals
  pinMode(BUZZER_PIN, OUTPUT);
  //Encoder
  pinMode(SPI_SCLK, OUTPUT);
  pinMode(SPI_MOSI, OUTPUT);
  pinMode(SPI_MISO, INPUT);
  pinMode(ENC_0, OUTPUT);

  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  //Encoder
  digitalWrite(ENC_0, HIGH);
  SPI.begin();

  //Conecting to WIFI
  Serial.println("----------");
  Serial.print("Conectando ao Wi-Fi");
  int status = WL_IDLE_STATUS;
  while (status != WL_CONNECTED) {
    status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("IP: ");
  Serial.println(WiFi.localIP());
  Serial.println("----------");
  //Connection to Firebase
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH, WIFI_SSID, WIFI_PASSWORD);
  Firebase.reconnectWiFi(true);  //Let's say that if the connection is down, try to reconnect automatically

  if (!Firebase.beginStream(firebaseData, "/test/data")) {
    //Could not begin stream connection, then print out the error detail
    Serial.println(firebaseData.errorReason());
  }
  
}

void loop() {

  /*
  Serial.println("Bom");
  PlaySound(GoodSound, GoodSoundNoteDurations, SizeGood); // Tocar o som "Bom!"
  delay(500); // Esperar 1 segundo
  */

  /*
  Serial.println("Mau");
  PlaySound(BadSound, BadSoundNoteDurations, SizeBad); // Tocar o som "Mau!"
  delay(500); // Esperar 1 segundo
  */

  readEncoderValues();

  float n = encoderPositionDegree;
  n=round(n * 100) / 100;
  n=ProcessedEncoderValues(n);
  if (Firebase.setFloat(firebaseData, ComplitPath, n)) {
    if (firebaseData.dataType() == "float")      // se hace para asegurar que si el tipo almacenado es entero cogemso el valor correctamente y no basura
      Serial.println(firebaseData.floatData());  //Realmente estamos leyendo con esto el valor introducido
    i = i + 1;

  } else {
    Serial.println("ERROR : " + firebaseData.errorReason());
    Serial.print(i);
    Serial.println();
    i = 0;
  }
  
  delay(250);
}

// Função para tocar um som
void PlaySound(int sound[], int duration[], int size) {
  for (int thisNote = 0; thisNote < size; thisNote++) {
    int noteDuration = 1000 / duration[thisNote];
    tone(BUZZER_PIN, sound[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(BUZZER_PIN);
    delay(50);  // Adiciona um pequeno atraso entre as notas
  }
}

//funcao para ler valores do encoder
void readEncoderValues() {
  attempts = 0;

  encoderPosition = getPositionSPI(RES14);

  while (encoderPosition == 0xFFFF && ++attempts < 3) {
    encoderPosition = getPositionSPI(RES14);
  }

  if (encoderPosition == 0xFFFF) {
    Serial.print("Encoder error. Attempts: ");
    Serial.println(attempts, DEC);
  } else {
    Serial.print("Encoder: ");
    Serial.print(encoderPosition, DEC);
    Serial.print("   -   ");
    encoderPositionDegree = encoderPosition / 45.5083; //transform degrees
    Serial.println(encoderPositionDegree);
    Serial.println();
  }
}

uint16_t getPositionSPI(uint8_t resolution) {
  uint16_t currentPosition;
  bool binaryArray[16];

  currentPosition = spiWriteRead(AMT22_NOP, false) << 8;
  delayMicroseconds(3);
  currentPosition |= spiWriteRead(AMT22_NOP, true);

  for (int i = 0; i < 16; i++) {
    binaryArray[i] = (0x01) & (currentPosition >> (i));
  }

  if ((binaryArray[15] == !(binaryArray[13] ^ binaryArray[11] ^ binaryArray[9] ^ binaryArray[7] ^ binaryArray[5] ^ binaryArray[3] ^ binaryArray[1])) && (binaryArray[14] == !(binaryArray[12] ^ binaryArray[10] ^ binaryArray[8] ^ binaryArray[6] ^ binaryArray[4] ^ binaryArray[2] ^ binaryArray[0]))) {
    currentPosition &= 0x3FFF;
  } else {
    Serial.println("Encoder error.");
    currentPosition = 0xFFFF;
  }

  if ((resolution == RES12) && (currentPosition != 0xFFFF)) {
    currentPosition = currentPosition >> 2;
  }

  return currentPosition;
}

uint8_t spiWriteRead(uint8_t sendByte, uint8_t releaseLine) {
  uint8_t data;

  digitalWrite(ENC_0, LOW);
  delayMicroseconds(3);

  data = SPI.transfer(sendByte);
  delayMicroseconds(3);

  digitalWrite(ENC_0, releaseLine ? HIGH : LOW);

  return data;
}

float ProcessedEncoderValues(float i) {
  if(i>180){
    return i-360;
  }
  else 
    return i;
}

You know never used firebase but doesn't it have limits??
Looks like you're updating the same field 4 times a second..
Even running your own database server that's going to consume allot of resources..
A remote host might consider this a DOS attack and black list your ip for a spell too..

good luck.. ~q

Supposedly it allows you to write 1000 values per second (in a single database), which is why I didn't understand the reason for the error, as it doesn't specify that it can't be in the same field.
But it could be something like that, thanks for the idea, I'll try to solve it assuming that's the problem.

https://firebase.google.com/docs/database/usage/limits?hl=pt

I keep getting the same problem even when I change the way I save the values. Does anyone have any other suggestions?

void loop() {

  /*
  Serial.println("Bom");
  PlaySound(GoodSound, GoodSoundNoteDurations, SizeGood); // Tocar o som "Bom!"
  delay(500); // Esperar 1 segundo
  */

  /*
  Serial.println("Mau");
  PlaySound(BadSound, BadSoundNoteDurations, SizeBad); // Tocar o som "Mau!"
  delay(500); // Esperar 1 segundo
  */

  if (j == 0) {

    /* testar */

    String ComplitPath1 = path1 + "/Value" + String(i);

    // Adiciona zeros à esquerda se o número tiver apenas um dígito
    if (i < 100000000) {
      ComplitPath1 = path1 + "/Value" + String(i, DEC);
    }

    if (i < 10000000) {
      ComplitPath1 = path1 + "/Value0" + String(i, DEC);
    }

    if (i < 1000000) {
      ComplitPath1 = path1 + "/Value00" + String(i, DEC);
    }

    if (i < 100000) {
      ComplitPath1 = path1 + "/Value000" + String(i, DEC);
    }

    if (i < 10000) {
      ComplitPath1 = path1 + "/Value0000" + String(i, DEC);
    }

    if (i < 1000) {
      ComplitPath1 = path1 + "/Value00000" + String(i, DEC);
    }

    if (i < 100) {
      ComplitPath1 = path1 + "/Value000000" + String(i, DEC);
    }

    if (i < 10) {
      ComplitPath1 = path1 + "/Value0000000" + String(i, DEC);
    }

    readEncoderValues();

    float n = encoderPositionDegree;
    n = round(n * 100) / 100;
    n = ProcessedEncoderValues(n);
    if (Firebase.setFloat(firebaseData, ComplitPath1, n)) {
      if (firebaseData.dataType() == "float")      // se hace para asegurar que si el tipo almacenado es entero cogemso el valor correctamente y no basura
        Serial.println(firebaseData.floatData());  //Realmente estamos leyendo con esto el valor introducido
      i = i + 1;

    } else {
      Serial.println("ERROR : " + firebaseData.errorReason());
      Serial.print(i);
      Serial.println();
      //i = 0;
    }
  }

  if (i > 110) {
    j = 1;
  }

  delay(50);
}

Took another peek for you..
Let me ask..
Are you using this mobizt/Firebase-Arduino-WiFiNINA??
If yes, did you read the Warning posted on it..

Looks like you should be using this mobizt/FirebaseClient instead..

Very well could be that the issue you're facing is one that could not be resolved in the old lib and as it's been depreciated, time to upgrade..

good luck.. ~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.