In the code I attached what I am trying to do is to save the "String password = "1234567890"; to EEPROM and than reading it from EEPROM.And after reading it from EEPROM I am converting it into "const char* pass2". It shows no error when I compile and burn it but it shows blank on serial monitor in the password section and it don't even set password.
I am using NODEMCU.
code #1
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFiGeneric.h>
#include <EEPROM.h>
int led = 13;
String state;
String password = "1234567890";
String pass1;
const char* pass2;
//const char* pass3;
String read_StringEE(int Addr, int length);
IPAddress local_IP(192,168,4,2);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
void setup()
{
write_StringEE(0,password);
pass1 = read_StringEE(0,10);
pass2 = pass1.c_str();
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
String mac;
String ssid = "NODEMCU_";
String finalString;
const char* ssid_1;
mac = WiFi.softAPmacAddress().c_str();
finalString = ssid + mac;
ssid_1 = finalString.c_str();
Serial.begin(9600);
Serial.println();
Serial.print("configuring soft-AP configuration ...");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet)? "Ready" : "Failed!");
Serial.print("setting soft-ap ...");
Serial.println(WiFi.softAP(ssid_1, pass2, 9) ? "Ready" : "Failed!");
Serial.println();
Serial.print("ssid:- ");
Serial.print(ssid_1);
Serial.println();
Serial.print("password:- ");
Serial.print(pass1);
Serial.println();
Serial.print("MAC address:-");
Serial.println(mac);
// Serial.println();
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
server.on("/led", [](){String state=server.arg("state");
if(state == "on")
digitalWrite(13, LOW);
//delay(300);
else if(state == "off")
digitalWrite(13, HIGH);
server.send(200, "text/plain", "state");});
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
server.handleClient();
//Serial.printf("Station connected to the AP = %d\n", WiFi.softAPgetStationNum());
delay(1000);
}
code #2
#include <EEPROM.h>
// Absolute min and max eeprom addresses. Actual values are hardware-dependent.
// These values can be changed e.g. to protect eeprom cells outside this range.
const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 1023;
// Returns true if the address is between the
// minimum and maximum allowed values, false otherwise.
//
// This function is used by the other, higher-level functions
// to prevent bugs and runtime errors due to invalid addresses.
boolean eeprom_is_addr_ok(int addr) {
return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
}
// Writes a sequence of bytes to eeprom starting at the specified address.
// Returns true if the whole array is successfully written.
// Returns false if the start or end addresses aren't between
// the minimum and maximum allowed values.
// When returning false, nothing gets written to eeprom.
boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
// counter
int i;
// both first byte and last byte addresses must fall within
// the allowed range
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}
for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
}
return true;
}
// Writes a string starting at the specified address.
// Returns true if the whole string is successfully written.
// Returns false if the address of one or more bytes fall outside the allowed range.
// If false is returned, nothing gets written to the eeprom.
boolean eeprom_write_string(int addr, const char* string) {
int numBytes; // actual number of bytes to be written
//write the string contents plus the string terminator byte (0x00)
numBytes = strlen(string) + 1;
return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}
// Reads a string starting from the specified address.
// Returns true if at least one byte (even only the string terminator one) is read.
// Returns false if the start address falls outside the allowed range or declare buffer size is zero.
//
// The reading might stop for several reasons:
// - no more space in the provided buffer
// - last eeprom address reached
// - string terminator byte (0x00) encountered.
boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
byte ch; // byte read from eeprom
int bytesRead; // number of bytes read so far
if (!eeprom_is_addr_ok(addr)) { // check start address
return false;
}
if (bufSize == 0) { // how can we store bytes in an empty buffer ?
return false;
}
// is there is room for the string terminator only, no reason to go further
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0; // initialize byte counter
ch = EEPROM.read(addr + bytesRead); // read next byte from eeprom
buffer[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
// stop conditions:
// - the character just read is the string terminator one (0x00)
// - we have filled the user buffer
// - we have reached the last eeprom address
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
// if no stop condition is met, read the next byte from eeprom
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
}
// make sure the user buffer has a string terminator, (0x00) as its last byte
if ((ch != 0x00) && (bytesRead >= 1)) {
buffer[bytesRead - 1] = 0;
}
return true;
}
String read_StringEE(int Addr, int length)
{
char cbuff[length+1];
eeprom_read_string(Addr, cbuff, length+1);
String stemp(cbuff);
return stemp;
}
bool write_StringEE(int Addr, String input)
{
char cbuff[input.length()+1];//Finds length of string to make a buffer
input.toCharArray(cbuff,input.length()+1);//Converts String into character array
return eeprom_write_string(Addr,cbuff);//Saves String
}
The output on the Serial monitor shows this:-
configuring soft-AP configuration ...Ready
setting soft-ap ...Ready
ssid:- NODEMCU_5E:CF:7F:F0:56:12
password:-
MAC address:-5E:CF:7F:F0:56:12
Soft-AP IP address = 192.168.4.2
HTTP server started
I have attached the code in zip.
Please tell me if I am wrong somewhere.
softAP_new.zip (2.66 KB)