Real Time Clock and BT Shield

Simple Bluetooth serial communications for your Arduino. Pair with your Mac, Linux, or Windows computer to send and receive messages using regular Serial.print() commands. [Product page]
Post Reply
John R
Posts: 5
Joined: Sat Mar 21, 2015 5:31 am

Real Time Clock and BT Shield

Post by John R » Wed May 27, 2015 8:40 am

Hi fellow Arduinians

Can anyone help please – put me back on the right path?

The Task. To monitor the Arduino Real Time Clock (RTC) via the Blue Tooth Shield into the serial monitor.

Achievements so far:
successfully monitor the RTC via the USB serial port into the serial monitor using the quick start code link.
http://www.freetronics.com.au/pages/rtc ... WFOqnuKy2Q

successfully outputting BT shield data via the code on the quick start guide
http://www.freetronics.com.au/pages/esp ... WFO_XuKy2Q

So far unsolved:
monitoring the RTC via BT shield to serial monitor
this is my best attempt at coding (ie merging the two quickstart guide sketches mentioned above).

Please note the code attached does not work (of course).

Thanks -
Attachments
TEST_RTC.144.zip
(4 KiB) Downloaded 48 times

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Real Time Clock and BT Shield

Post by andrew » Wed May 27, 2015 10:56 pm

Example - you have the Bluetooth shield connected to your Eleven, and the RX jumper is over 2 and TX jumper over 3 - and the RTC is connected and working with the example sketch:
http://cdn.shopify.com/s/files/1/0045/8 ... ds3232.ino

To send the data out via Bluetooth, we create a new Softwareserial port using the following at the start of the sketch:

Code: Select all

#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX
... and then activate it within void setup() with:
bt.begin(9600);

Now anything that you can send to the serial monitor with Serial.print (etc) you can send out to Bluetooth with
bt.print()

You can see the modifications in the following sketch:

Code: Select all

// DS3232 I2C test with Bluetooth output


#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX

#include "Wire.h"
#define DS3232_I2C_ADDRESS 0x68

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  bt.begin(9600);
}

void readDS3232time(byte *second, 
byte *minute, 
byte *hour, 
byte *dayOfWeek, 
byte *dayOfMonth, 
byte *month, 
byte *year)
{
  Wire.beginTransmission(DS3232_I2C_ADDRESS);
  Wire.write(0); // set DS3232 register pointer to 00h
  Wire.endTransmission();  
  Wire.requestFrom(DS3232_I2C_ADDRESS, 7); // request 7 bytes of data from DS3232 starting from register 00h

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.read() & 0x7f);
  *minute     = bcdToDec(Wire.read());
  *hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month      = bcdToDec(Wire.read());
  *year       = bcdToDec(Wire.read());
}

void setDS3232time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
// sets time and date data to DS3232
{
  Wire.beginTransmission(DS3232_I2C_ADDRESS);  
  Wire.write(0); // sends 00h - seconds register
  Wire.write(decToBcd(second));     // set seconds
  Wire.write(decToBcd(minute));     // set minutes
  Wire.write(decToBcd(hour));       // set hours
  Wire.write(decToBcd(dayOfWeek));  // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1~31)
  Wire.write(decToBcd(month));      // set month
  Wire.write(decToBcd(year));       // set year (0~99)
  Wire.endTransmission();
}

void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  
  // retrieve data from DS3232  
  readDS3232time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  
  // send it to the serial monitor
  bt.print(hour, DEC);// convert the byte variable to a decimal number when being displayed
  bt.print(":");
  if (minute<10)
  {
      bt.print("0");
  }
  bt.print(minute, DEC);
  bt.print(":");
  if (second<10)
  {
      bt.print("0");
  }
  bt.print(second, DEC);
  bt.print("  ");
  bt.print(dayOfMonth, DEC);
  bt.print("/");
  bt.print(month, DEC);
  bt.print("/");
  bt.print(year, DEC);
  bt.print("  Day of week:");
  switch(dayOfWeek){
  case 1:
    bt.println("Sunday");
    break;
  case 2:
    bt.println("Monday");
    break;
  case 3:
    bt.println("Tuesday");
    break;
  case 4:
    bt.println("Wednesday");
    break;
  case 5:
    bt.println("Thursday");
    break;
  case 6:
    bt.println("Friday");
    break;
  case 7:
    bt.println("Saturday");
    break;
  }
}


void loop()
{
   // setDS3232time(00, 31, 23, 2, 23, 7, 12);   // set DS3232 seconds, minutes, hours, day of week, date, month, year
   
   displayTime();
   delay(1000);
   
}
Don't forget to ensure your other device (computer etc) is paired, connected and operating at 9600 bps.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest