Page 1 of 1

rtc_ds1307 - Can't read temp on DS3231

Posted: Sun Mar 27, 2016 12:52 pm
by pdesnoyers76
I'm try to read the temp from a DS3231 rtc. But everytime I try I get: IOError: [Errno 16] Device or resource busy.... If I kill the process rtc_ds1307, as per below, then it works:
1- sudo rmmod rtc_ds1307
2- run python script to get temp

Code: Select all

import smbus
bus = smbus.SMBus(1)
address = 0x68
def getTemp(address):
   byte_tmsb = bus.read_byte_data(address,0x11)
   byte_tlsb = bin(bus.read_byte_data(address,0x12))[2:].zfill(8)
   return byte_tmsb+int(byte_tlsb[0])*2**(-1)+int(byte_tlsb[1])*2**(-2)
print getTemp(address)
3- re-enable: sudo modprobe rtc_ds1307

I don't want to have to do that, is there a better driver, or another way that would allow me to get the internal temp without kill the process all the time? If this is the only way, I there negative effect of kill a process and restart it all the time. My plan is to check the internal temp every 2 to 5 min. So potentially 30 time a hour the process would get kill and then restarted.

Re: rtc_ds1307 - Can't read temp on DS3231

Posted: Sat Aug 13, 2016 3:37 am
by sgarcia05
I am not sure if there is a way to read the temperature while the rtc is active, but you could disable and re-enable the module inside the python script. it prints temp in *F and *C

call the os to remove the rtc and call it again to add it. I have this on a rev B and a pi zero, it saves on adding a temp sensor :mrgreen:

Code: Select all

import smbus
import os

bus = smbus.SMBus(0)
address = 0x68

os.system('sudo rmmod rtc_ds1307')
def getTemp(address):
   byte_tmsb = bus.read_byte_data(address,0x11)
   byte_tlsb = bin(bus.read_byte_data(address,0x12))[2:].zfill(8)
   return byte_tmsb+int(byte_tlsb[0])*2**(-1)+int(byte_tlsb[1])*2**(-2)

#print getTemp(address)
Celsius = getTemp(address)
Fahrenheit = 9.0/5.0 * Celsius + 32
print Fahrenheit, "*F /", Celsius, "*C"
os.system('sudo modprobe rtc_ds1307')

Re: rtc_ds1307 - Can't read temp on DS3231

Posted: Sun Aug 14, 2016 7:03 pm
by GSSMahadevan
I had similar issue with my 9$ chip's i2c module axp209 (power management IC).
By default i2-tools/py-smbus module does not provide an option to forcefully open a i2c-device-address( for safe reasons). But i2get command gives option '-y' to read/write.
So i forked i2c-tools/py-smbus in githubhttps://github.com/GssMahadevan/i2c-tools to allow python code also to have similar option. I have changed 3 lines in python module code.
One can see example of my modified python module code at py-smbus githubhttps://github.com/GssMahadevan/chip_axp209_tool
This modified py-smbus modules can be used for raspberry also

Re: rtc_ds1307 - Can't read temp on DS3231

Posted: Wed Nov 16, 2016 1:58 pm
by huygens
There is a way to get the temperature for the sysfs pseudo-filesystem (aka /sys). It does not require to unload the module because it is information that the module expose directly to the system.

Simply read the following file: /sys/class/i2c-adapter/i2c-1/1-0068/hwmon/hwmon0/temp1_input (note that this is on a Raspberry Pi 2, it might be different path on older Raspberry Pies, my DS3231 is exposed on address 68 on SPI2 bus and I'm also running Raspbian Jessie with Kernel 4.8).

The output of this file might be something like:

Code: Select all

21250
You need to divide it by 1000 for proper reading of the temperature in °C.

Example shell code:

Code: Select all

echo "$(cat /sys/class/i2c-adapter/i2c-1/1-0068/hwmon/hwmon0/temp1_input)/1000" | bc -l
Note: I was surprised Raspbian does not come with 'bc' preinstalled. First time ever I had to install it on a Linux distribution ;-). But this is as easy as doing:

Code: Select all

sudo apt-get install bc
[/i]

Re: rtc_ds1307 - Can't read temp on DS3231

Posted: Wed Nov 16, 2016 2:08 pm
by huygens
Actually there is something even simpler :-)

Once your module is loaded it is recognised by the sensors command (package lm-sensors).

So simply do:
$ sensors
ds3231-i2c-1-68
Adapter: 3f804000.i2c
temp1: +21.5°C
You can install lm-sensors by doing:
sudo apt-get install lm-sensors