Nodemcu Esp8266 - How to send Time Date to channel


#1

Hi im trying to make A Beer fermentation chamber Temperature controller, Using Node mcu And thermistor. I try to make device Get the chambers temperature and Write Timestamp of the Time when the device got the Data. I got time from NTP server and got Hour-Minute-Second seperatally ( actually ican get data like 01.01.1970) But im having issues to send timestamp information to Virtual Channel

#include <Arduino.h>
#include <CountUpDownTimer.h>
#include <time.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

//Timestamp settings
int timezone = 3;
int dst = 0;

const double VCC = 3.3; // NodeMCU on board 3.3v vcc
const double R2 = 10000; // 10k ohm series resistor
const double adc_resolution = 1023; // 10-bit adc

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

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

unsigned long lastMillis = 0;

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

//Countdown settings
T.SetTimer(0,30,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();

configTime(timezone * 3600, dst * 0, “tr.pool.ntp.org”, “time.nist.gov”);
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
Serial.println("");

}
void loop() {
Cayenne.loop();
T.Timer();
//time stamp settings
time_t now;
struct tm * timeinfo;
time(&now);
timeinfo = localtime(&now);
//Serial.println(timeinfo->tm_hour);
//Serial.println(timeinfo->tm_min);
//Serial.println(timeinfo->tm_sec);
//Serial.println(timeinfo->tm_mday);
//Serial.println(timeinfo->tm_mon);
//Serial.println(timeinfo->tm_year);
float timefloat = timeinfo->tm_hour*100 + timeinfo->tm_min + timeinfo->tm_sec/100.0;

double Vout, Rth, temperature, adc_value;

adc_value = analogRead(A0);
Vout = (adc_value * VCC) / adc_resolution;
Rth = (VCC * R2 / Vout) - R2;

/* Steinhart-Hart Thermistor Equation:

  • Temperature in Kelvin = 1 / (A + B[ln®] + C[ln®]^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, timefloat, “counter”, “null”);

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) {
Cayenne.virtualWrite(1, temperature, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(2, timefloat, “counter”, “null”);
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);

}

wit this code i could do like

time is 03.08 now and i want to send this data like 03:09:00 To cayenne

Thanks


#2

you can try to combine hour min and sec, and send them on the counter widget like this https://cayenne.mydevices.com/shared/5b262aa7e2e42961900d852c
i have used a RTC and it at IST timezone. Time updates every 10 sec.


#3

I did like what you said

float timefloat = timeinfo->tm_hour*100 + timeinfo->tm_min + timeinfo->tm_sec/100.0;

Cayenne.virtualWrite(2, timefloat, “counter”, “null”);

time is now 13.50 - the float gives me result like 1350.180

but the counter looks like that

The data looks like here. Counter does not Write time like 13:50:18 or 13.50.18

Would you please tell me how to do it

maybe i need to change float i just copy paste it from some forum dont know how to use like for example what for tm_hour*100 is *100 in this case or what for tm_sec/100.0 / 100.0

thanks


#4

Try using long instead of float .
Long hr, min, sec;
Long time = hr*10000 + min *100 +sec;
And you want get hr: min:sec , you will only get it as continuos number.


#5

i will give a try, can you please say me what for do we use *1000 *100 in Long and

in float case

float timefloat = timeinfo->tm_hour*100 + timeinfo->tm_min + timeinfo->tm_sec/100.0;

*100 - /100.
im just new at arduino and want to learn

thanks


#6

have a look at this https://playground.arduino.cc/Code/DatatypePractices

Long time = hr*10000 + min *100 +sec;

example: current time is 3:45:06pm. so my hr = 3, min = 45, sec = 06. when i put this values in above equation, i get the time as “34506”.