So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Paolo Cinaglia
#70814
rudy wrote:@ Paolo

As long as the PCF8523 has power it should be working. Maybe the problem is with your code rather than the part.

Get this sketch working https://github.com/adafruit/RTClib/blob ... cf8523.ino

Set the clock, then reload the program without the clock setting function enabled. After that you should be able to turn off the power, turn the power back on, read the clock, and the time should be correct. As long as the clock chip has power from the line powered supply, or power from the battery, the clock should keep running.


Rudy,

the code for the clock works well (i try every single component when it arrives to me). I have this code for my datalogger

Code: Select all// Libraries
#include <ESP8266WiFi.h>
#include <RTClib.h>
#include "Adafruit_SHT31.h"
#include "Adafruit_TSL2591.h"
#include <Adafruit_Sensor.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Wire.h>
#include <Time.h>
#include <TimeAlarms.h>

// WiFi parameters
#define WLAN_SSID       "XXXX"
#define WLAN_PASS       "XXXX"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "XXXX"
#define AIO_KEY         "XXXX"

//Dichiaro il pin analogico
/*#define analogPin 12*/

Adafruit_SHT31 sht;
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
RTC_PCF8523 rtc;

void configureSensor (void) {
  tsl.setGain(TSL2591_GAIN_MED);
  tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);
}

const int sleepTimeS = 0; //Sleep forever

// Functions
void connect();

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/

// Setup feeds for temperature & humidity
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperatura");

Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");

/*Adafruit_MQTT_Publish ultra = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/ultra");*/

Adafruit_MQTT_Publish lux = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/lux");

/*************************** Sketch Code ************************************/

void setup()
{
 
  // Init sensor
  sht.begin();
  tsl.begin();
  Serial.begin(115200);
 
  if (! rtc.begin()) {
   
    Serial.println("Couldn't find RTC");
    while (1);
 
  }

  if (! rtc.initialized()) {
    Serial.println("RTC is NOT running!");
    // 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));
 
  }
 
  rtc.writeSqwPinMode(PCF8523_OFF);
  configureSensor();

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connessione a "));
  Serial.println(WLAN_SSID);

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

  }

  Serial.println();
  Serial.println(F("WiFi connesso"));
  Serial.println(F("Indirizzo IP: "));
  Serial.println(WiFi.localIP());

  // connect to adafruit io
  connect();

  Alarm.timerRepeat(3600, Repeats);
}

float luce() {

  sensors_event_t event;
  tsl.getEvent(&event);


  if ((event.light == 0) |
      (event.light > 4294966000.0) |
      (event.light < -4294966000.0))
  {

    Serial.println(F("Invalid data (adjust gain or timing)"));
  }

  else
 
  {
    return (event.light);
  }
}

void  loop() {

  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display

}

// functions to be called when an alarm triggers:
void Repeats() {

  // ping adafruit io a few times to make sure we remain connected
  if (! mqtt.ping(3)) {
   
    // reconnect to adafruit io
    if (! mqtt.connected())
      connect();
 
  }

  Serial.println("10 second timer");
  rtc.writeSqwPinMode(PCF8523_SquareWave32HZ);
  float humidity_data = (float)sht.readHumidity();
  Serial.print(humidity_data);
  Serial.println(" %");
  float temperature_data = (float)sht.readTemperature();
  Serial.print(temperature_data);
  Serial.println(" °C");
  /* float mV = analogRead(analogPin);
    float zz = mV / 4.3;
    float uvin_data = zz * 9;
    Serial.println(uvin_data);
    delay(1000);*/
  float lux_data = luce();
  Serial.print(lux_data);
  Serial.println(" Lux");

  // Publish data
  if (! temperature.publish(temperature_data))
    Serial.println(F("Pubblicazione temperatura fallita"));
  else
    Serial.println(F("Temperatura pubblicata!"));

  if (! humidity.publish(humidity_data))
    Serial.println(F("Pubblicazione umidità fallita"));
  else
    Serial.println(F("Umidità pubblicata!"));

  /*if (! ultra.publish(uvin_data))
    Serial.println(F("Pubblicazione uv fallita"));
    else
    Serial.println(F("UV Pubblicati"));*/

  if (! lux.publish(lux_data))
    Serial.print(F("Pubblicazione Lux fallita"));
  else
    Serial.println(F("Lux pubblicati"));

  rtc.writeSqwPinMode(PCF8523_OFF);

}

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connessione ad Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
     
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
   
    }

    if (ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Riprovando a connettersi..."));
    delay(10000);

  }

  Serial.println(F("Adafruit IO Connessa!"));

}

void digitalClockDisplay()
{
 
  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  Serial.println();

}


(Some parts are in italian; the parts commented out are for testing)

In this case, i can upload all well. If i use this code without che deep sleep it works really well (i've used last week at home and it worked perfect). When I use the deep sleep, obviously, nothing works because the CPU put everything in deep sleep and stops the alarm function and everything else...
User avatar
By rudy
#70815 Forget about deep sleep. Just run the code. Do a serial print of the time from the RTC. Then hold down the reset line for two minutes. When the ESP starts and gets the time from the RTC the time should be correct.

Deep sleep only stops the ESP, not the RTC. If it doesn't work then there is only one thing that you can do. Read the data sheet of the RTC.
User avatar
By Paolo Cinaglia
#70816
rudy wrote:Forget about deep sleep. Just run the code. Do a serial print of the time from the RTC. Then hold down the reset line for two minutes. When the ESP starts and gets the time from the RTC the time should be correct.

Deep sleep only stops the ESP, not the RTC. If it doesn't work then there is only one thing that you can do. Read the data sheet of the RTC.


I will try tomorrow to do this
User avatar
By rudy
#70818 A RTC chip can exist on it's own. It is designed to keep working when the rest of the system is turned off. As long as it has power, and a working crystal circuit, it should continue working until the battery power is exhausted.

If you have confirmed that the clock is working, incrementing time, then it should continue working when the ESP is reset, in a sleep state, or just powered off. None of those conditions should affect the RTC.

The PCF8523 has a battery backup input pin and battery switch-over circuit. The battery
switch-over circuit monitors the main power supply and switches automatically to the
backup battery when a power failure condition is detected. Accurate timekeeping is
maintained even when the main power supply is interrupted.