Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
stevie135s
Published © GPL3+

Set a DS3231 RTC Over Bluetooth

An easy way to set a DS3231 RTC module connected to a device with BLE (esp32 perhaps) using an app on an Android phone.

BeginnerWork in progress1 hour341
Set a DS3231 RTC Over Bluetooth

Things used in this project

Story

Read more

Custom parts and enclosures

Android App

Install app on phone (may require permissions)

Schematics

Typical Circuit

Code

Code_Update_1

Arduino
Just a tidy up of the original code
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <DS3231.h>
#include <Wire.h>
#define SERVICE_UUID        "fe45c941-9b87-4ef1-ac31-990dd686f813"
#define CHARACTERISTIC_UUID "ebc2e890-8ad5-4d51-a4a1-0e60efc9df4a"

String BT_IN;
char inString[13];
bool century = false;
bool h12Flag;
bool pmFlag;
byte Year;
byte Month;
byte Date;
byte Dow;
byte Hour;
byte Minute;
byte Second,L_Second;
int counter;

String Days [8]= {"Null"," Monday"," Tuesday"," Wednesday"," Thursday"," Friday"," Saturday"," Sunday"};

BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
DS3231 myRTC;

void setup() {
  Serial.begin(57600);
  Wire.begin();
  Serial.println("Starting BLE Server!");   
  BLEDevice::init("ESP32-BLE-Server");
  pServer = BLEDevice::createServer();
  pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );
  pCharacteristic->setValue("");
  pService->start();
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();

  // Loop
  
    counter = 1;
    while(counter) {  
    BT_IN = pCharacteristic->getValue();
    if (BT_IN.length() == 15) {
    pCharacteristic->setValue("");
    inString[0]= BT_IN[0];  //Y
    inString[1]= BT_IN[1];  //Y
    inString[2]= BT_IN[2];  //M
    inString[3]= BT_IN[3];  //M
    inString[4]= BT_IN[4];  //D
    inString[5]= BT_IN[5];  //D
    inString[6]= BT_IN[6];  //w
    inString[7]= BT_IN[7];  //H
    inString[8]= BT_IN[8];  //H
    inString[9]= BT_IN[9];  //M
    inString[10]= BT_IN[10];//M
    inString[11]= BT_IN[11];//S
    inString[12]= BT_IN[12];//S

    Year = (((inString[0] -48)*10)+(inString[1] -48)); 
    // now month
    Month = (((inString[2] -48)*10)+(inString[3] -48));
    // now date
    Date = (((inString[4] -48)*10)+(inString[5] -48));
    // now Day of Week
    Dow = (inString[6] -48);
    // now hour
    Hour = (((inString[7] -48)*10)+(inString[8] -48));
    // now minute
    Minute = (((inString[9] -48)*10)+(inString[10] -48));
    // now second
    Second = (((inString[11] -48)*10)+(inString[12] -48));

    myRTC.setClockMode(false);// set to 24h 
    myRTC.setYear(Year);
    myRTC.setMonth(Month);
    myRTC.setDate(Date);
    myRTC.setDoW(Dow);
    myRTC.setHour(Hour);
    myRTC.setMinute(Minute);
    myRTC.setSecond(Second);
    myRTC.turnOffAlarm(1);
    myRTC.turnOffAlarm(2);
    Serial.println("The Time Has Been Set");
    delay(600);
    BLEDevice:: deinit(true);
    Serial.println("Bluetooth has been turned off");
    delay(600);             // Allow RTC registers to set before any read occurs
    
    counter = 0; 
    }
  }
}


void loop() { 
    if (myRTC.getSecond() != L_Second) {
    L_Second = myRTC.getSecond();
    Serial.print(myRTC.getDate());
    Serial.print("/");
    Serial.print(myRTC.getMonth(century));
    Serial.print("/");
    Serial.print("20");
    Serial.print(myRTC.getYear());
    Serial.print(" ");
    Serial.print(myRTC.getHour(h12Flag,pmFlag));
    Serial.print(":");
    Serial.print(myRTC.getMinute());
    Serial.print(":");
    Serial.print(myRTC.getSecond());
    Serial.print(" ");
    Serial.println(Days[myRTC.getDoW()]);
      }
  }

