Python Library for Rpi for: adafruit-ds3231-precision-rtc-breakout


#1

Hi all

Looking but not finding, can someone point me to the Python libraries for the Rpi for this component.

Thanks

Precision RTC Breakout](https://shop.pimoroni.com/products/adafruit-ds3231-precision-rtc-breakout)
G


#2

What is it you want to do from python?


#3

Anything and everything that can be done with the RTC.

Bought the unit primary to connect to my Rpi to play with, learn Python, interface.

G


#4

I have 2 in use, I just followed the install tutorial to get them working. The only thing I do in python is call up the date time to display it on my sense hat.

dateString = "%A %B %-d %-I:%M %p"
    msg = "It is %s" % (datetime.datetime.now().strftime(dateString))
    sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, 255))

The only other thing I’ve done is set the correct time via the terminal interface.


#5

A temperature/humidity or GPS breakout board would likely be better for learning python. I learned a lot with those types of boards. I learned a ton of code playing with my sense hat.


#6

Done the Temp, Humity, Pressure sensors,

thanks.


#7

Get an explorer Phat/Hat or enviro pHat maybe? I’m currently working on a rover project with a Zero W and Explorer pHat, or will be after Christmas. Or an adventures on Mincraft kit etc.


#8

Got an EnviroPhat already,
Had that connected to a Rpi, collecting and uploading data (Baro pressure) to https://corlysis.com/
Had a 2nd RPiZeroW with a DHT22 also uploading Temp and humidity to same Corlysis profile,

over the pass weekend removed the EnviroPhat (As a pressure source) and replaced it with a BMP280 (also connect to the Zero, so got Temp, Pressure and humidity being uploaded from a single source, and oh there are some LED’s also involved that light up depending on conditions.

Next one is making the RTC work with a Display-o-tron.

Got a motion sensor also lying around, will modify mini project to bring screen alive and display date/time when it detects motion.

G


#9

Cool.

I have a Pi A+ with a Sense Hat, DS3231, BMP180 and SI1145. Its a portable weather clock showing the Day, date, time, temp, humidity, pressure and UV index in a scrolling message on the Sense Hat LED matrix. The temps in the enclosure, even though its vented, make the Sense Hat temp read high so I mounted the BMP180 outside the case to get accurate temp readings. I also have 4 10mm LEDS, Red, Yellow, Green and Blue for a secondary indicator. Its coded for temps at the moment Blue if below 0c for example. They are switched on and off via GPIO pins so I can code them to what ever I want in my python file. The whole thing is powered by a PowerBoost 1000c and a 6600 MAH LIPO battery. The color of each part of the message changes color based on the conditions. Blue text for temp if below 0c etc.


#10

any chance you might be willing to share the code? Sounds interesting.

G


#11
import os
import time, datetime
import Adafruit_BMP.BMP085 as BMP085
import SI1145.SI1145 as SI1145
import RPi.GPIO as GPIO
from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
   
sense = SenseHat()
sense.set_rotation(180)
sense.set_imu_config(False, False, False)
sense.low_light = False

bmp = BMP085.BMP085()
uvs = SI1145.SI1145()

GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_OFF)  
GPIO.setup(16,GPIO.OUT, initial=1) #Red
GPIO.setup(19,GPIO.OUT, initial=1) #Yellow
GPIO.setup(20,GPIO.OUT, initial=1) #Green
GPIO.setup(21,GPIO.OUT, initial=1) #Blue


s=(0.1) # scroll speed
w=(0) # color all white toggle
o=(165)
L=(1) #LED's on or off
x=(2) #shutdown variable
m=(0)

def Shutdown(channel):  
    global x
    x = (0)

def readvis():
    vis = uvs.readVisible()
    global w
    global o
    global L
    global m
    if vis < 270 and m == (0):
        sense.low_light = True
        w = (0)
        o = (165)
        L = (1)
    elif vis >= 270 and vis < 500 and m == (0):
        sense.low_light = False
        w = (0)
        o = (165)
        L = (0)
    elif vis >= 500 and m == (0):
        sense.low_light = False
        w = (255)
        o = (255)
        L = (0)
    
