Ds3231 temperature display in Cayenne


#1

Using an ESP8266 (HUZZAH) with a DS3231 RTC. The temperature displays in the serial monitor but I’m unable to figure out how to get it to Cayenne. Tried every combination I could think of. Truly, I don’t have any idea of what I’m doing. Any pointers?
Thanks,
John

ERROR:
In function ‘void loop()’:
RTC_cayenne_min:93: error: cannot convert ‘RtcTemperature’ to ‘float’ in initialization
float currentTemp = (temp);
^
exit status 1
cannot convert ‘RtcTemperature’ to ‘float’ in initialization

Relevant code:

RtcTemperature temp = Rtc.GetTemperature();
Serial.print(temp.AsFloat());
Serial.println(“C”);
Serial.println();

// My stuff:
float currentTemp = (temp);
//float currentTemp = (96); // Test: this creates temperature widget with 96 in it.
Cayenne.celsiusWrite(1, currentTemp);

Full Code:

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

#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231 Rtc(Wire);

Usual Login Stuff:

unsigned long lastMillis = 0;

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

Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);

RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();

if (!Rtc.IsDateTimeValid()) 
{
    // Common Cuases:
    //    1) first time you ran and the device wasn't running yet
    //    2) the battery on the device is low or even missing

    Serial.println("RTC lost confidence in the DateTime!");

    // following line sets the RTC to the date & time this sketch was compiled
    // it will also reset the valid flag internally unless the Rtc device is
    // having an issue

    Rtc.SetDateTime(compiled);Rtc.Begin();
}

if (!Rtc.GetIsRunning())
{
    Serial.println("RTC was not actively running, starting now");
    Rtc.SetIsRunning(true);
}

RtcDateTime now = Rtc.GetDateTime();
if (now < compiled) 
{
    Serial.println("RTC is older than compile time!  (Updating DateTime)");
    Rtc.SetDateTime(compiled);
}
else if (now > compiled) 
{
    Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled) 
{
    Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}

// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); 

}

void loop() {
delay(15000);
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println(“RTC lost confidence in the DateTime!”);
}

RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();

RtcTemperature temp = Rtc.GetTemperature();
Serial.print(temp.AsFloat());
Serial.println("C");
Serial.println();

// My stuff:
float currentTemp = (temp);
// float currentTemp = (96); // Test: this creates temperature widget with 96 in it.
Cayenne.celsiusWrite(1, currentTemp);

}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
char datestring[20];

snprintf_P(datestring, 
        countof(datestring),
        PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
        dt.Month(),
        dt.Day(),
        dt.Year(),
        dt.Hour(),
        dt.Minute(),
        dt.Second() );
Serial.print(datestring);

}


#2

Hi @margettepv,

Did you add a widget using virtual pin 1 to your Cayenne dashboard?

~Benny


#3

Yup, got a widget as such. But I never got it to compile, except when using the test line.
John


#4

try this code and see if it works for you.

// CONNECTIONS:
// DS3231 SDA --> SDA
// DS3231 SCL --> SCL
// DS3231 VCC --> 3.3v or 5v
// DS3231 GND --> GND

/* for software wire use below
#include <SoftwareWire.h>  // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>

SoftwareWire myWire(SDA, SCL);
RtcDS3231<SoftwareWire> Rtc(myWire);
 for software wire use above */

/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// 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>

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

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

float x;
unsigned long lastMillis = 0;

void setup () 
{
    Serial.begin(9600);
    Cayenne.begin(username, password, clientID, ssid, wifiPassword);
    Serial.begin(57600);
    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);

    //--------RTC SETUP ------------
    // if you are using ESP-01 then uncomment the line below to reset the pins to
    // the available pins for SDA, SCL
    // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
    
    Rtc.Begin();

    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();

    if (!Rtc.IsDateTimeValid()) 
    {
        // Common Cuases:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing

        Serial.println("RTC lost confidence in the DateTime!");

        // following line sets the RTC to the date & time this sketch was compiled
        // it will also reset the valid flag internally unless the Rtc device is
        // having an issue

        Rtc.SetDateTime(compiled);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) 
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled) 
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }

    // never assume the Rtc was last configured by you, so
    // just clear them to your needed state
    Rtc.Enable32kHzPin(false);
    Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); 
}

void loop () 
{
    Cayenne.loop();

    if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
 Cayenne.celsiusWrite(1, x);
    }
    if (!Rtc.IsDateTimeValid()) 
    {
        // Common Cuases:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }

    RtcDateTime now = Rtc.GetDateTime();
    printDateTime(now);
    Serial.println();

  RtcTemperature temp = Rtc.GetTemperature();
  temp.Print(Serial);
  // you may also get the temperature as a float and print it
    x = temp.AsFloatDegC();
    Serial.println("C");

}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

#5

Not there yet. Compile error:

Arduino: 1.8.5 (Linux), Board: “Adafruit HUZZAH ESP8266, 80 MHz, 4M (3M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200”

/home/m/Arduino/sketch_feb16a/sketch_feb16a.ino: In function ‘void loop()’:
sketch_feb16a:122: error: ‘class RtcTemperature’ has no member named 'Print’
temp.Print(Serial);
^
sketch_feb16a:124: error: ‘class RtcTemperature’ has no member named ‘AsFloatDegC’
x = temp.AsFloatDegC();
^
exit status 1
’class RtcTemperature’ has no member named ‘Print’

Just to be clear, the code I posted was good other than where I tried to make it post to Cayenne.
Thanks shramiksalgaonkar.
John