parent
838fac7108
commit
29438b7f61
@ -1,197 +1,138 @@ |
|||||||
// CONNECTIONS:
|
/**
|
||||||
// DS1307 SDA --> SDA
|
* |
||||||
// DS1307 SCL --> SCL
|
* HX711 library for Arduino - example file |
||||||
// DS1307 VCC --> 5v
|
* https://github.com/bogde/HX711
|
||||||
// DS1307 GND --> GND
|
* |
||||||
|
* MIT License |
||||||
|
* (c) 2018 Bogdan Necula |
||||||
|
* |
||||||
|
**/ |
||||||
|
|
||||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
#include "HX711.h" |
||||||
|
|
||||||
/* for software wire use below
|
|
||||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
|
||||||
#include <RtcDS3231.h> |
|
||||||
#include <EepromAT24C32.h> |
|
||||||
|
|
||||||
SoftwareWire myWire(SDA, SCL); |
// HX711 circuit wiring
|
||||||
RtcDS1307<SoftwareWire> Rtc(myWire); |
const int LOADCELL_DOUT_PIN = 21; |
||||||
for software wire use above |
const int LOADCELL_SCK_PIN = 20; |
||||||
|
|
||||||
|
|
||||||
|
HX711 scale; |
||||||
|
|
||||||
|
void setup() { |
||||||
|
Serial.begin(38400); |
||||||
|
Serial.println("HX711 Demo"); |
||||||
|
|
||||||
|
Serial.println("Initializing the scale"); |
||||||
|
|
||||||
|
// Initialize library with data output pin, clock input pin and gain factor.
|
||||||
|
// Channel selection is made by passing the appropriate gain:
|
||||||
|
// - With a gain factor of 64 or 128, channel A is selected
|
||||||
|
// - With a gain factor of 32, channel B is selected
|
||||||
|
// By omitting the gain factor parameter, the library
|
||||||
|
// default "128" (Channel A) is used here.
|
||||||
|
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); |
||||||
|
|
||||||
|
Serial.println("Before setting up the scale:"); |
||||||
|
Serial.print("read: \t\t"); |
||||||
|
Serial.println(scale.read()); // print a raw reading from the ADC
|
||||||
|
|
||||||
|
Serial.print("read average: \t\t"); |
||||||
|
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
|
||||||
|
|
||||||
|
Serial.print("get value: \t\t"); |
||||||
|
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
|
||||||
|
|
||||||
|
Serial.print("get units: \t\t"); |
||||||
|
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
|
||||||
|
// by the SCALE parameter (not set yet)
|
||||||
|
|
||||||
|
scale.set_scale(1000.f); // this value is obtained by calibrating the scale with known weights; see the README for details
|
||||||
|
scale.tare(); // reset the scale to 0
|
||||||
|
|
||||||
|
Serial.println("After setting up the scale:"); |
||||||
|
|
||||||
|
Serial.print("read: \t\t"); |
||||||
|
Serial.println(scale.read()); // print a raw reading from the ADC
|
||||||
|
|
||||||
|
Serial.print("read average: \t\t"); |
||||||
|
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
|
||||||
|
|
||||||
|
Serial.print("get value: \t\t"); |
||||||
|
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
|
||||||
|
|
||||||
for normal hardware wire use below */ |
Serial.print("get units: \t\t"); |
||||||
#include <Wire.h> // must be included here so that Arduino library object file references work |
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
|
||||||
|
// by the SCALE parameter set with set_scale
|
||||||
|
|
||||||
|
Serial.println("Readings:"); |
||||||
|
} |
||||||
|
|
||||||
|
void loop() { |
||||||
|
Serial.print("one reading:\t"); |
||||||
|
Serial.print(scale.get_units(), 1); |
||||||
|
Serial.print("\t| average:\t"); |
||||||
|
Serial.println(scale.get_units(10), 1); |
||||||
|
|
||||||
|
scale.power_down(); // put the ADC in sleep mode
|
||||||
|
delay(1000); |
||||||
|
scale.power_up(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* #include <Arduino.h>
|
||||||
|
#include <HX711.h> |
||||||
#include <RtcDS3231.h> |
#include <RtcDS3231.h> |
||||||
#include <EepromAT24C32.h> |
|
||||||
|
|
||||||
RtcDS3231<TwoWire> Rtc(Wire); |
|
||||||
EepromAt24c32<TwoWire> RtcEeprom(Wire); |
|
||||||
|
|
||||||
// if you have any of the address pins on the RTC soldered together
|
|
||||||
// then you need to provide the state of those pins, normally they
|
|
||||||
// are connected to vcc with a reading of 1, if soldered they are
|
|
||||||
// grounded with a reading of 0. The bits are in the order A2 A1 A0
|
|
||||||
// thus the following would have the A2 soldered together
|
|
||||||
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b011);
|
|
||||||
|
|
||||||
/* for normal hardware wire use above */ |
int RXLED = 17; // The RX LED has a defined Arduino pin
|
||||||
|
// Note: The TX LED was not so lucky, we'll need to use pre-defined
|
||||||
|
// macros (TXLED1, TXLED0) to control that.
|
||||||
|
// (We could use the same macros for the RX LED too -- RXLED1,
|
||||||
|
// and RXLED0.)
|
||||||
|
|
||||||
|
// 1. HX711 circuit wiring
|
||||||
|
const int LOADCELL_DOUT_PIN = 21; |
||||||
|
const int LOADCELL_SCK_PIN = 20; |
||||||
|
|
||||||
// nothing longer than 32 bytes
|
// 2. Adjustment settings
|
||||||
// rtc eeprom memory is 32 byte pages
|
const long LOADCELL_OFFSET = 50682624; |
||||||
// writing is limited to each page, so it will wrap at page
|
const long LOADCELL_DIVIDER = 5895655; |
||||||
// boundaries.
|
|
||||||
// But reading is only limited by the buffer in Wire class which
|
|
||||||
// by default is 32
|
|
||||||
const char data[] = "What time is it in Greenwich?"; |
|
||||||
const uint16_t stringAddr = 64; // stored on page boundary
|
|
||||||
|
|
||||||
void printDateTime(const RtcDateTime& dt); |
HX711 loadcell; |
||||||
|
|
||||||
void setup ()
|
void setup() |
||||||
{ |
{ |
||||||
Serial.begin(9600); |
pinMode(RXLED, OUTPUT); // Set RX LED as an output
|
||||||
|
// TX LED is set as an output behind the scenes
|
||||||
Serial.print("compiled: "); |
|
||||||
Serial.print(__DATE__); |
Serial.begin(9600); //This pipes to the serial monitor
|
||||||
Serial.println(__TIME__); |
Serial.println("Initialize Serial Monitor"); |
||||||
|
|
||||||
//--------RTC SETUP ------------
|
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
|
||||||
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
Serial1.println("Initialize Serial Hardware UART Pins"); |
||||||
// the available pins for SDA, SCL
|
|
||||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
// 3. Initialize library
|
||||||
|
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); |
||||||
Rtc.Begin(); |
loadcell.set_scale(LOADCELL_DIVIDER); |
||||||
RtcEeprom.Begin(); |
loadcell.set_offset(LOADCELL_OFFSET); |
||||||
|
|
||||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
|
||||||
printDateTime(compiled); |
|
||||||
Serial.println(); |
|
||||||
|
|
||||||
if (!Rtc.IsDateTimeValid())
|
|
||||||
{ |
|
||||||
if (Rtc.LastError() != 0) |
|
||||||
{ |
|
||||||
// we have a communications error
|
|
||||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
|
||||||
// what the number means
|
|
||||||
Serial.print("RTC communications error = "); |
|
||||||
Serial.println(Rtc.LastError()); |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
Serial.println("RTC lost confidence in the DateTime!"); |
|
||||||
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); |
|
||||||
} |
|
||||||
|
|
||||||
// never assume the Rtc was last configured by you, so
|
|
||||||
// just clear them to your needed state
|
|
||||||
Rtc.Enable32kHzPin(false); |
|
||||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
|
||||||
|
|
||||||
/* comment out on a second run to see that the info is stored long term */ |
|
||||||
// Store something in memory on the Eeprom
|
|
||||||
|
|
||||||
// store starting address of string
|
|
||||||
RtcEeprom.SetMemory(0, stringAddr);
|
|
||||||
// store the string, nothing longer than 32 bytes due to paging
|
|
||||||
uint8_t written = RtcEeprom.SetMemory(stringAddr, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add
|
|
||||||
// store the length of the string
|
|
||||||
RtcEeprom.SetMemory(1, written); // store the
|
|
||||||
/* end of comment out section */ |
|
||||||
} |
} |
||||||
|
|
||||||
void loop ()
|
void loop() |
||||||
{ |
{ |
||||||
if (!Rtc.IsDateTimeValid())
|
// Serial.println("Hello world!!!"); // Print "Hello World" to the Serial Monitor
|
||||||
{ |
// Serial1.println("Hello! Can anybody hear me?"); // Print "Hello!" over hardware UART
|
||||||
if (Rtc.LastError() != 0) |
|
||||||
{ |
|
||||||
// we have a communications error
|
|
||||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
|
|
||||||
// what the number means
|
|
||||||
Serial.print("RTC communications error = "); |
|
||||||
Serial.println(Rtc.LastError()); |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
// Common Causes:
|
|
||||||
// 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(); |
|
||||||
|
|
||||||
delay(5000); |
|
||||||
|
|
||||||
// read data
|
|
||||||
|
|
||||||
// get the offset we stored our data from address zero
|
|
||||||
uint8_t address = RtcEeprom.GetMemory(0); |
|
||||||
if (address != stringAddr) |
|
||||||
{ |
|
||||||
Serial.print("address didn't match "); |
|
||||||
Serial.println(address); |
|
||||||
} |
|
||||||
|
|
||||||
{ |
|
||||||
// get the size of the data from address 1
|
|
||||||
uint8_t count = RtcEeprom.GetMemory(1); |
|
||||||
uint8_t buff[64]; |
|
||||||
|
|
||||||
// get our data from the address with the given size
|
|
||||||
uint8_t gotten = RtcEeprom.GetMemory(address, buff, count); |
|
||||||
|
|
||||||
if (gotten != count || |
|
||||||
count != sizeof(data) - 1) // remove the extra null terminator strings add
|
|
||||||
{ |
|
||||||
Serial.print("something didn't match, count = "); |
|
||||||
Serial.print(count, DEC); |
|
||||||
Serial.print(", gotten = "); |
|
||||||
Serial.print(gotten, DEC); |
|
||||||
Serial.println(); |
|
||||||
} |
|
||||||
Serial.print("data read ("); |
|
||||||
Serial.print(gotten); |
|
||||||
Serial.print(") = \""); |
|
||||||
for (uint8_t ch = 0; ch < gotten; ch++) |
|
||||||
{ |
|
||||||
Serial.print((char)buff[ch]); |
|
||||||
} |
|
||||||
Serial.println("\""); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
delay(5000); |
|
||||||
} |
|
||||||
|
|
||||||
|
// 4. Acquire reading
|
||||||
|
Serial.print("Weight: "); |
||||||
|
Serial.println(loadcell.get_units(10), 2); |
||||||
|
|
||||||
|
digitalWrite(RXLED, LOW); // set the RX LED ON
|
||||||
|
TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF
|
||||||
|
delay(200); // wait for a second
|
||||||
|
|
||||||
void printDateTime(const RtcDateTime& dt) |
digitalWrite(RXLED, HIGH); // set the RX LED OFF
|
||||||
{ |
TXLED1; //TX LED macro to turn LED ON
|
||||||
char datestring[20]; |
delay(200); // wait for a second
|
||||||
|
} */ |
||||||
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); |
|
||||||
} |
|
@ -1,138 +0,0 @@ |
|||||||
/** |
|
||||||
* |
|
||||||
* HX711 library for Arduino - example file |
|
||||||
* https://github.com/bogde/HX711 |
|
||||||
* |
|
||||||
* MIT License |
|
||||||
* (c) 2018 Bogdan Necula |
|
||||||
* |
|
||||||
** |
|
||||||
|
|
||||||
#include "HX711.h" |
|
||||||
|
|
||||||
|
|
||||||
// HX711 circuit wiring |
|
||||||
const int LOADCELL_DOUT_PIN = 21; |
|
||||||
const int LOADCELL_SCK_PIN = 20; |
|
||||||
|
|
||||||
|
|
||||||
HX711 scale; |
|
||||||
|
|
||||||
void setup() { |
|
||||||
Serial.begin(38400); |
|
||||||
Serial.println("HX711 Demo"); |
|
||||||
|
|
||||||
Serial.println("Initializing the scale"); |
|
||||||
|
|
||||||
// Initialize library with data output pin, clock input pin and gain factor. |
|
||||||
// Channel selection is made by passing the appropriate gain: |
|
||||||
// - With a gain factor of 64 or 128, channel A is selected |
|
||||||
// - With a gain factor of 32, channel B is selected |
|
||||||
// By omitting the gain factor parameter, the library |
|
||||||
// default "128" (Channel A) is used here. |
|
||||||
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); |
|
||||||
|
|
||||||
Serial.println("Before setting up the scale:"); |
|
||||||
Serial.print("read: \t\t"); |
|
||||||
Serial.println(scale.read()); // print a raw reading from the ADC |
|
||||||
|
|
||||||
Serial.print("read average: \t\t"); |
|
||||||
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC |
|
||||||
|
|
||||||
Serial.print("get value: \t\t"); |
|
||||||
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet) |
|
||||||
|
|
||||||
Serial.print("get units: \t\t"); |
|
||||||
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided |
|
||||||
// by the SCALE parameter (not set yet) |
|
||||||
|
|
||||||
scale.set_scale(1000.f); // this value is obtained by calibrating the scale with known weights; see the README for details |
|
||||||
scale.tare(); // reset the scale to 0 |
|
||||||
|
|
||||||
Serial.println("After setting up the scale:"); |
|
||||||
|
|
||||||
Serial.print("read: \t\t"); |
|
||||||
Serial.println(scale.read()); // print a raw reading from the ADC |
|
||||||
|
|
||||||
Serial.print("read average: \t\t"); |
|
||||||
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC |
|
||||||
|
|
||||||
Serial.print("get value: \t\t"); |
|
||||||
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare() |
|
||||||
|
|
||||||
Serial.print("get units: \t\t"); |
|
||||||
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided |
|
||||||
// by the SCALE parameter set with set_scale |
|
||||||
|
|
||||||
Serial.println("Readings:"); |
|
||||||
} |
|
||||||
|
|
||||||
void loop() { |
|
||||||
Serial.print("one reading:\t"); |
|
||||||
Serial.print(scale.get_units(), 1); |
|
||||||
Serial.print("\t| average:\t"); |
|
||||||
Serial.println(scale.get_units(10), 1); |
|
||||||
|
|
||||||
scale.power_down(); // put the ADC in sleep mode |
|
||||||
delay(1000); |
|
||||||
scale.power_up(); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* #include <Arduino.h> |
|
||||||
#include <HX711.h> |
|
||||||
#include <RtcDS3231.h> |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int RXLED = 17; // The RX LED has a defined Arduino pin |
|
||||||
// Note: The TX LED was not so lucky, we'll need to use pre-defined |
|
||||||
// macros (TXLED1, TXLED0) to control that. |
|
||||||
// (We could use the same macros for the RX LED too -- RXLED1, |
|
||||||
// and RXLED0.) |
|
||||||
|
|
||||||
// 1. HX711 circuit wiring |
|
||||||
const int LOADCELL_DOUT_PIN = 21; |
|
||||||
const int LOADCELL_SCK_PIN = 20; |
|
||||||
|
|
||||||
// 2. Adjustment settings |
|
||||||
const long LOADCELL_OFFSET = 50682624; |
|
||||||
const long LOADCELL_DIVIDER = 5895655; |
|
||||||
|
|
||||||
HX711 loadcell; |
|
||||||
|
|
||||||
void setup() |
|
||||||
{ |
|
||||||
pinMode(RXLED, OUTPUT); // Set RX LED as an output |
|
||||||
// TX LED is set as an output behind the scenes |
|
||||||
|
|
||||||
Serial.begin(9600); //This pipes to the serial monitor |
|
||||||
Serial.println("Initialize Serial Monitor"); |
|
||||||
|
|
||||||
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board |
|
||||||
Serial1.println("Initialize Serial Hardware UART Pins"); |
|
||||||
|
|
||||||
// 3. Initialize library |
|
||||||
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); |
|
||||||
loadcell.set_scale(LOADCELL_DIVIDER); |
|
||||||
loadcell.set_offset(LOADCELL_OFFSET); |
|
||||||
} |
|
||||||
|
|
||||||
void loop() |
|
||||||
{ |
|
||||||
// Serial.println("Hello world!!!"); // Print "Hello World" to the Serial Monitor |
|
||||||
// Serial1.println("Hello! Can anybody hear me?"); // Print "Hello!" over hardware UART |
|
||||||
|
|
||||||
// 4. Acquire reading |
|
||||||
Serial.print("Weight: "); |
|
||||||
Serial.println(loadcell.get_units(10), 2); |
|
||||||
|
|
||||||
digitalWrite(RXLED, LOW); // set the RX LED ON |
|
||||||
TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF |
|
||||||
delay(200); // wait for a second |
|
||||||
|
|
||||||
digitalWrite(RXLED, HIGH); // set the RX LED OFF |
|
||||||
TXLED1; //TX LED macro to turn LED ON |
|
||||||
delay(200); // wait for a second |
|
||||||
} */ |
|
@ -0,0 +1,197 @@ |
|||||||
|
// CONNECTIONS: |
||||||
|
// DS1307 SDA --> SDA |
||||||
|
// DS1307 SCL --> SCL |
||||||
|
// DS1307 VCC --> 5v |
||||||
|
// DS1307 GND --> GND |
||||||
|
|
||||||
|
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||||
|
|
||||||
|
/* for software wire use below |
||||||
|
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
||||||
|
#include <RtcDS3231.h> |
||||||
|
#include <EepromAT24C32.h> |
||||||
|
|
||||||
|
SoftwareWire myWire(SDA, SCL); |
||||||
|
RtcDS1307<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> |
||||||
|
#include <EepromAT24C32.h> |
||||||
|
|
||||||
|
RtcDS3231<TwoWire> Rtc(Wire); |
||||||
|
EepromAt24c32<TwoWire> RtcEeprom(Wire); |
||||||
|
|
||||||
|
// if you have any of the address pins on the RTC soldered together |
||||||
|
// then you need to provide the state of those pins, normally they |
||||||
|
// are connected to vcc with a reading of 1, if soldered they are |
||||||
|
// grounded with a reading of 0. The bits are in the order A2 A1 A0 |
||||||
|
// thus the following would have the A2 soldered together |
||||||
|
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b011); |
||||||
|
|
||||||
|
/* for normal hardware wire use above */ |
||||||
|
|
||||||
|
// nothing longer than 32 bytes |
||||||
|
// rtc eeprom memory is 32 byte pages |
||||||
|
// writing is limited to each page, so it will wrap at page |
||||||
|
// boundaries. |
||||||
|
// But reading is only limited by the buffer in Wire class which |
||||||
|
// by default is 32 |
||||||
|
const char data[] = "What time is it in Greenwich?"; |
||||||
|
const uint16_t stringAddr = 64; // stored on page boundary |
||||||
|
|
||||||
|
void printDateTime(const RtcDateTime& dt); |
||||||
|
|
||||||
|
void setup () |
||||||
|
{ |
||||||
|
Serial.begin(9600); |
||||||
|
|
||||||
|
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(); |
||||||
|
RtcEeprom.Begin(); |
||||||
|
|
||||||
|
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||||
|
printDateTime(compiled); |
||||||
|
Serial.println(); |
||||||
|
|
||||||
|
if (!Rtc.IsDateTimeValid()) |
||||||
|
{ |
||||||
|
if (Rtc.LastError() != 0) |
||||||
|
{ |
||||||
|
// we have a communications error |
||||||
|
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||||
|
// what the number means |
||||||
|
Serial.print("RTC communications error = "); |
||||||
|
Serial.println(Rtc.LastError()); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Serial.println("RTC lost confidence in the DateTime!"); |
||||||
|
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); |
||||||
|
} |
||||||
|
|
||||||
|
// never assume the Rtc was last configured by you, so |
||||||
|
// just clear them to your needed state |
||||||
|
Rtc.Enable32kHzPin(false); |
||||||
|
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); |
||||||
|
|
||||||
|
/* comment out on a second run to see that the info is stored long term */ |
||||||
|
// Store something in memory on the Eeprom |
||||||
|
|
||||||
|
// store starting address of string |
||||||
|
RtcEeprom.SetMemory(0, stringAddr); |
||||||
|
// store the string, nothing longer than 32 bytes due to paging |
||||||
|
uint8_t written = RtcEeprom.SetMemory(stringAddr, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add |
||||||
|
// store the length of the string |
||||||
|
RtcEeprom.SetMemory(1, written); // store the |
||||||
|
/* end of comment out section */ |
||||||
|
} |
||||||
|
|
||||||
|
void loop () |
||||||
|
{ |
||||||
|
if (!Rtc.IsDateTimeValid()) |
||||||
|
{ |
||||||
|
if (Rtc.LastError() != 0) |
||||||
|
{ |
||||||
|
// we have a communications error |
||||||
|
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||||
|
// what the number means |
||||||
|
Serial.print("RTC communications error = "); |
||||||
|
Serial.println(Rtc.LastError()); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// Common Causes: |
||||||
|
// 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(); |
||||||
|
|
||||||
|
delay(5000); |
||||||
|
|
||||||
|
// read data |
||||||
|
|
||||||
|
// get the offset we stored our data from address zero |
||||||
|
uint8_t address = RtcEeprom.GetMemory(0); |
||||||
|
if (address != stringAddr) |
||||||
|
{ |
||||||
|
Serial.print("address didn't match "); |
||||||
|
Serial.println(address); |
||||||
|
} |
||||||
|
|
||||||
|
{ |
||||||
|
// get the size of the data from address 1 |
||||||
|
uint8_t count = RtcEeprom.GetMemory(1); |
||||||
|
uint8_t buff[64]; |
||||||
|
|
||||||
|
// get our data from the address with the given size |
||||||
|
uint8_t gotten = RtcEeprom.GetMemory(address, buff, count); |
||||||
|
|
||||||
|
if (gotten != count || |
||||||
|
count != sizeof(data) - 1) // remove the extra null terminator strings add |
||||||
|
{ |
||||||
|
Serial.print("something didn't match, count = "); |
||||||
|
Serial.print(count, DEC); |
||||||
|
Serial.print(", gotten = "); |
||||||
|
Serial.print(gotten, DEC); |
||||||
|
Serial.println(); |
||||||
|
} |
||||||
|
Serial.print("data read ("); |
||||||
|
Serial.print(gotten); |
||||||
|
Serial.print(") = \""); |
||||||
|
for (uint8_t ch = 0; ch < gotten; ch++) |
||||||
|
{ |
||||||
|
Serial.print((char)buff[ch]); |
||||||
|
} |
||||||
|
Serial.println("\""); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
delay(5000); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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); |
||||||
|
} |
Loading…
Reference in new issue