# is really stick down
def pushed_up(event):
    global L
    global m
    if event.action == ACTION_PRESSED:
       sense.low_light = True
       L = (1)
       m = (1)
        
# is really stick up
def pushed_down(event):
    global L
    global m
    if event.action == ACTION_PRESSED:
       sense.low_light = False
       L = (0)
       m = (1)

#is really stick right
def pushed_left(event):
    global w
    global o
    global m
    if event.action == ACTION_PRESSED:
        w = (255)
        o = (255)
        m = (1)
        
# is really stick left
def pushed_right(event):
    global w
    global o
    global m
    if event.action == ACTION_PRESSED:
        w = (0)
        o = (165)
        m = (1)

def pushed_middle(event):
    global m
    if event.action == ACTION_PRESSED:
        m = (0)

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
sense.stick.direction_middle = pushed_middle

GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

while True:

    readvis()

    dateString = "%A %B %-d %-I:%M %p"
    msg = "It is %s" % (datetime.datetime.now().strftime(dateString))
    sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, 255))

    t = bmp.read_temperature()
    t = (round(t))
          
    if t <= 0: 
        tc = [w, w, 255]  # blue
        GPIO.output(16, 1) #Red
        GPIO.output(19, 1) #Yelow
        GPIO.output(20, 1) #Green
        GPIO.output(21, L) #Blue
    elif t > 0 and t < 13:
        tc = [255, 255, w]  # yellow
        GPIO.output(16, 1) #Red
        GPIO.output(19, L) #Yelow
        GPIO.output(20, 1) #Green
        GPIO.output(21, 1) #Blue
    elif t >= 13 and t <= 25:
        tc = [w, 255, w]  # green
        GPIO.output(16, 1) #Red
        GPIO.output(19, 1) #Yelow
        GPIO.output(20, L) #Green
        GPIO.output(21, 1) #Blue
    else:
        tc = [255, w, w]  # red
        GPIO.output(16, L) #Red
        GPIO.output(19, 1) #Yelow
        GPIO.output(20, 1) #Green
        GPIO.output(21, 1) #Blue
        
    msg = "and %sc" % (t)
    sense.show_message(msg, scroll_speed=s, text_colour=tc)

    h = sense.get_humidity()
    h = (round(h))
    if h > 100:
        h = 100
    if h >= 30 and h <= 60:
        hc = [w, 255, w]  # green
        msg = "with %s%% Humidity" % (h)
        sense.show_message(msg, scroll_speed=s, text_colour=hc)
    elif h > 60 and h < 80:
        hc = [255, 255, w]  # yellow
        msg = "with %s%% Humidity" % (h)
        sense.show_message(msg, scroll_speed=s, text_colour=hc)
    else:
        hc = [255, w, w]  # red
        msg = "with %s%% Humidity" % (h)
        sense.show_message(msg, scroll_speed=s, text_colour=hc)

    readvis()

    p = sense.get_pressure()
    p = round(p)
        
    if p >= 960 and p < 985:
        pc = [255, w, w]  # red
        msg = "- Barometer is Very Low @ %smb - Storm Watch" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 985 and p < 1005:
        pc = [255, 255, w]  # yellow
        msg = "- Barometer is Low @ %sm - Posible Percipitation" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 1005 and p < 1025:
        pc = [w, 255, w]  # green
        msg = "- Barometer is Mid Range @ %smb" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 1025 and p < 1050:
        pc = [w, w, 255]  # blue
        msg = "- Barometer is High @ %smb" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 1050:
        pc = [255, w, w]  # red
        msg = "- Barometer is Very High @ %smb - Expect Dry Conditions" % (p) 
        sense.show_message(msg, scroll_speed=s, text_colour=pc)

    uv = uvs.readUV()
    u = uv/100
    u = (round(u))

    if u < 3 and L == 0:
        msg = "- UV Index is Low @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, w)) # green        
    elif u >= 3 and u < 6 and L == 0:
        msg = "- UV Index is Moderate @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, 255, w)) # yellow        
    elif u >= 6 and u < 8 and L == 0:
        msg = "- UV Index is High @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, o, w)) # Orange       
    elif u >= 8 and u < 11 and L == 0:
        msg = "- UV Index is Yery High @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, w ,w)) # red
    elif u >= 11 and L == 0:
        msg = "- UV Index is Extreme @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, w, 255)) # violet
        
    #vis = uvs.readVisible()

    #msg = "and the VIS is %s" % (vis)
    #sense.show_message(msg, scroll_speed=s, text_colour=(255, w, 255))

    if x == 0:
        os.system("sudo shutdown now -P")

