-->
Page 1 of 2

Unable to Get SSID and PASSWORD from SPIFFS before Webserver

PostPosted: Sun Mar 06, 2016 5:01 pm
by Ayush Sharma
Hello Everyone,
Please Read this post full , i am in really big trouble :!: . I am working with Spiffs +ESP8266WebServer. My ESP-12e keeps on resetting if i put spiffs.begin command before wifi.softAP command . What i want to do is, I have 2 Files in SPIFFS memory
1) ssid.txt
2) pass.txt
When the Module Starts it should begin with spiffs memory and then reads these 2 files and get ssid and password for softAP.
Data from 2 Files are Stared in Strings. which in inputed by adding ".c_str" in Wifi.softAP.
Please see the code below. ESP Starts giving Reset Erroe and Boot error .
Code: Select all#include <FS.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* host = "bubbles-webupdate";
char *password;
char *ssid;


ESP8266WebServer server(80);


  String SSIDfinal = "";
  String PASSfinal = "";

 

const char SETTINGS_HTML[] =
"<!DOCTYPE HTML>"
"<html>"
"<head>"
"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
"<title>Settings</title>"
"<style>"
"\"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\""
"</style>"
"</head>"
"<body>"
"<h1>Settings</h1>"
"<FORM action=\"/submit\" method=\"post\">"
"<P>"
"<h4>Setup WiFi SSID & PASSWORD</h4>"
"Ssid:<BR>(Minimum 5 Characters)"
"<INPUT type=\"text\" name=\"SSID\"><BR>"
"Password: (Minimum 8 Characters Or Wifi will Fail!)<BR>"
"<INPUT type=\"text\" name=\"PASS\"><BR>"
"<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
"</P>"
"</FORM>"
"</body>"
"</html>";

void handlevalues(){
 
  File ssidvalue = SPIFFS.open("/config/ssid.txt", "r");
  if (ssidvalue){Serial.println("ssidvalue Success");
  while (ssidvalue.available()) {
  SSIDfinal = ssidvalue.read();
  }Serial.println(*ssid);}
  ssidvalue.close();
 
   File passvalue = SPIFFS.open("/config/pass.txt", "r");
  if (passvalue){Serial.println("passvalue Success");
  while (passvalue.available()){
  PASSfinal = passvalue.read();
  }Serial.println(*password);}
  passvalue.close();
 
}


void returnFail(String msg)
{
  server.sendHeader("Connection", "close");
  server.sendHeader("Access-Control-Allow-Origin", "*");
  server.send(500, "text/plain", msg + "\r\n");
}

void handleSettings(){
  server.send(200, "text/html", SETTINGS_HTML);
}

void handleSubmit(){
  String SSIDvalue;
  String PASSvalue;
 
  if (!server.hasArg("SSID")) return returnFail("BAD ARGS");
  SSIDvalue = server.arg("SSID");
Serial.println(SSIDvalue);

  if (!server.hasArg("PASS")) return returnFail("BAD ARGS");
  PASSvalue = server.arg("PASS");
Serial.println(PASSvalue);

  File ssid = SPIFFS.open("/config/ssid.txt", "w");
  if (ssid){
  ssid.print(SSIDvalue);
  ssid.close();
  }
  File pass = SPIFFS.open("/config/pass.txt", "w");
  if (pass){
  pass.print(PASSvalue);
  pass.close();
  }
  server.send(200, "text/html", "<html>"
  "<title>Settings Saving..</title>"
  "<body>"
  "<h1>Setting Are Getting Saved...</h1><BR>"
  "<h5>Please Try Connecting Module After 30 Seconds!... </h5>");
 
  Serial.println("AT");
  delay(1000);
  Serial.println("AT+RST");
 }


void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
  Serial.println("");
    SPIFFS.begin();
  handlevalues();
  Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(SSIDfinal.c_str(), PASSfinal.c_str());
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleSettings);
  server.on("/settings/submit", handleSubmit);
 
}

void loop() {
  // put your main code here, to run repeatedly:

  server.handleClient();
}


Please Help me , Your Support is very great to me. :) Files Attached Below.

Re: Unable to Get SSID and PASSWORD from SPIFFS before Webse

PostPosted: Mon Mar 07, 2016 9:47 am
by martinayotte
The way you read the ssid/password is not working at all since you overwriting each read characters in the string until the last one.

Re: Unable to Get SSID and PASSWORD from SPIFFS before Webse

PostPosted: Mon Mar 07, 2016 7:48 pm
by Ayush Sharma
martinayotte wrote:The way you read the ssid/password is not working at all since you overwriting each read characters in the string until the last one.


Anyother Possible Way? Or should i use "char" ?

Re: Unable to Get SSID and PASSWORD from SPIFFS before Webse

PostPosted: Mon Mar 07, 2016 8:05 pm
by Mmiscool
I use the following 2 functions to read and write data to the spiffs flash.


Maybe it will be helpful to you.

Code: Select allvoid SaveDataToFile(String fileNameForSave, String DataToSave)
{
  Serial.println(fileNameForSave);
  //SPIFFS.begin();
  File f = SPIFFS.open(String("/data/" + fileNameForSave + ".dat"), "w");
  if (!f)
  {
    PrintAndWebOut(F("file open failed"));
  }
  else
  {
    f.println(String(DataToSave + String('\r')));
    f.close();
  }
  return;
}



String LoadDataFromFile(String fileNameForSave)
{
  String WhatIwillReturn;
  //SPIFFS.begin();
  File f = SPIFFS.open(String("/data/" + fileNameForSave + ".dat"), "r");
  if (!f)
  {
    fileOpenFail = 1;
    //Serial.print("file open failed  :");
    //Serial.println(fileNameForSave);
  }
  else
  {
    fileOpenFail = 0;
    WhatIwillReturn =  f.readStringUntil('\r');
    WhatIwillReturn.replace("\n", "");
    f.close();
    return WhatIwillReturn;
  }
}