0

I'm working on administration for my ESP8266 based IoT thing.
I can edit some settings via web server and store them to EEPROM.

But I'm stuck at setting default input form value that I read from EEPROM. I saved SSID to EEPROM, I can read it back and connect to it, so I know it written right.

Serial.print(); prints: secretwifi (SSID of wifi). But once I try to set it as default value for input form text field I get:

���?etwi0��?�!@P��?`��?0��?B!@���?���?��?���?
void EEPROMwriteChar(int address, String to_save) {
 int how_long= to_save.length(); 

 EEPROM.write(address, how_long); // zápis délky řetězce na nultou adresu
 EEPROM.commit();

 for ( int i = 0; i <= how_long; i++) {
   if (i < delka) {
     EEPROM.write((address+ 1 + i), to_save[i]);
   }
   else {
     EEPROM.write((address+ 1 + i), '\0'); // ukončení řetězce
   }
   EEPROM.commit();
 }
}

char* EEPROMreadChar(int address) {
 int how_long = EEPROM.read(address);
 char value[how_long];

 if (how_long > 0) {
   for (int i = 0; i <= how_long; i++) {
     value[i] = EEPROM.read((address + 1 + i));
   }
   return value;
 }
}

Those are the read and write functions.

HTML output function:

String formWifiSettings() {

  char* WSSID = EEPROMreadChar(0);
  Serial.print("SSID HTML:");
  Serial.println(WSSID);

  String html = "<form action='/wifi' method='POST' id='wifi_form'> \n";

  //...some more html code...

  html += "<td><input name='WIFI_SSID' type='text' id='WIFI_SSID' size='40'     maxlength='63' ";

  html += "value='";
  html += String(WSSID);  
  html += "'";

  html += " /></td> \n";

  //...some more html code again...

  return html;
}

Serial output:

SSID HTML:secretwifi

html: 
    input name="WIFI_SSID" id="WIFI_SSID" size="40" maxlength="63" value="���?etwi0��?�!@P��?`��?0��?B!@���?���?��?���?" type="text"

Even without String(); conversion it's garbage.

Maybe it's a stupid question, but I' Googling it for two days like a mad man, without success.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • You have a pointer to a string that doesn't exist anymore. It's probably also missing a nul terminator. – gre_gor Aug 17 '18 at 20:24
  • don't bother with EEPROM, use SPIFFS; so much better and simpler. – dandavis Aug 17 '18 at 20:52
  • Thank you @gre_gor, that was usefull. I was able to fix my issue with your hint. I still have no idea how pointers work, but hey, it's fixed. I have so much to learn... – Martin Štilc Aug 24 '18 at 10:27
  • Thank you too @dandavis. Once i finish the basics, i have a look on that. If it could be used to store HTML pages for administration of ESP too, it would be awesome. – Martin Štilc Aug 24 '18 at 10:30

0 Answers0