It can be difficult to set the time and date on a DS3231 RTC there's so many variables that need to be set at the precise time. This project is designed to use the system time and date of an android phone and by using an app connected to an esp32 over BLE, it will simplify the setup.
It is not important to have a computer connected to the ESP32/DS3231 when setting the code. The serial monitor will merely show what's happening (be sure to have it open before flashing). This code can be added to any project you do that periodically require time setting. It runs on boot.
It seems that the code may not compile at the point std:value = ...
If this is the case try the second code. I will check things work when i have time. This is a new error
#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()]);
}
}
#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--;
}
}
}
Comments