1

I have a temperature/humidity project using an ESP-01 and DHT11. I want to be able to put t into deep sleep to preserve battery life. I have soldered a wire from GPIO16 to RST.

#include <SimpleTimer.h>  //https://github.com/jfturcot/SimpleTimer
#include <ESP8266WiFi.h>
#include <PubSubClient.h>  //https://github.com/knolleary/pubsubclient
#include <ESP8266mDNS.h> 
#include <WiFiUdp.h>
#include <ArduinoOTA.h>  //https://github.com/esp8266/Arduino/tree/master/libraries/ArduinoOTA
#include "DHT.h"

//USER CONFIGURED SECTION START//
#define DHTPIN 2     // what digital pin we're connected to

const char* ssid = "mySSID";
const char* password = "password";
const char* mqtt_server = "mqttserverip";
const int mqtt_port = 1883;
const char *mqtt_user = "usser";
const char *mqtt_pass = "pass";
const char *mqtt_client_name = "DryerMCU"; // Client connections can't have the same connection name
//USER CONFIGURED SECTION END//

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);


WiFiClient espClient;
PubSubClient client(espClient);
SimpleTimer timer;

bool boot = true;
int status = WL_IDLE_STATUS;

//Functions
void setup_wifi() 
{
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void getTemperature()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // 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();
  // 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!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");


  char temperature[50];
  char humidity[50];

   // Update temperature and humidity
  String temp_str = String(f);
  temp_str.toCharArray(temperature, temp_str.length() + 1);
  client.publish("dryer/temperature", temperature);

  String hum_str = String(h);
  hum_str.toCharArray(humidity, hum_str.length() + 1);
  client.publish("dryer/humidity", humidity);

}

void setup() {
  Serial.begin(38400);
  delay(2000);
  Serial.println("Booting");

  dht.begin();
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  getTemperature();

  Serial.println("Going to sleep");
  ESP.deepSleep(10000000,RF_DEFAULT);
}

void loop() {

}

When I start it up it takes one measurement, goes to sleep and then wakes up, but nothing happens once it wakes up. Serial output:

Booting

Connecting to SSID
  ...... 
WiFi connected  
IP address: 
mqttip  
Humidity: 51.00 %   Temperature: 20.00 *C 68.00 *F  Heat index: 19.39 *C 66.90 *F  
Going to sleep

ets Jan  8 2013,rst cause:5, boot mode:(3,7)

ets_main.c
7
  • I added a Serial.println("woke up"); right after ESP.DeepSleep and it never prints that line. Also, I was under the impression that the setup() method gets called again when it wakes up as if it had just been reset
    – Brian
    Jan 21 '20 at 23:54
  • That did not help. It still doesn't do anything after the ESP.DeepSleep
    – Brian
    Jan 22 '20 at 0:27
  • reduce your code to minimum ... example code is here at "basic deep sleep" ... losant.com/blog/making-the-esp8266-low-powered-with-deep-sleep .... i was very wrong about putting code in loop() ... looks like the esp resets when it wakes up
    – jsotola
    Jan 22 '20 at 2:26
  • 1
    I tried the very basic version from your link and still get the same result. It appears to sleep, but nothing runs after it wakes up. What else could it be?
    – Brian
    Jan 22 '20 at 4:44
  • 1
    I'd try putting Serial.flush(); before the deepsleep.
    – Gerben
    Jan 22 '20 at 15:41
1

In the question the OP said:

I have soldered a wire from GPIO16 to RST.

But, I'm going to post an answer for the people who find this question and need the ESP-01 modification instructions in order to wake an ESP-01 module up from deep-sleep:

The ESP8266 mounted on an ESP-01 board will not wake up from deep-sleep because the ESP8266 wake up signal is not connected to the reset pin of the ESP8266 on an ESP-01 board. You need to add this connection which can be difficult for most people. You can see the modification in this picture: enter image description here ...which comes from this instructables posting.

If this is too difficult, consider using a different ESP8266 board which brings these ESP8266 pins to board edge connectors.

0

I guess you have a race condition between automatic connection to WiFi by the Espressif SDK and your WiFi.begin(). You have two options how to solve it.

1. option: Don't use WiFi.begin in this sketch. Use an utility sketch to set the SSID and password and the SDK remembers it persistent on flash. Then it is able to connect fast to WiFi specially on reset, because it has the WiFi channel number in RTC memory which is not cleared at reset (only on power down).

2. option: turn of remembering of the SSID and password and the automatic connection at start. Then the esp8266 will connect to the WiFi only with your WiFi.begin call. The commands are WiFi.persistent(false) and WiFi.setAutoConnect(false).

You can turn of the SoftAP too. At default it starts automatically. The command is WiFi.softAPdisconnect()

2
  • I don't think the issue has to do with WiFi at all. I have completely stripped down the code to just have it go into Deep Sleep and I get the same behavior.
    – Brian
    Jan 22 '20 at 18:46
  • @Brian, try setAutoConnect(false). the automatic connection starts before the sketch
    – Juraj
    Jan 23 '20 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.