Go Down

Topic: Getting wrong characters in EEPROM.get (Read 117 times) previous topic - next topic

eliastuzo10

Oct 16, 2020, 07:58 am Last Edit: Oct 16, 2020, 08:06 am by eliastuzo10
Good night everybody. I'm in need of a bit of help, thanks a lot in advance. I've this code that receives a string named ''valor'' via bluetooth, then writes it into the EEPROM memory in a charArray called "contrasena". The thing is that, for example, when I send "4039ED5C", what I get in the variable "contra", which stores in a string the data read from the EEPROM, is "⸮'⸮⸮⸮⸮?⸮⸮⸮?T@4039ED5C." I'm assuming there's a mistake in my handling of the address, but I don't know how to solve it. Thanks a lot!
Code: [Select]
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <EEPROM.h>

String valor;
const char* aleatorio;
String alea = "2";

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();
      pCharacteristic->setValue(alea.c_str()); // Pone el numero aleatorio

      if (value.length() > 0) {
        valor = "";
        for (int i = 0; i < value.length(); i++) {
          // Serial.print(value[i]); // Presenta value.
          valor = valor + value[i];
        }
        int longitud = 0;
        longitud = valor.length() + 1;
        char contrasena[longitud];
        valor.toCharArray(contrasena, longitud);
        EEPROM.put(0, contrasena[longitud]);
        EEPROM.commit();
        char cguardada[longitud];
        EEPROM.get(0, cguardada[longitud]);
        String contra = String(cguardada);
        Serial.println("*********");
        Serial.print("valor = ");
        Serial.println(valor); // Presenta valor.
        Serial.println(longitud);
        Serial.println(contrasena);
        Serial.println(cguardada);
      }
    }
};

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);
  BLEDevice::init("ESP32");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());
  pCharacteristic->setValue("Iniciado.");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
  //Serial.println("valor en el loop = ");
  //Serial.println(valor); // Presenta valor.
  aleatorio = "Contraseña modificada"; // Crea el numero aleatorio.
  alea = (String) aleatorio; // Lo convierte en String.
}

sterretje

I think that this will work better
Code: [Select]
EEPROM.put(0, contrasena);
...
...
EEPROM.get(0, cguardada);
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

eliastuzo10

Already tried that, and it sends error on those lines saying that it doesn't fit the function EEPROM.get requirements. Any other ideas on what may actually be happening? Thanks for replying anyways :) 

sterretje

#3
Oct 17, 2020, 02:14 pm Last Edit: Oct 17, 2020, 02:14 pm by sterretje
I don't have an ESP or whatever you are using ;) So I can't test. For the AVR, I get an error if I try
Code: [Select]
#include <EEPROM.h>

char txt[] = "4039ED5C";

void setup()
{
  EEPROM.put(2, txt);

  int cnt = strlen(txt) + 1;
  char txt2[cnt];

  EEPROM.get(2, txt2);
  Serial.begin(57600);
  Serial.println(txt2);
}

void loop()
{
}


Code: [Select]
Arduino: 1.8.5 (Windows 10), Board: "SparkFun RedBoard"

In file included from C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_214167\sketch_oct17a.ino:1:0:

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_214167\sketch_oct17a.ino: In function 'void setup()':

sketch_oct17a:12: error: no matching function for call to 'EEPROMClass::get(int, char [cnt])'

   EEPROM.get(2, txt2);

                     ^

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_214167\sketch_oct17a.ino:12:21: note: candidate is:

In file included from C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_214167\sketch_oct17a.ino:1:0:

C:\Users\sterretje\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\EEPROM\src/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)

     template< typename T > T &get( int idx, T &t ){

                               ^

C:\Users\sterretje\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\EEPROM\src/EEPROM.h:130:31: note:   template argument deduction/substitution failed:

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_214167\sketch_oct17a.ino:12:21: note:   variable-sized array type 'char [cnt]' is not a valid template argument

   EEPROM.get(2, txt2);

I suggest that you test with fixed size arrays for your cguardada and contrasena and make the change that I sugegsted.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up