am2315 for raspberry pi in python

Our weekly LIVE video chat. Every Wednesday at 8pm ET!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
mglenn12
 
Posts: 39
Joined: Thu Dec 12, 2013 11:56 am

Re: am2315 for raspberry pi in python

Post by mglenn12 »

NerdWorld, first, I got a new AM2315 and it does register on i2c with 2 attempts--thanks. I've replicated your output of bytes using Python 2. At this time I don't have the savvy to step into Python 3. So I'm stuck at 2. If I can just get the data out of the AM2315 in some form, I can work with it. So were you ever able to simply read the sensor data? Any suggestions of code for Python 2 are appreciated.

Sopwith
 
Posts: 14
Joined: Wed Mar 19, 2014 9:50 pm

Re: am2315 for raspberry pi in python

Post by Sopwith »

I spent a lot of time and aggravation getting the AM2315 to work for me. I wrote up my findings including a step-by-step guide with screen-shots to assist others.
You can find the document here: http://sopwith.ismellsmoke.net/?p=46

Hope this helps end some agony.

Sopwith

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: am2315 for raspberry pi in python

Post by adafruit_support_rick »

@sopwith: Wow. Nice work! I never would have got there - it's wayyyy beyond my frustration threshold.
How can something so simple be so difficult? It's mind-boggling. :shock:

User avatar
ydna
 
Posts: 15
Joined: Sun Jul 21, 2013 7:47 pm

Re: am2315 for raspberry pi in python

Post by ydna »

Thanks! I havn't looked at it yet but I hope it helps :lol:

User avatar
Jahanzeb
 
Posts: 1
Joined: Wed Feb 11, 2015 6:47 pm

Re: am2315 for raspberry pi in python

Post by Jahanzeb »

hi,
i am trying to interface am2315 with PIC microcontroller. I am following your reference library for arduino...When i read the response the first 2 bytes of the response i.e 0x03(the function code) and 0x04(the length) are returned as it is...but after that i couldnot read the values of humidity and temperature....can any body help me with it?

User avatar
dkfj580
 
Posts: 1
Joined: Tue Apr 07, 2015 3:38 pm

Re: am2315 for raspberry pi in python

Post by dkfj580 »

Hi
If anyone is still interested in connecting a am2315 by smbus and python 2 after some research I managed to get it going, the problem was that in the library smbus instructions are passed as a command argument, this prevented a reading of the Answer returning results garbage data. After analyzing the code for python3 suggested by NerdWorld and library adafruit for arduino consegui implement a simple example that I describe below, this code only aims to show an example of how to do a reading, does not calculate crc and all I intended note is indicated in bold.
Sorry for any syntax errors.

Code: Select all

# -*- coding: utf-8 -*-
import time
from smbus import SMBus

I2C_BUS = 0
AM2315_I2CADDRESS = 0xB8 >> 1
AM2315_READREG = 0x03
HUMIDITY = 0
TEMPERATURE = 1

bus = SMBus(I2C_BUS)
#wake_up
try:
   bus.write_quick(AM2315_I2CADDRESS)
except:
   pass
finally:
   time.sleep(0.002)
bus.write_i2c_block_data(AM2315_I2CADDRESS, 0x03, [0x00, 4])
time.sleep(0.01)
reply = bus.read_i2c_block_data(AM2315_I2CADDRESS, 0x01, 8)
if reply[0] != AM2315_READREG or reply[1] != 4:
    print 'Fail'
humidity = float((reply[2] * 256 + reply[3]) / 10)
temperature = float(((reply[4] & 0x7F) * 256 + reply[5]) / 10)
if reply[4] >> 7:
    temperature = -temperature
print "humidity", humidity
print "temperature", temperature
Last edited by adafruit_support_rick on Wed Apr 08, 2015 8:48 am, edited 1 time in total.
Reason: please use Code tags when posting code (</> button)

User avatar
Bennard
 
Posts: 2
Joined: Fri Jul 04, 2014 1:44 am

Re: am2315 for raspberry pi in python

Post by Bennard »

Hi,
Here is another solution. Ive had my AM2315 working fine under Raspberry Pi Python2 since last summer. I gave the code so someone last summer who seemed official and he was supposed to pass it on but i dont think it went anywhere. It uses io and fcntl. I found this solution somewhere else so I am not even pretending to claim I came up with it. It has been coexisting with several other i2c components so I categorize it under "It works and that is all i care about. I do not know if I am breaking rules/conventions or anything". I cleaned some stuff out just now, but test drove this snippet and it functions and reads the 8 bytes. This also works fine for the htu21d, a sensor that is equally annoying under Python2. I never got around to doing the CRC calcs and my data has always been reasonable and consistent.

-b

Code: Select all

#!/usr/bin/python

import time
import io
import fcntl

I2C_SLAVE=0x0703
I2C_BUS=1
AM2315_ADDR = 0x5c
CMD_READ = "\x03"

class i2c(object):
    def __init__(self, device, bus):
        self.fr = io.open("/dev/i2c-"+str(bus), "rb", buffering=0)
        self.fw = io.open("/dev/i2c-"+str(bus), "wb", buffering=0)
        fcntl.ioctl(self.fr, I2C_SLAVE, device)
        fcntl.ioctl(self.fw, I2C_SLAVE, device)

    def writeI2C(self, bytes):
        self.fw.write(bytes)

    def readI2C(self, bytes):
        return self.fr.read(bytes)

    def closeI2C(self):
        self.fw.close()
        self.fr.close()

am = i2c(AM2315_ADDR, I2C_BUS) # AM2315 0x5C, bus 1
try:
    am.writeI2C(CMD_READ) # Send a read command to wake it up
except IOError:
    pass # It is going to generate an ioError, since the AM2315 wont respond while waking up, so ignore that error and then it can be read
time.sleep(0.1)
am.writeI2C(CMD_READ+"\x00\x04") # send read command starting at zero (0) (0x00) and get four (4) (0x04) registers. Which returns 8 bytes total.
time.sleep(0.01)
data = am.readI2C(8) # read 8 bytes into data. Just read it. Normal smbus has some protocol for waiting that the AM2315 doesnt follow.
am.closeI2C()
# note that data is now a string of length 8 (8 bytes). ord() or bytearray() would be two ways to get the bytes back out to crunch the numbers.

Locked
Please be positive and constructive with your questions and comments.

Return to “Ask an Engineer! VIDEO CHAT (closed)”