#12

I’m using the ambient light reading to auto adjust my display brightness. In bright sunlight all text turns white. The Red and Blue text is hard to read in bright light. In normal light the text is colored. In dark conditions the LED matrix dims and the three 10 mm LED’s are turned off. I can override the auto mode by using the sense hat joystick. Center press puts it back in auto mode. I have a shutdown button wired to a GPIO to do the shutdown. It’s in a watertight case with a clear front. It’s not waterproof anymore though, I drilled a couple vent holes in it so the sense sensors can work. And for cooling. Build pictures here if your interested.
https://1drv.ms/f/s!AjOYwiwlwDtpgq8_0VrdS3_H5xL_AA
THPUV.py is the above file. THP.py is a simpler version without the BMP180 or SI1145.


#13

Some really nice clean code there :)

hmm, missing the lines where you use the DS ?

G


#14

Once the DS3231 is setup correctly, it becomes the hwclock. On boot up the time is pulled from it. When I call the time in python “import time, datetime” its taken from the DS3231. This Pi is an A+ with no WFI dongle. It has no Internet connection so I need accurate battery backed up time. That’s what the DS3231 does. That’s all I use it for.

dateString = "%A %B %-d %-I:%M %p"
    msg = "It is %s" % (datetime.datetime.now().strftime(dateString))
    sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, 255))

The message shown is “It is Thursday December 7 5.48 AM”


#15

Basically read a article about 30min ago that made that light also come on for me.

Realised that…

Wondering if the error messages that I get when calling the DS3231 directly is not because of this, that i wants me to pull the time from the standard date time calls and not try and access the DS3231 directly.

After Reboot on first calls the hwclock calls work, once I call a python script to access say the temperature, first call works, after that it throws errors, the call directly to the DS3231 via the SDL_DS3231 library reports device is busy.

G


#16

That may be, I’m no expert, especially where Python is concerned. Everything I’ve learned was by trial and error. Mostly error lol. I do believe mine will auto update the time (correct it if its off) if I connect to the Internet. I’ve only ever been at most a minute off. Without the RTC Raspbian just saves the current time when you shut down. Then uses that on boot up. If it has an Internet connection it gets the time from a time server and updates it so its current and correct. Otherwise its back to the future, lol.

My code doesn’t care or even know if there is a RTC. It just pulls the current time the Pi is using. I’m running the full Raspbian Jessie, I just set it to boot to the command line to save time and resources. I just find setup easier than going with the lite version.


#17

Would be interesting to get more feedback from someone at Pimoroni.

Have another source I’ll also quickly ping, find his view/experience…

G


#18

This is the guide I follow to setup my RTC, https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi
I have one DS1307 and three DS3231’s, all bought from Adafruit.


#19

So some feedback.

If/When you add dtoverlay to /boot/config.txt and comment out the 3 lines in /lib/udev/hwclock-set then the only way to get time is via the standard datetime. call in a Python script, or date at the command line or via hwclock at the command line, you can’t call something like switch doc’s SDL_DS3231 library to get a time directly from the RTC as it is now locked down by the kernel.

if you wanna make calls to the RTC directly then remove lines from /boot/config.txt and uncomment lines from the /lib directory.

G


#20

Kind of what I thought. Thanks for the info. I would think that’s going to mess up the displayed time in Raspbian though. It won’t be getting it (syncing) from the RTC. It wouldn’t be a big deal for me as that Pi runs headless. I think I’d rather do it the way I’m doing it now though. The time is the same everywhere that way.