Reading Data from Device in every 30 minutes

Hi,
i am getting temperature and humidity info with my 2 devices for test. Nodemcu and other one is Arduino uno+esp8266 (i will choose the most suitable one)

My problem is i want to get information from device for example every 30 minutes or each hour. How can i do it. As i know its not a good way to do it with delay. Would you share a simple code how can i do longer delays in loop…

P.S : Im connecting cayenne with Wifi (nodemcu or arduino+esp8266) long wait time may cause that the device goes offlie from mqtt server?

Thanks

``````  if (millis() - lastMillis > 10000) {
lastMillis = millis();
// this send information to cayenne every 10 seconds,
// so do the necessary calculation for 30 min.
}
``````

in this case my code should be looking like…

``````void loop() {
Cayenne.loop();

Rth = (VCC * R2 / Vout) - R2;

/*  Steinhart-Hart Thermistor Equation:
*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3))));   // Temperature in kelvin

temperature = temperature - 273.15;
if (millis() - lastMillis > 1800000) {
lastMillis = millis();
Cayenne.virtualWrite(1, temperature, TYPE_TEMPERATURE, UNIT_CELSIUS);
// this send information to cayenne every 10 seconds,
// so do the necessary calculation for 30 min.
}
//30 minutes = 1800000 miliseconds
//delay(1800000);

}
``````

like that or how to correct it?
p.s Delay(90

Thank you very much

that should work

1 Like

Thank you it works Well…
is it possible that i can write How many minutes Ago was Last wirtual write or how many minutes left until the next virtual write in minutes…

i researched on google and the videos let me try to write Cayenne.virtualWrite(0, millis()/1000); something like that but does not work…

try this below code:

``````unsigned long millisLeft = 0;
unsigned long lastMillis = 0;
unsigned long millisSend = 0;
void loop() {
Cayenne.loop();

if ( millis() - millisSend > 60000)
{
millisSend = millis();
Cayenne.virtualWrite(6, temperature, TYPE_TEMPERATURE, UNIT_CELSIUS);
}

millisLeft = millis() - millisSend;
if ( millis() - lastMillis > 10000)
{
lastMillis = millis();
if ( millisLeft < 60000)
{
millisLeft = millisLeft / 1000;
Cayenne.virtualWrite(5, millisLeft, "counter", "null");

}
}
}
``````
1 Like

I Appreciate…
you help a lot i will give a try this code.

Reading sensor works well but each 30 minute sends duplicated data when i check data section. ıt does not make problem for workflow but wonder Why sends duplicated…

can you share the entire code you are using currently.

1 Like
``````// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

const double VCC = 3.3;             // NodeMCU on board 3.3v vcc
const double R2 = 10000;            // 10k ohm series resistor

const double A = 0.001129148;   // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741;
// WiFi network info.
char ssid[] = "xxx";

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

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
}
void loop() {
Cayenne.loop();

Rth = (VCC * R2 / Vout) - R2;

/*  Steinhart-Hart Thermistor Equation:
*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3))));   // Temperature in kelvin

temperature = temperature - 273.15;
if (millis() - lastMillis > 1800000) {
lastMillis = millis();
Cayenne.virtualWrite(1, temperature, TYPE_TEMPERATURE, UNIT_CELSIUS);
// 1 minutes 60000
//10 minutes 600000
//30 minutes = 1800000
//60 minutes 3600000
}

} // loop bitis

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{

// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);

}
``````

code looks fine. can you try some debug?
`#define CAYENNE_DEBUG` add this line in your code. change time from 30 minutes to 10 seconds. Open your Serial monitor and check the output whether it is sending data two times or paste the serial monitor output here.

1 Like

– Everything looks fine if i set timer 10 seconds intresting…

⸮txyH⸮⸮⸮D:hų⸮[557] Connecting to DSL-6740U
[3559] Connected to WiFi
[3559] IP: 192.168.1.3
[3560] Connecting to mqtt.mydevices.com:1883
[3984] Connected
[4146] Publish: topic 4, channel 65534, value NodeMCU, subkey , key
[4147] Publish: topic 6, channel 65534, value Xtensa32, subkey , key
[4160] Publish: topic 7, channel 65534, value 80000000, subkey , key
[4234] Publish: topic 5, channel 65534, value 1.2.0, subkey , key
[4305] Publish: topic 8, channel 65534, value ESP8266, subkey , key
[6385] Publish: topic 1, channel 0, value 6385, subkey , key
[10401] Publish: topic 1, channel 1, value 29.576, subkey c, key temp
[15420] Connection ok
[20444] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[21452] Publish: topic 1, channel 0, value 21452, subkey , key
[25475] Connection ok
[30502] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[35536] Connection ok
[36540] Publish: topic 1, channel 0, value 36540, subkey , key
[40565] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[45592] Connection ok
[50620] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[51630] Publish: topic 1, channel 0, value 51629, subkey , key
[55656] Connection ok
[60681] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[65713] Connection ok
[66718] Publish: topic 1, channel 0, value 66718, subkey , key
[70745] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[75772] Connection ok
[80799] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[81808] Publish: topic 1, channel 0, value 81807, subkey , key
[85834] Connection ok
[90860] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[95887] Connection ok
[96894] Publish: topic 1, channel 0, value 96893, subkey , key
[100917] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[105950] Connection ok
[110979] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[111988] Publish: topic 1, channel 0, value 111988, subkey , key
[116012] Connection ok
[121037] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[126069] Connection ok
[127075] Publish: topic 1, channel 0, value 127075, subkey , key
[131103] Publish: topic 1, channel 1, value 29.761, subkey c, key temp
[136124] Connection ok
[141150] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[142158] Publish: topic 1, channel 0, value 142157, subkey , key
[146181] Connection ok
[151208] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[156232] Connection ok
[157238] Publish: topic 1, channel 0, value 157238, subkey , key
[161261] Publish: topic 1, channel 1, value 30.133, subkey c, key temp
[166292] Connection ok
[171317] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[172326] Publish: topic 1, channel 0, value 172325, subkey , key
[176353] Connection ok
[181379] Publish: topic 1, channel 1, value 29.761, subkey c, key temp
[186402] Connection ok
[187408] Publish: topic 1, channel 0, value 187408, subkey , key
[191437] Publish: topic 1, channel 1, value 30.226, subkey c, key temp
[196467] Connection ok
[201494] Publish: topic 1, channel 1, value 29.761, subkey c, key temp
[202503] Publish: topic 1, channel 0, value 202503, subkey , key
[206531] Connection ok

This code works well too, Channel Five Counts like : 10- 20-30-40-50 how man seconds elplased until last Read… Is it possible to do it in minutes

millisLeft = millisLeft / 1000 * 60 ; something like that because in my Code i will read every 30 minutes or 1 hours.

ı know im asking very much questions but this topic is like millis() lesson for community.

Thanks a lot

can you try same debug with 30 minutes and check the output? Also, check the data on the dashboard.

give this a try,do it on your own, this will help you learn. if it does not work then I will help.

1 Like

Almost DONE!!
actually i decided to use countdown library, The code works well But it sends data to cayenne Every Second its not goot for the battery life and Server load. Instead of Live count down im Trying to Write Time when Tempareture have been read. Does cayenne have something like Write.Time function or should i use another library for know time and send data to Cayenne dashboard. Here is the working code now.

``````#include <Arduino.h>
#include <CountUpDownTimer.h>
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

//Countdown Settings
CountUpDownTimer T(DOWN, HIGH); // DOWN / UP

const double VCC = 3.3;             // NodeMCU on board 3.3v vcc
const double R2 = 10000;            // 10k ohm series resistor

const double A = 0.001129148;   // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741;
// WiFi network info.
char ssid[] = "xxx;

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

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);

//Countdown settings
T.SetTimer(0,1,0);   H-M-S  //start at 1 minute (USE FOR: DOWN ONLY)
// T.SetStopTime(0,0,30); // stop at 10 seconds (USE FOR: UP/DOWN)
T.StartTimer();

}
void loop() {
Cayenne.loop();
T.Timer();

Rth = (VCC * R2 / Vout) - R2;

/*  Steinhart-Hart Thermistor Equation:
*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3))));   // Temperature in kelvin

temperature = temperature - 273.15;

if (T.TimeHasChanged() ) // this prevents the time from being constantly shown.
{
Cayenne.virtualWrite(2, T.ShowSeconds(), "counter", "null"); // !!! SENDS DATA EVERY SECOND
Serial.print(T.ShowMinutes());
Serial.print(" - Timecheck:");
Serial.println(T.TimeCheck());

// This DOES NOT format the time to 0:0x when seconds is less than 10.
// if you need to format the time to standard format, use the sprintf() function.
if (T.TimeCheck() > 0) {
//IF Time is over send data to Cayenne dashboard
Cayenne.virtualWrite(1, temperature, TYPE_TEMPERATURE, UNIT_CELSIUS);
// I NEED A CODE HERE WRITE TIME STAMP TO Channel 5.....??
Serial.println(" - Sıcaklık Girildi ");
T.ResetTimer();
}
}

// 1 minutes 60000
//10 minutes 600000
//30 minutes = 1800000
//60 minutes 3600000

} // loop bitis

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{

// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);

}
``````

sending data fast will block your device Sending MQTT messages within rate limits

i am working on this using an RTC module and done with it. It will be out by next week. It is possible, give it a try with RTC module if you have.

1 Like

I tried to use NPT time than realised i have “RTC DS1302” module. with 5 pins Cant find the suitable library or circuit about wiring on internet. working on in since hours.

P.S: Countdown library works perfect with Cayenne i would suggest to other developers about timing…

The RTC what i have i think its an old version coz its hard to find suitable library and circuit for nodemcu

try this library Arduino Playground - DS1302RTC. i have DS3231 which has a good support.

1 Like

i tried RTC Virtuabotix Library Link here if you want to try

The output comes Seperated with RTC like “-RTC.hours - RTC.minutes -myRTC.seconds” Do i supposed to get each data for seperated Channel or do you have idea if there is a way to link them together and send with one channel.

Serial.print(“Current Date / Time: “);
Serial.print(myRTC.dayofmonth); //You can switch between day and month if you’re using American system
Serial.print(”/”);
Serial.print(myRTC.month);
Serial.print(“/”);
Serial.print(myRTC.year);
Serial.print(" “);
Serial.print(myRTC.hours);
Serial.print(”:“);
Serial.print(myRTC.minutes);
Serial.print(”:");
Serial.println(myRTC.seconds);

this is what i am doing.

` long time1 = hr * 10000 + min * 100 + sec;`

then send this to

``````Cayenne.virtualWrite(2, time1, "counter", "null");
``````
1 Like

in this case you get just HR wich supposed to be hour and let counter calculates the rest? if yes it could be good for countdown but would not work good for timestamp (when the device last Read Data from Device) on dashboard in my opinion…