-->
Page 1 of 1

Getting DeepSleep working properly.

PostPosted: Tue Dec 25, 2018 5:59 am
by MegaTux
Hi,

I have a HUZZAH ESP8266 logging a DTH sensor to Emon server. The sensors are on USB power for the moment but I want to get the ESPs on battery and to do that I need to save energy, so rely on the DeepSleep function. You'll see from the sketch below that i have inserted the
Code: Select allESP.deepSleep(sleepTimeS * 1000000);
line in the
Code: Select allvoid setup(void){ }
function. The unit does go to sleep and it does wake up correctly (I have pin 16 shorted to RST) as I have a multimeter attached and can see the power (mA) going to zero most of the time and peaking every 20 secs.

The problem is that its not logging the data to the Emon server as it appears to go to sleep before it logs the data. The output to serial port is not showing any useful code (of course not enough debug lines I suspect). It does show that its logging onto WiFI and then goes to sleep but when checking access log to Apache web server nothing is showing up (i.e. the sensors are definitely not logging data. As soon as you comment out the DeepSleep line, it logs data to the server. I'm missing something but not sure what. Any suggestions?

I've also put the DeeSleep call in the
Code: Select allvoid gettemperature() {
and in the
Code: Select allvoid setup(void)
but it still won't work

Thanks



Code: Select all/***************** LOAD LIBRARIES ********************************************/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>

/************************* DHT22 Sensor *************************************/
#define DHTTYPE DHT22
#define DHTPIN  5

/************************* WiFi Access Point Settings ***********************/
const char* ssid     = "MySSID";
const char* password = "MyPASS";

/**************Initialise the DHT sensor ***********************************/
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
 
float humidity, temperature;  // Values read from sensor
//String webString="";     // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        // will store last temp was read
const long interval = 2000;              // interval at which to read sensor
byte mac[6];                     // the MAC address of your Wifi shield

char server[] = "192.168.0.34";

// Time to sleep (in seconds):
const int sleepTimeS = 20;

WiFiClient client;

void setup(void)
{
  // You can open the Arduino IDE Serial Monitor window to see what the code is doing
  Serial.begin(115200);  // Serial connection from ESP-01 via 3.3v console cable
  delay(100);

  Serial.println(); // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Attempting to connect to WPA2 network ...");
  Serial.println(ssid);
 
  dht.begin();           // initialize temperature sensor

  WiFi.begin(ssid, password); // Connect to WiFi network
  Serial.print("\n\r \n\r Working to connect");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[0],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.println(mac[5],HEX);

    // Sleep
  Serial.println("ESP8266 in sleep mode");
  ESP.deepSleep(sleepTimeS * 1000000);
  //delay(5000);

}

void loop() {
 
  gettemperature(); // Read the sensor
 
  // Connect to the server (your local server on intranet) 
  if (client.connect(server, 193)) {
    client.print("GET /html/emoncms/input/post?"); // This is for emon
    client.print("node=THesp04");
    client.print("&");
    client.print("fulljson={");
    client.print("\"temperature\":"); // This is the temperature value to post to the database
    client.print(temperature); // Temp is posted
    client.print(",");
    client.print("\"humidity\":"); //Humidity is posted
    client.print(humidity);
    client.print("}");
    client.print("&");
    client.print("apikey=c815a12de82c97re9404fceh9b10c0ed");
   
    //client.println(" HTTP/1.1"); // Part of the GET request
    client.println(" "); //
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server

  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
}

void gettemperature() {
  // Wait at least 2 seconds seconds between measurements.
  // if the difference between the current time and last time you read
  // the sensor is bigger than the interval you set, read the sensor
  // Works better than delay for things happening elsewhere also
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you read the sensor
    previousMillis = currentMillis;   

    // Reading temperature for humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
    humidity = dht.readHumidity();          // Read humidity (percent)
    temperature = dht.readTemperature();     // Read temperature as Fahrenheit
    // Check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(temperature)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
  }

Re: Getting DeepSleep working properly.

PostPosted: Fri Dec 28, 2018 6:21 am
by davydnorris
You've got your deep sleep in the setup, so it's going to sleep before properly executing the loop.

Put your deep sleep in the loop code and test that you've transmitted correctly before executing that line.

Re: Getting DeepSleep working properly.

PostPosted: Sat Dec 29, 2018 2:16 pm
by Bonzo
I also had a problem with a Nodemcu where it was going to sleep before the data was fully sent. I resolved it by putting a delay(100) before the deep sleep command.