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
Serial.flush();
before the deepsleep.