Nano 33 iot - RTC and BLE to update Alarm times

Hello All,
I am trying to update the Alarm times for my Nano 33 iot using BLE. I can get the BLE example to work but when I add in the RTC and sleep functionality, it breaks. I only need to be able to update the times when the nano is awake (not in Low Power mode). My Android phone can see the nano’s LED Service on nRF Connect, but it never actually connects to it.

Thanks,
Ian

/*
  Sleep RTC Alarm for Arduino nano 33 IOT
  with time set from a DS3231
*/
#include <ArduinoBLE.h>
#include <RTCZero.h>
#include <SPI.h>

/* Create an rtc object */
RTCZero rtc;
//RTC_DS3231 extrtc;

boolean awake = false;
boolean turnRelayOff = false;
const int relayPin = 2;     // use this pin to drive the Relays
const int ledPin = LED_BUILTIN; // pin to use for the LED
//const unsigned long timeON = 60000;
byte timeON = 6; // In hours
//unsigned long time_now = 0;
//unsigned long previousMs;
byte alarmHours = 13;
byte alarmMins = 31;
byte alarm2Hours = 00;
byte alarm2Mins = 35;

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 30;
const byte hours = 13;

/* Change these values to set the current initial date */
const byte day = 7;
const byte month = 6;
const byte year = 20;


//BLEService alarmService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE Hour Service
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE Alarm Hour Characteristic - custom 128-bit UUID, read and writable by central
//BLEIntCharacteristic hoursCharacteristic("19B10000-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
//BLEByteCharacteristic minsCharacteristic("3862A78D-13AF-4EC9-9615-D599F201617C", BLERead | BLEWrite | BLENotify);
//BLEByteCharacteristic timeONCharacteristic("3862A78D-13AF-4EC9-9615-D599F201617C", BLERead | BLEWrite | BLENotify);
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

//21600000;

void setAlarm () {
  rtc.setAlarmTime(alarmHours, alarmMins, 00);
  rtc.enableAlarm(rtc.MATCH_HHMMSS);
  rtc.attachInterrupt(alarmMatch);
}

void setAlarm2 () {
  rtc.setAlarmTime(alarm2Hours, alarm2Mins, 00);
  rtc.enableAlarm(rtc.MATCH_HHMMSS);
  rtc.attachInterrupt(alarm2Match);
}

void setup()
{
  Serial.begin(115200);
//  if (! extrtc.begin()) {
//    Serial.println("Couldn't find RTC");
//    while (1);
//  }
//  if (extrtc.lostPower()) {
//    Serial.println("RTC lost power, let's set the time!");
//    // When time needs to be set on a new device, or after a power loss, the
//    // following line sets the RTC to the date & time this sketch was compiled
//    extrtc.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));
//  }


  pinMode(ledPin, OUTPUT); // set LED pin to output mode
  digitalWrite(ledPin, LOW);
  pinMode(relayPin, OUTPUT); // set relay pin to output mode
  digitalWrite(relayPin, HIGH);

//  DateTime now = extrtc.now();

  rtc.begin();
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);
//  rtc.setTime(now.hour(), now.minute(), now.second()); //Set internal RTC from DS3231
//  rtc.setDate(now.day(), now.month(), now.year());

  setAlarm();

  rtc.standbyMode();
}

void loop()
{
  if (awake) {
    if (!BLE.begin()) {
      Serial.println("starting BLE failed!");

      while (1);
    }
    
    // set the local name peripheral advertises
    BLE.setLocalName("LED Sign");

    
    // set the UUID for the service this peripheral advertises:
    //  BLE.setAdvertisedService(alarmService);
  
    //  BLE.setAdvertisedService(ledService);
  
    // add the characteristics to the service
    //  alarmService.addCharacteristic(hoursCharacteristic);
    //  alarmService.addCharacteristic(minsCharacteristic);
    //  alarmService.addCharacteristic(timeONCharacteristic);
      ledService.addCharacteristic(switchCharacteristic);
  
    // add the service
    //  BLE.addService(alarmService);
    BLE.addService(ledService);
    switchCharacteristic.writeValue(0);
  
    //  hoursCharacteristic.writeValue(alarmHours);
    //  minsCharacteristic.writeValue(alarmMins);
    //  timeONCharacteristic.writeValue(timeON);
  
    // start advertising
    BLE.advertise();
    BLEDevice central = BLE.central();
    // poll for BLE events
    BLE.poll();
    if (central) {
      while (central.connected()) {
        digitalWrite(ledPin, LOW);
        delay(500);
        if (switchCharacteristic.written()) {
          if (switchCharacteristic.value()) {   // any value other than 0
            Serial.println("LED on");
            digitalWrite(ledPin, HIGH);         // will turn the LED on
          } else {                              // a 0 value
            Serial.println(F("LED off"));
            digitalWrite(ledPin, LOW);          // will turn the LED off
          }
        }
      } // end while central
    } // end if central
    Serial.begin(115200);
    Serial.println("I'm Awake");
    digitalWrite(relayPin, LOW);
    digitalWrite(ledPin, HIGH);
    awake = false;
  } // end of if awake
  Serial.println("Going to sleep");
  if (turnRelayOff) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(ledPin, LOW);
    turnRelayOff = false;
  } // end of turnRelayOff

rtc.standbyMode();    // Sleep until next alarm match
} // end of loop()

void alarmMatch()
{
  awake = true;
  setAlarm2();
}

void alarm2Match()
{
  setAlarm();
  turnRelayOff = true;
}

Greetings. I also have been having this issue. My troubleshooting leads me to believe that when the arduino calls the rtc chip (DS3231 in my case) this somehow interferes with the BLE. Unfortunately I have yet to find an answer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.