Go Down

Topic: esp32 deep sleep works but (Read 44 times) previous topic - next topic

Jean24816

Hi to everyone,

I have   marked the sleep  lines with  <<<<==================================


I took all the functions out of the file because it is too big for the forum.

The sketch works perfectly without the sleep feature.

The setup function is executed every two minutes, but the web page is not open nor is the Serial.print is not executed in the loop function.

I tried placing the two lines

 
Code: [Select]

 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);       
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);   


at the beginning of the setup function  and at the beginning of the loop function, same results.

The full code may be found  here:
   https://www.dropbox.com/s/x5bysoo3gmdx6v2/DHT_Ruis_SantosV1.1.ino?dl=0

Code: [Select]


#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 120 /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;


//===========  SETUP BEGIN  ====================================
void setup() {

  uint8_t xCnt = 0;
// initialize the DHT sensor
  dht.begin();
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    // Connect to WPA/WPA2 network.
    //Change this line if using open or WEP network:
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();

  //if a temp file has already been in use, open it and load it's data to array.
  SPIFFS.begin();
  //listDir(SPIFFS, "/", 0);

  if (!SPIFFS.begin()) {
    Serial.println(" ! SPIFFS.begin()");
    return;
  }

  File f = SPIFFS.open("/gardenTemp.txt", "r");

  if (!f) {
    Serial.println("file open failed");
  }
  Serial.println("====== Setup Start reading from SPIFFS file =======");

  while (f.available() && xCnt < maxCount) {
    //Lets read line by line from the file
    String line = f.readStringUntil('\n');
    if (line != 0) {
      Serial.print(xCnt);
      Serial.print("  ");
      Serial.println(line);
      datArray[xCnt] = line;
      xCnt ++;
    }
  }
  f.close();
  Serial.println("====== Setup End reading from SPIFFS file =======");
 

    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);        <<<<==================================
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);      <<<<==================================
 
}

//===========  SETUP END  ====================================


void loop() {
  Serial.println("Client Connected  bootCount : " + (String)(bootCount++));

  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New client");
    memset(linebuf, 0, sizeof(linebuf));
    charcount = 0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        //read char by char HTTP request
        linebuf[charcount] = c;
        if (charcount < sizeof(linebuf) - 1) charcount++;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
          float h = dht.readHumidity();
          // Read temperature as Celsius (the default)
          float t = dht.readTemperature();
          double dew = dewPoint(t, h);
          // Read temperature as Fahrenheit (isFahrenheit = true)
          float f = dht.readTemperature(true);
          // Check if any reads failed and exit early (to try again).
          if (isnan(h) || isnan(t) || isnan(f)) {
            Serial.println("Failed to read from DHT sensor!");
            strcpy(celsiusTemp, "Failed");
            strcpy(fahrenheitTemp, "Failed");
            strcpy(humidityTemp, "Failed");
          }
          else {
            // Computes temperature values in Celsius + Fahrenheit and Humidity
            hic = dht.computeHeatIndex(t, h, false);
            dtostrf(hic, 6, 2, celsiusTemp);
            float hif = dht.computeHeatIndex(f, h);
            dtostrf(hif, 6, 2, fahrenheitTemp);
            dtostrf(h, 6, 2, humidityTemp);
            // You can delete the following Serial.print's, it's just for debugging purposes

            val = touchRead(pinToRead);
            val = map(val, 500, 0, 0, 100); //may use an exponential function
            Serial.print("myDate()  ");
            aNewLine += myDate();
            Serial.println(aNewLine);
            aNewLine += " : " + (String)val + "%  Soil Moisture\tTemperature: " ;
            aNewLine += (String)t + " °C Humidity: " + (String)h;
            aNewLine += "%\t Heat index: " + (String)hic + " °C  Dew " + (String)dew + "%\n" ;
            apPend(aNewLine);
            Serial.println(aNewLine);

            for (int stat = decCnt; stat >= 0; stat--) {
              if (datArray[stat] != 0) {
                Serial.println(datArray[stat]);
              }
              Serial.println(datArray[stat]);
            }
          }

          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          //client.println("Content-Type: image/gif");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println();
          client.println("<!DOCTYPE HTML><html lang=\"fr\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
          client.println("<meta http-equiv=\"refresh\" content=\"30\"></head>");
          client.println("<body style=\"color:#f3d6e1;\"><div style=\"font-size: 3.5rem;color:green;\"><p style=\"text-align:center;\">Météo</p>");
          if (atoi(celsiusTemp) >= 25) {
            client.println("<div style=\"color: #930000;\"><p style=\"text-align:center;\">");
          }
          else if (atoi(celsiusTemp) < 25 && atoi(celsiusTemp) >= 5) {
            client.println("<div style=\"color: #006601;\"><p style=\"text-align:center;\">");
          }
          else if (atoi(celsiusTemp) < 5) {
            client.println("<div style=\"color: #009191;\">");
          }
          client.println("<p style=\"text-align:center;font-size:14pt;\">");
          client.println(myDate());
          client.println("</p><p></p><p style=\"text-align:center;\">");
          client.print(celsiusTemp);  //<img src=\"./temperature.gif\" width=\"504\" height=\"459\">
          client.print("&deg;C&nbsp;&nbsp;&nbsp;&nbsp;");
          client.print(fahrenheitTemp);
          client.println(" &deg;F</p><p style=\"text-align:center;\">");
          client.print(humidityTemp);
          client.print("&nbsp;%H&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
          client.print(dew);
          client.println("&nbsp;&nbsp;&deg; Rosé</p><p style=\"text-align:center;\">");
          client.print(hic);
          client.print("&nbsp;&deg;Ressenti&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
          client.print(val);
          client.print("&nbsp;%Soil Moisture</p><p style=\"text-align:center;font-size:12pt;\">");
          for (int stat = decCnt; stat >= 0; stat--) {
            if (datArray[stat] != 0) {
              client.print(datArray[stat] + "<br>");
            }
          }

          client.print("</p></div>");
          client.print("</body></html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          memset(linebuf, 0, sizeof(linebuf));
          charcount = 0;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }

    // give the web browser time to receive the data
    delay(10000);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected for : " + (String)(TIME_TO_SLEEP / 60) + " minutes");
  }
 
  Serial.flush();
  esp_deep_sleep_start();    <<<<==================================

}




Regards,


JPDaviau

Go Up