-->
Page 1 of 1

Getting DeepSleep working properly.

PostPosted: Mon Dec 24, 2018 10:31 pm
by MegaTux
Hi,

I have the following code running on a HUZZAH ESP 8266. The code runs well and logs T/H on my server but when I insert the following line of code
Code: Select allESP.deepSleep(sleepTimeS * 1000000);
in the void setup (), the sketch stops loading data to my server but the sleep mode seems to work (I have a amp meter connected, plus I see the ligts going off every 20 secs as expected. Where do you think the line ESP.deepSleep shoudl go?

Even if I put the line of code in the
Code: Select allvoid loop()
or
Code: Select allvoid gettemperature()
, I get the same effect.

Any clues?

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 = "my-PASS";

/**************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.52";

// 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);

      // Give the server some time to recieve the data and store it. I used 120 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
    // 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, 181)) {
    client.print("GET /html/emoncms/input/post?"); // This PHP script that is run on the zeus Apache 2.0 server and that posts the data to the MySQL database called "domotics".
    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); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print(",");
    client.print("\"humidity\":");
    client.print(humidity);
    client.print("}");
    client.print("&");
    client.print("apikey=c815g52de81c97ee9405fcef9d10c0ed");
   
    //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;
    }
  }
}