georgelza
Posts: 68
Joined: Sun Oct 09, 2016 2:17 pm

RTC3231 only working for one call, then going unavailable...

Wed Dec 06, 2017 3:34 pm

Hi guys

Followed both:
1. https://gary-dalton.github.io/Raspberry ... _RTCds3231
2. viewtopic.php?f=63&t=161133

Both times I can call the hwclock calls after the reboot.
If I then run the python script as per Gary Dalton, once it's run I can't communicate with the RTI3231.

RTC Works:
sudo i2cdetect -y 1
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- 2c -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

RTC Does not work
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- 2c -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Error when calling Temperature script fails.
rmmod: ERROR: Module rtc_ds1307 is not currently loaded
71.6 *F / 22.0 *C

So it's sort of working,
After reboot it does not give the rammed error.

Comment/suggestions.

G

georgelza
Posts: 68
Joined: Sun Oct 09, 2016 2:17 pm

Re: RTC3231 only working for one call, then going unavailable...

Wed Dec 06, 2017 4:46 pm

Found the following document that discusses a fake-hwclock and how it interferes. Hope thats the cause.

https://learn.adafruit.com/adding-a-rea ... d-test-i2c

so far all links I'm finding interfaces directly with the RTC3231 via the hwclock command.

Got the following code now, but throwing error: '[Errno 16] Device or resource busy'

G

Code: Select all


import sys
import time
import datetime

import SDL_DS3231

# Main Program

print ""
print "Test SDL_DS3231 Version 1.0 SwitchDoc Labs"
print ""
print "Program Started at:" + time.strftime("%Y-%m-%d %H:%M:%S")

filename = time.strftime("%Y-%m-%d%H:%M:%SRTCTest") + ".txt"
starttime = datetime.datetime.utcnow()

print ""
print ""

try:
        ds3231 = SDL_DS3231.SDL_DS3231(1, 0x68)
        ds3231.write_now()
except Exception, e:
        print('Somethigng went wrong in rtc_clock', str(e))
        sys.exit()

# Main Loop sleeps 10 minutes, then reads and prints values of all clocks

while True:

        currenttime = datetime.datetime.utcnow()

        deltatime = currenttime - starttime

        print ""
        print "Raspberry Pi=\t"  + time.strftime("%Y-%m-%d %H:%M:%S")

        print "DS3231=\t\t%s" % ds3231.read_datetime()

        time.sleep(10.0)


User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC3231 only working for one call, then going unavailable...

Wed Dec 06, 2017 7:12 pm

What do you have in /boot/config.txt?
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

georgelza
Posts: 68
Joined: Sun Oct 09, 2016 2:17 pm

Re: RTC3231 only working for one call, then going unavailable...

Thu Dec 07, 2017 3:43 am

I"ll double check tonight when at home again, but should be:

dtoverlay=i2c-rtc,ds3231

G

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC3231 only working for one call, then going unavailable...

Thu Dec 07, 2017 10:05 am

Have you considered that your DS3231 may be faulty? What's in dmesg output? It's a 3V3 device unlike the DS1307 which won't run on 3V3.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

georgelza
Posts: 68
Joined: Sun Oct 09, 2016 2:17 pm

Re: RTC3231 only working for one call, then going unavailable...

Thu Dec 07, 2017 10:11 am

Havent considered the "faulty" option.

Strange though that on reboot hwclock calls work, then once the python script is called things go south.

G

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC3231 only working for one call, then going unavailable...

Thu Dec 07, 2017 1:03 pm

You need to choose between having the kernel driver (dtoverlay) and reading /dev/rtc or /dev/rtc0 or not having the driver and hitting the hardware with raw I2C (smbus) requests. You can't do both.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

georgelza
Posts: 68
Joined: Sun Oct 09, 2016 2:17 pm

Re: RTC3231 only working for one call, then going unavailable...

Thu Dec 07, 2017 2:13 pm

hehehe

Dammm, I always wanted my cake and eat it, what use is it otherwise.

So ye, went and removed the settings from /boot/config.txt and removed the comment out section in /lib/udev/hwclock-set, rebooted Pi.

Now calls via the SDL_DS3231 to the RTC works,
Note now as expected hwclock calls fail.

Interesting though the below code causes this : First call success, 2nd not so much.

I'm going to guess its just because no proper cleanup is done.
pi@rpibb:~/projects/source/rtc3231 $ python rtc_temp.py
71.6 *F / 22.0 *C
pi@rpibb:~/projects/source/rtc3231 $ python rtc_temp.py
rmmod: ERROR: Module rtc_ds1307 is not currently loaded
71.6 *F / 22.0 *C

Code: Select all

## python

import smbus
import os
import time

# Release RTC 3231
os.system('sudo rmmod rtc_ds1307')

# Setup RTC 3231 for temperature reading
bus = smbus.SMBus(1)
address = 0x68
CONV = 32

# Force a conversion and wait until it completes
def convTemp(address):
    byte_control = bus.read_byte_data(address,0x0E)
    if byte_control&CONV == 0:
        bus.write_byte_data(address, 0x0E, byte_control|CONV)
    byte_control = bus.read_byte_data(address,0x0E)
    while byte_control&CONV != 0:
        time.sleep(1)
        byte_control = bus.read_byte_data(address,0x0E)
    return True

# Get temperature in degrees C
def getTemp(address):
    convTemp(address)
    byte_tmsb = bus.read_byte_data(address,0x11)
    byte_tlsb = bus.read_byte_data(address,0x12)
    tinteger = (byte_tmsb & 0x7f) + ((byte_tmsb & 0x80) >> 7) * -2**8
    tdecimal = (byte_tmsb >> 7) * 2**(-1) + ((byte_tmsb & 0x40) >> 6) * 2**(-2)
    return tinteger + tdecimal

Celsius = getTemp(address)
Fahrenheit = 9.0/5.0 * Celsius + 32
print Fahrenheit, "*F /", Celsius, "*C"

georgelza
Posts: 68
Joined: Sun Oct 09, 2016 2:17 pm

Re: RTC3231 only working for one call, then going unavailable...

Thu Dec 07, 2017 4:21 pm

Hi all

Found another nice set of libraries and how to's:

it's for a board that available via
ThePihut, but it uses the DS3231 so all should work.

https://www.abelectronics.co.uk/kb/arti ... ian-jessie
https://thepihut.com/collections/raspbe ... rtc-pizero
https://github.com/abelectronicsuk/ABEl ... _Libraries

G

Return to “Python”

Who is online

Users browsing this forum: danjperron and 17 guests