Code

Arduino
Compile using Arduino IDE
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <DS3231.h>
#include <Wire.h>
#define SERVICE_UUID        "fe45c941-9b87-4ef1-ac31-990dd686f813"
#define CHARACTERISTIC_UUID "ebc2e890-8ad5-4d51-a4a1-0e60efc9df4a"

String BT_IN;
char inString[13];
bool century = false;
bool h12Flag;
bool pmFlag;
byte Year;
byte Month;
byte Date;
byte Dow;
byte Hour;
byte Minute;
byte Second,L_Second;
byte counter;

String Days [8]= {"Null"," Monday"," Tuesday"," Wednesday"," Thursday"," Friday"," Saturday"," Sunday"};

BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
DS3231 myRTC;

void setup() {
  Serial.begin(57600);
  Wire.begin();
  Serial.println("Starting BLE Server!");   
  BLEDevice::init("ESP32-BLE-Server");
  pServer = BLEDevice::createServer();
  pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );
    pCharacteristic->setValue("");
  pService->start();
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
   
}


void loop() { 
    std::string value = pCharacteristic->getValue();
    if (value.length() == 15) {
    BT_IN = (value.c_str());
    pCharacteristic->setValue("");

    inString[0]= BT_IN[0];  //Y
    inString[1]= BT_IN[1];  //Y
    inString[2]= BT_IN[2];  //M
    inString[3]= BT_IN[3];  //M
    inString[4]= BT_IN[4];  //D
    inString[5]= BT_IN[5];  //D
    inString[6]= BT_IN[6];  //w
    inString[7]= BT_IN[7];  //H
    inString[8]= BT_IN[8];  //H
    inString[9]= BT_IN[9];  //M
    inString[10]= BT_IN[10];//M
    inString[11]= BT_IN[11];//S
    inString[12]= BT_IN[12];//S

    Year = (((inString[0] -48)*10)+(inString[1] -48)); 
    // now month
    Month = (((inString[2] -48)*10)+(inString[3] -48));
    // now date
    Date = (((inString[4] -48)*10)+(inString[5] -48));
    // now Day of Week
    Dow = (inString[6] -48);
    // now hour
    Hour = (((inString[7] -48)*10)+(inString[8] -48));
    // now minute
    Minute = (((inString[9] -48)*10)+(inString[10] -48));
    // now second
    Second = (((inString[11] -48)*10)+(inString[12] -48));

    myRTC.setClockMode(false);// set to 24h 
    myRTC.setYear(Year);
    myRTC.setMonth(Month);
    myRTC.setDate(Date);
    myRTC.setDoW(Dow);
    myRTC.setHour(Hour);
    myRTC.setMinute(Minute);
    myRTC.setSecond(Second);
    myRTC.turnOffAlarm(1);
    myRTC.turnOffAlarm(2);

    delay(1200);             // Allow RTC registers to set before any read occurs
    
    Serial.print("The Time Has Been Set :");
    show_time(); 
    }
  }

  void show_time() {
    counter = 120;
    while (counter) { 
    if (myRTC.getSecond() != L_Second) {
    L_Second = myRTC.getSecond();
    Serial.print(myRTC.getDate());
    Serial.print("/");
    Serial.print(myRTC.getMonth(century));
    Serial.print("/");
    Serial.print("20");
    Serial.print(myRTC.getYear());
    Serial.print(" ");
    Serial.print(myRTC.getHour(h12Flag,pmFlag));
    Serial.print(":");
    Serial.print(myRTC.getMinute());
    Serial.print(":");
    Serial.print(myRTC.getSecond());
    Serial.print(" ");
    Serial.println(Days[myRTC.getDoW()]);
    counter--;
      }
    }
  }

Credits

stevie135s

stevie135s

25 projects • 11 followers

Comments