Esp_sleep_enable_timer_wakeup

Hello everyone!

This is my first time posting here and sorry if i do not post in the correct category . I want to ask you if there is any possibility to change

#define TIME_TO_SLEEP 5; 
to 
#define TIME_TO_SLEEP yourInputString = readFile(SPIFFS, "/inputString.txt"); 

In the "/inputString.txt" is a number like 30 . I want to be able to put that number in the TIME_TO_SLEEP if there is a possibility .

server.on("/L", HTTP_GET, [] 
(AsyncWebServerRequest *request) {
digitalWrite(led_gpio, LOW);                // GET /L turns the LED off
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);
    esp_deep_sleep_start();

request->send(200, "text/plain", "ok");
});

I tried putting

   #define TIME_TO_SLEEP yourInputString =  readFile(SPIFFS, "/inputString.txt"); 

and i get this error

 'yourInputString' was not declared in this scope 

I am using SPIFFS to write and read the file inputString.txt

I am a beginner in programming so be kind with me please . Thank you in advanced !

Canned message to you that may help you help us.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.
Please post an image of your project.
Please describe the problem better then you just did.

define TIME_TO_SLEEP yourInputString = readFile(SPIFFS, "/inputString.txt");
You want to use a variable not a define
String yourInputString = readFile(SPIFFS, "/inputString.txt");

As a note SPIFF's have been depreciated on the ESP's. Use preference's of littleFS.

Here is my sketch :

#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
#include "FS.h"


const char* ssid = "myssid";
const char* password =  "mypass";
const byte led_gpio = 22;

#define TIME_TO_SLEEP yourInputString

AsyncWebServer server(80);

IPAddress local_IP(192, 168, 0, 8);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

const char* PARAM_STRING = "inputString";
const char* PARAM_INT = "inputInt";
const char* PARAM_FLOAT = "inputFloat";

// HTML web page to handle 3 input fields (inputString, inputInt, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <title>ESP Input Form</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script>
    function submitMessage() {
      alert("Saved value to ESP SPIFFS");
      setTimeout(function(){ document.location.reload(false); }, 500);   
    }
  </script></head><body>
  <form action="/get" target="hidden-form">
    inputString (current value %inputString%): <input type="text" name="inputString">
    <input type="submit" value="Submit" onclick="submitMessage()">
  </form><br>
  
  
  <iframe style="display:none" name="hidden-form"></iframe>
</body></html>)rawliteral";

void notFound(AsyncWebServerRequest *request) {
  request->send(404, "text/plain", "Not found");
}

String readFile(fs::FS &fs, const char * path) {
  Serial.printf("Reading file: %s\r\n", path);
  File file = fs.open(path, "r");
  if (!file || file.isDirectory()) {
    Serial.println("- empty file or failed to open file");
    return String();
  }
  Serial.println("- read from file:");
  String fileContent;
  while (file.available()) {
    // fileContent+=String((char)file.read());
    for (int i = 1; i <= 10; i++) {
      String line = file.readStringUntil('n');
      Serial.println(line);
    }
  }
  file.close();
  Serial.println(fileContent);
  return fileContent;
}

void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\r\n", path);
  File file = fs.open(path, "w");
  if (!file) {
    Serial.println("- failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("- file written");
  } else {
    Serial.println("- write failed");
  }
  file.close();
}

// Replaces placeholder with stored values
String processor(const String& var) {
  //Serial.println(var);
  if (var == "inputString") {
    return readFile(SPIFFS, "/inputString.txt");
  }
  else if (var == "inputInt") {
    return readFile(SPIFFS, "/inputInt.txt");
  }
  else if (var == "inputFloat") {
    return readFile(SPIFFS, "/inputFloat.txt");
  }
  return String();
}

