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;
}