I'm trying to set an alarm to update sensor readings at the top of each hour. After lots of trial and error I finally got the RTC to work on my Nano 33 IOT. The sensor readings and everything else is working fine, but the code just seems to skip the alarm. I put a counter in the alarmMatch subroutine to increment each time the alarm is tripped, but so far I haven't seen it work.
Here's the code I'm working with:
/*
Sketch generated by the Arduino IoT Cloud Thing "Weather V3"
https://create.arduino.cc/cloud/things/710e1d97-e9ce-414d-893a-49f0166698b8
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String barState;
String rainAmount;
float humidity;
float maxTempF;
float minTempF;
float pressure;
float pressure_HG;
float rainAmt;
float rainAmtInches;
float rainVal;
float tempC;
float tempF;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
int alarmRun;
// Real Time Clock
#include <RTCZero.h>
//#include <WiFiNINA.h>
//#include <WiFiUdp.h>
const int GMT = -5; // Time zone constant - change as required for your location
// Create object
RTCZero rtc2;
/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 50;
const byte hours = 13;
/* Change these values to set the current initial date */
const byte day = 9;
const byte month = 3;
const byte year = 22;
// BMP280
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; // use I2C interface
float previousPressure;
int displayPressure;
// DHT11
#include <DHT.h>
#define Type DHT11
int sensePin=5;
DHT HT(sensePin,Type);
// Rain Sensor
int rainSense=A3;
int rainPresence=4;
int rainState;
// Tipping Bucket
int tipPin=9;
int tipSense;
int tipCount;
unsigned long button_time = 0;
unsigned long last_button_time = 0;
void setup()
{
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Start Real Time Clock
// Initialize RTC
rtc2.begin();
// Set the time
rtc2.setHours(hours-GMT);
rtc2.setMinutes(minutes);
rtc2.setSeconds(seconds);
// Set the date
rtc2.setDay(day);
rtc2.setMonth(month);
rtc2.setYear(year);
// Set alarm to update min, max and rain
rtc2.setAlarmTime(00, 00, 00);
rtc2.enableAlarm(rtc2.MATCH_MMSS);
rtc2.attachInterrupt(alarmMatch);
// Start DHT
HT.begin();
// Start BMP
unsigned status;
status = bmp.begin();
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
if (!status)
{
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
pinMode(rainSense,INPUT);
pinMode(tipPin,INPUT_PULLUP);
digitalWrite(tipPin,HIGH);
minTempF=70;
attachInterrupt(digitalPinToInterrupt(tipPin), tipBucket_ISR, CHANGE);
}
void loop()
{
ArduinoCloud.update();
// Read Sensors
// Get temp and humidity
humidity=HT.readHumidity();
humidity=round(humidity);
tempC=HT.readTemperature();
tempF=HT.readTemperature(true);
tempF=round(tempF);
if (tempF>maxTempF)
{
maxTempF=tempF;
}
if (tempF<=minTempF)
{
minTempF=tempF;
}
// Get Pressure
pressure=((bmp.readPressure()/100)+43.33);
displayPressure=round(pressure);
pressure_HG=(pressure*.0295);
//pressure_HG=round(pressure_HG);
//displayPressure=int(pressure);
if (pressure == previousPressure)
{
barState="Steady";
}
else if(pressure<previousPressure+5)
{
barState="Falling";
}
else if(pressure>previousPressure-5)
{
barState="Rising";
}
previousPressure=displayPressure;
// Get rain sense
rainVal=analogRead(rainSense);
rainState=digitalRead(rainPresence);
if (rainState==HIGH)
{
rainAmount="Dry";
}
else if(rainState==LOW)
{
rainAmount="Raining";
}
// Display sensor data on Serial Monitor
Serial.print("Max Temp ");
Serial.println(maxTempF);
if (tempF<=minTempF)
{
minTempF=tempF;
}
Serial.print("Min Temp ");
Serial.println(minTempF);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("temp C ");
Serial.println(tempC);
Serial.print("temp F ");
Serial.println(tempF);
Serial.println();
Serial.print("Previous Pressure ");
Serial.println(previousPressure);
Serial.print(F("Pressure = "));
Serial.print(displayPressure);
Serial.println(" hPa");
Serial.print("Pressure inches HG ");
Serial.println(pressure_HG);
Serial.println(barState);
Serial.println();
Serial.print("Rain/Dry");
Serial.println(rainAmount);
Serial.println();
Serial.print ("tipSense ");
Serial.println(tipSense);
Serial.print ("Tip Count = ");
Serial.println (tipCount);
rainAmt=tipCount*.2;
rainAmtInches=(rainAmt*.0394);
Serial.print("Rain Amount mm ");
Serial.println(rainAmt);
Serial.print("Rain Amount Inches ");
Serial.println(rainAmtInches);
Serial.println();
Serial.println(alarmRun);
Serial.println("Current Time ");
// Print date...
print2digits(rtc2.getDay());
Serial.print("/");
print2digits(rtc2.getMonth());
Serial.print("/");
print2digits(rtc2.getYear());
Serial.print(" ");
// ...and time
print2digits(rtc2.getHours());
Serial.print(":");
print2digits(rtc2.getMinutes());
Serial.print(":");
print2digits(rtc2.getSeconds());
Serial.println();
}
void print2digits(int number)
{
if (number < 10) {
Serial.print("0"); // print a 0 before if the number is < than 10
}
Serial.print(number);
}
// Get tipping bucket ISR
void tipBucket_ISR()
{
tipSense=digitalRead(tipPin);
button_time=millis();
if (button_time - last_button_time > 80)
{
if (tipSense==0)
{
tipCount=tipCount+1;
}
last_button_time=button_time;
}
}
void alarmMatch()
{
// reset Min Temp, Max Temp and Rain Amount
minTempF=minTempF;
maxTempF=maxTempF;
rainAmt=0;
tipCount=0;
int alarmRun=alarmRun+1;
}