void setup() {
  ///////////////////////////////
  SPIFFS.begin();
  pinMode(led_gpio, OUTPUT);
  digitalWrite(led_gpio, HIGH);



  Serial.begin(115200);
  // Initialize SPIFFS
#ifdef ESP32
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
#else
  if (!SPIFFS.begin()) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
#endif

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Failed!");
    return;
  }
  Serial.println();
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Send web page with input fields to client
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send_P(200, "text/html", index_html, processor);
  });

  // Send a GET request to <ESP_IP>/get?inputString=<inputMessage>
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {
    String inputMessage;
    // GET inputString value on <ESP_IP>/get?inputString=<inputMessage>
    if (request->hasParam(PARAM_STRING)) {
      inputMessage = request->getParam(PARAM_STRING)->value();
      writeFile(SPIFFS, "/inputString.txt", inputMessage.c_str());
    }
    // GET inputInt value on <ESP_IP>/get?inputInt=<inputMessage>
    else if (request->hasParam(PARAM_INT)) {
      inputMessage = request->getParam(PARAM_INT)->value();
      writeFile(SPIFFS, "/inputInt.txt", inputMessage.c_str());
    }
    // GET inputFloat value on <ESP_IP>/get?inputFloat=<inputMessage>
    else if (request->hasParam(PARAM_FLOAT)) {
      inputMessage = request->getParam(PARAM_FLOAT)->value();
      writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str());
    }
    else {
      inputMessage = "No message sent";
    }
    Serial.println(inputMessage);
    request->send(200, "text/text", inputMessage);
  });

  // Receive an HTTP GET request
  server.on("/H", HTTP_GET, [] (AsyncWebServerRequest * request) {
    digitalWrite(led_gpio, HIGH);               // GET /H turns the LED on
    delay(500);
    digitalWrite(led_gpio, LOW);
    delay(500);
    digitalWrite(led_gpio, HIGH);
    delay(500);
    digitalWrite(led_gpio, LOW);
    delay(500);
    digitalWrite(led_gpio, HIGH);
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);
    esp_deep_sleep_start();
    request->send(200, "text/plain", "ok");
  });

  // Receive an HTTP GET request
  server.on("/L", HTTP_GET, [] (AsyncWebServerRequest * request) {
    digitalWrite(led_gpio, LOW);                // GET /L turns the LED off
    request->send(200, "text/plain", "ok");
  });

  // Receive an HTTP GET request
  server.on("/O", HTTP_GET, [] (AsyncWebServerRequest * request) {
    digitalWrite(led_gpio, HIGH);                // GET /L turns the LED off
    request->send(200, "text/plain", "ok");
  });

  server.onNotFound(notFound);
  server.begin();
}

void loop() {
  // To access your stored values on inputString, inputInt, inputFloat
  String yourInputString = readFile(SPIFFS, "/inputString.txt");
  Serial.print("*** Your inputString: ");
  Serial.println(yourInputString);

  //int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
  // Serial.print("*** Your inputInt: ");
  // Serial.println(yourInputInt);

  // float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
  // Serial.print("*** Your inputFloat: ");
  // Serial.println(yourInputFloat);
  delay(5000);


}

And my error is :

Arduino: 1.8.19 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

C:\Users\Zet\AppData\Local\Temp\arduino_modified_sketch_284202\sketch_nov04a.ino: In lambda function:

sketch_nov04a:11:23: error: 'yourInputString' was not declared in this scope

 #define TIME_TO_SLEEP yourInputString

                       ^

C:\Users\Zet\AppData\Local\Temp\arduino_modified_sketch_284202\sketch_nov04a.ino:171:35: note: in expansion of macro 'TIME_TO_SLEEP'

     esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);

                                   ^

Multiple libraries were found for "WiFi.h"

 Used: C:\Users\Zet\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi

 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi

exit status 1

'yourInputString' was not declared in this scope

Thank you!

const byte led_gpio = 22;
String yourInputString ="I'm need to be defined before I can be used";
#define TIME_TO_SLEEP yourInputString

AsyncWebServer server(80);

Now its defined.

Follow the link in post#2 about to learn about variables.

const byte led_gpio = 22;

String yourInputString = readFile(SPIFFS, "/inputString.txt");

#define TIME_TO_SLEEP yourInputString

i get this error

Arduino: 1.8.19 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

sketch_nov04abcde:11:61: error: 'readFile' was not declared in this scope

 String yourInputString = readFile(SPIFFS, "/inputString.txt");

                                                             ^

C:\Users\Zet\Documents\Arduino\sketch_nov04abcde\sketch_nov04abcde.ino: In lambda function:

sketch_nov04abcde:173:48: error: cannot convert 'String' to 'uint64_t {aka long long unsigned int}' for argument '1' to 'esp_err_t esp_sleep_enable_timer_wakeup(uint64_t)'

     esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);

                                                ^

Multiple libraries were found for "WiFi.h"

 Used: C:\Users\Zet\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi

 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi

exit status 1

'readFile' was not declared in this scope

Thank you !

Is there a function called readfile?

This is it?

String readFile(fs::FS &fs, const char * path) {
  Serial.printf("Reading file: %s\r\n", path);
  File file = fs.open(path, "r");
  if (!file || file.isDirectory()) {
    Serial.println("- empty file or failed to open file");
    return String();

If i read the file in the serial monitor the output of the text in /inputInt.txt is correct , but when i put in :

esp_sleep_enable_timer_wakeup(readFile(SPIFFS, "/inputInt.txt").toInt());

it returns 0

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.