Device goes offline-

hi @harunsivasli. I just made a prototype of your project and tested it out. it is based on @adam Battery Powered ESP8266 Temperature/Humidity Monitor with DHT11

Components: nodemcu, RTC DS3231, DTH11(you can replace this with the sensor you are using to read the temp).

code:

/*
  This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.

  The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

  Steps:
  1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
  2. Select your ESP8266 board from the Tools menu.
  3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  4. Set the network name and password.
  5. Compile and upload the sketch.
  6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;
unsigned long lastMillis_rtc;

long x;
long y;
long z;

//DTH
#include "DHT.h"
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define DTH_hum_Virtual_Channel 1
#define DTH_tempC_Virtual_Channel 3
#define DTH_tempF_Virtual_Channel 4
#define DTH_hic_Virtual_Channel 5
#define DTH_hif_Virtual_Channel 6
unsigned long lastMillis_dht = 0;

int SleepTime = 60000000;
bool CayenneSent = false;


void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  dht.begin();
  CayenneSent = false;
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop() {
  Cayenne.loop();
  if (millis() - lastMillis_rtc  > 10000) {
    lastMillis_rtc  = millis();
    DateTime now = rtc.now();
    x = now.hour();
    y = now.minute();
    z = now.second();
    long a = x * 10000 + y * 100 + z;
    Cayenne.virtualWrite(2, a, "counter", "null");

    //DTH
    // 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);
    Cayenne.virtualWrite(DTH_hum_Virtual_Channel, h, "rel_hum", "p");
    Cayenne.virtualWrite(DTH_tempC_Virtual_Channel, t, "temp", "c");
    Cayenne.virtualWrite(DTH_tempF_Virtual_Channel, f, "temp", "f");
    Cayenne.virtualWrite(DTH_hif_Virtual_Channel, hif, "temp", "f");
    Cayenne.virtualWrite(DTH_hic_Virtual_Channel, hic, "temp", "c");
    CayenneSent = true;
  }
  if (CayenneSent) {
    Serial.println("Sent all values - sleeping");
    delay(100);
    ESP.deepSleep(SleepTime, WAKE_RF_DEFAULT);
    delay(100);
    }

}

currently, the sleep time is set to 10 minutes, you can change to 30 minutes.
this code reads the current time from DS3231 and temp from DTH11. It sends data to cayenne and sleeps for 10 minutes. After 10 min it wakes up and sends data and time to cayenne.

Now if you want to check the timestamp during which the temp is sent, you can click on the data option on your dashboard and get it.

1 Like