Post topics, source code that relate to the Arduino Platform

User avatar
By Ayush Sharma
#42659
Mmiscool wrote: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;
  }
}


Thanks For Helping but i have another Problem with this Code, When i add Spiffs.begin(); before WiFi.softAP(ssid, password);
Command then the Module Keeps on Resetting with rst and boot errors in Serial.

Please let me know the problem. or if you can run this code on your own esp, ( It will not Brick it , Assured and tested :D )

Once Again, Your Help is Very Valuable.!
User avatar
By Mmiscool
#42660 I warped up my own functions to put it in ap mode or station mode. It will also do an automatic fail over and save these setting for next time so if you provide blank values to the functions it will ignore them.

These have been working in my project for some time.

You do need the file functions to. These functions use strings which might make things a bit simpler also.

Code: Select all//wifi code here

bool ConnectToTheWIFI(String NetworkName, String NetworkPassword, String NetworkStaticIP, String NetworkGateway, String NetworkSubnet)
{
  WiFi.mode(WIFI_AP_STA);
  byte numberOfAtempts = 0;
  int str_len = NetworkName.length() + 1;
  char ssid[str_len];
  NetworkName.toCharArray(ssid, str_len);

  str_len = NetworkPassword.length() + 1;
  char password[str_len];
  NetworkPassword.toCharArray(password, str_len);


  // Connect to WiFi network
  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    numberOfAtempts = numberOfAtempts  + 1;
    delay(1000);
    Serial.println(numberOfAtempts);
    if (numberOfAtempts >= 12)
    {
      Serial.println("");
      Serial.println(F("Failed Wifi Connect "));
      return 0;
    }
  }

delay(1000);

  if (NetworkStaticIP != "" & NetworkGateway != "" & NetworkSubnet != "" )
  {

    NetworkStaticIP += ".";
    NetworkGateway += ".";
    NetworkSubnet += ".";
    IPAddress ip(     getValue(NetworkStaticIP, '.', 0).toInt(), getValue(NetworkStaticIP, '.', 1).toInt(), getValue(NetworkStaticIP, '.', 2).toInt(), getValue(NetworkStaticIP, '.', 3).toInt());
    IPAddress gateway(getValue(NetworkGateway,  '.', 0).toInt(), getValue(NetworkGateway, '.', 1).toInt(), getValue(NetworkGateway, '.', 2).toInt(), getValue(NetworkGateway, '.', 3).toInt());
    IPAddress subnet( getValue(NetworkSubnet,   '.', 0).toInt(), getValue(NetworkSubnet, '.', 1).toInt(), getValue(NetworkSubnet, '.', 2).toInt(), getValue(NetworkSubnet, '.', 3).toInt());
    WiFi.config(ip, gateway, subnet);
  }

delay(1000);

  if (WiFi.localIP().toString().endsWith(".0"))
  {
    Serial.println(WiFi.localIP());
    CreateAP("", "");
    return 0;
  }
  else
  {
    //configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
    configTime(LoadDataFromFile("TimeZone").toFloat() * 3600, LoadDataFromFile("DaylightSavings").toInt(), "pool.ntp.org", "time.nist.gov");
    Serial.println("");
    Serial.print(F("Connected to "));
    Serial.println(ssid);
    Serial.print(F("IP address : "));
    Serial.println(WiFi.localIP());
    SaveDataToFile("WIFIname" ,  NetworkName);
    SaveDataToFile("WIFIpass", NetworkPassword);
    return 1;
  }


}




void CreateAP(String NetworkName, String NetworkPassword)
{
  WiFi.mode(WIFI_AP_STA);
  Serial.println(F("Creating WIFI access point"));


  if (NetworkName == "")
  {
    NetworkName = LoadDataFromFile("APname");
    NetworkPassword = LoadDataFromFile("APpass");

    if (NetworkName == "")
    {
      NetworkName = "ESP";
      NetworkPassword = "";
    }
  }

  Serial.println(NetworkName);


  int str_len = NetworkName.length() + 1;
  char ssid[str_len];
  NetworkName.toCharArray(ssid, str_len);

  str_len = NetworkPassword.length() + 1;
  char password[str_len];
  NetworkPassword.toCharArray(password, str_len);

 
  delay(2000);
  if (NetworkPassword.length() < 8)
  {
    WiFi.softAP(ssid);
  }
  else
  {
    WiFi.softAP(ssid, password);
  }
  delay(2000);

  SaveDataToFile("APname" ,  NetworkName);
  SaveDataToFile("APpass", NetworkPassword);
}
User avatar
By martinayotte
#42664
Ayush Sharma wrote:
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" ?


I think you didn't read my response thoroughly , you've coded :
Code: Select allSSIDfinal = ssidvalue.read();
where it should be :
Code: Select allSSIDfinal += ssidvalue.read();

:ugeek: