Skip to content

ESP.deepsleep losing time #3867

@YaddyVirus

Description

@YaddyVirus

Basic Infos

Hardware

Hardware: NodeMCU 1.0
Core Version: ?2.1.0-rc2?

Description

What you see below is the screenshot of my NodeMCU writing temperature and light intensity to a MySQL database and going to deep sleep in between. I've programmed it to read the sensors and send the data to the table and goto deep sleep, then repeat the cycle every 60 minutes.

As you can see in the LastUpdate column, each update is 2-3 minutes earlier than the previous one. Why is this happening and how do I ensure that the readings are every 60 minutes only because as you can see the updates are about 41 minutes early already.

I've written my deep sleep command like this - ESP.deepSleep(60000000*60);

Settings in IDE

Module: NodeMCU 1.0 (ESP12E Module)
Flash Size: 4MB(3M SPIFFS)
CPU Frequency: 80Mhz
Flash Frequency: 40Mhz
Upload Using: SERIAL

Sketch

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <DHT.h>

/* DHT AND LDR SETUP */

#define DHTTYPE DHT11
#define DHTPIN  0

const int LDR = A0;
int input_val = 0;

/* WIFI SETUP */

#define WLAN_SSID       "ssid"
#define WLAN_PASS       "Password"

/* ADAFRUIT IO SETUP */

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   //8883 for SSL
#define AIO_USERNAME    "YaddyVirus"      
#define AIO_KEY         "AIO_KEY"

DHT dht(DHTPIN, DHTTYPE); 
 
float temp_f;  // Values read from sensor
String webString="";     // String to display
unsigned long previousMillis = 0;        // will store last temp was read
const long interval = 2300;              // interval at which to read sensor

/************ Global State******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Store the MQTT server, username, and password in flash memory.

const char MQTT_SERVER[]     = AIO_SERVER;
const char MQTT_USERNAME[]   = AIO_USERNAME;
const char MQTT_PASSWORD[]   = AIO_KEY;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

/************************* Feeds ***************************************/
const char TEMP_FEED[] = AIO_USERNAME "/feeds/data";
const char LIGHT_FEED[] = AIO_USERNAME "/feeds/light";
Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, TEMP_FEED);
Adafruit_MQTT_Publish light = Adafruit_MQTT_Publish(&mqtt, LIGHT_FEED);


/********************** Sketch Code ************************************/
void MQTT_connect();


void setup() {
  Serial.begin(115200);
  delay(10);

  dht.begin();           // initialize temperature sensor

  Serial.println(F("MQTT Temp"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  temp_f = dht.readTemperature();
  input_val = analogRead(LDR);
  Serial.println();
  Serial.print("Initial Temp: ");
  Serial.println(temp_f);
  Serial.println();
  Serial.print("Initial Light:");
  Serial.println(input_val);
  Serial.println();


  // Loop begins

    // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).
  MQTT_connect();

  HTTPClient http;
  http.begin("http://203.185.191.35/test3.php");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");  //Specify content-type header
  int httpCode = http.POST("Sensor_ID=MBoxTest_1&temp="+String(temp_f)+"&light="+String(input_val));
  String payload = http.getString();
  Serial.println(httpCode);
  Serial.println(payload);
  http.end();
                             
  // Publishing now...
    temp_f = dht.readTemperature();
    input_val = analogRead(LDR);
    Serial.print(F("\nSending temp: "));
    Serial.print(temp_f);
    Serial.print("...");
    
    Serial.print(F("\nSending Light: "));
    Serial.print(input_val);
    Serial.print("...");
    if (! temp.publish(temp_f) || ! light.publish(input_val)) {
      Serial.println(F("Failed"));
    } else {
      Serial.println(F("Sent!"));
    }
  
  // ping the server to keep the mqtt connection alive
  // NOT required if publishing once every KEEPALIVE seconds

  if(! mqtt.ping()) {
    mqtt.disconnect();
  }

  ESP.deepSleep(60000000*60);
}


void loop() {

}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care of connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);
       retries--;
       if (retries == 0) {
         // basically die
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

Debug Messages

messages here

sup

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions