Am2315

I worked in a small project over the weekend to connect an AM2315 from adafruit up to the imp.

the following is the code I created for this project.

`function phex(s) {
local h = “”;
for(local i=0;i<s.len();i++) h+=format("%02x", s[i]);
return h;
}

imp.configure(“AM2315 Sensor”, [], []);

local i2c = 0x5c<<1;

hardware.configure(I2C_89);
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ);

function bigLoop() {
hardware.i2c89.write(i2c, “\x00”); // I need this to wake up the bus
hardware.i2c89.write(i2c, “\x03\x00\x04”);

imp.sleep(0.15);

reply <- hardware.i2c89.read(i2c, “\x03”, 6);

if ( reply[0] == 0x03 && reply[1] == 0x04) {
local humidity = reply[2];
humidity *= 256;
humidity += reply[3];
humidity /= 10.0;

local temperature = reply[4];
temperature *= 256;
temperature += reply[5];
temperature /= 10.0;
temperature *= (9.0/5.0);
temperature += 32.0
server.log(format("temperature=%.02f F, humidity=%.02f %%R.H.", temperature, humidity))
server.show(format("%.02f F, %.02f %%R.H.", temperature, humidity))

}
else {
server.log(“bad reply from i2c:” +phex(reply))
}
imp.wakeup(15, bigLoop); // sleep for 5 min.
} bigLoop();`

Hope this helps. btw, you must add pull-up resistors to SDA and SCL. Also the device seems to work better if you use Vin

deleted… edited the previous post. thanks @jwehr. I did not see the edit link.

You can. There is an edit button after your name and the time. Thanks for posting BTW… I’ll pick one up the next time I order from Adafruit.

I am planning going to power my April board with 5VDC by way of the VIN and GND terminals. I’m also planning on pulling the SCL and SDA lines to the 3.3v terminal on the April board with 10K resistors. Am I correct in doing so?

I’ve been using your code successfully for the past 24 hours and all of a sudden, I started getting the following errors one after another:
2014-01-10 23:05:05 UTC-5: [Status] Device booting; 3.18% program storage used
2014-01-10 23:05:05 UTC-5: [Device] ERROR: the index ‘0’ does not exist
2014-01-10 23:05:05 UTC-5: [Device] ERROR: at bigLoop:23
2014-01-10 23:05:05 UTC-5: [Device] ERROR: from main:49

line 23 is:
if ( reply[0] == 0x03 && reply[1] == 0x04) {

and line 49 is:
bigLoop();

Any thoughts?

It would seem that reply is not an array any more. That would figure if the i2c read failed due to there being a problem with the sensor or the wiring to it?

I opened the AM2315, found the conductors from the cable twisted what I thought was a bit excessively, straightened them out and voila! It started working, again. I’ve used one of the sensors in the field for a couple of months with a Ruggeduino and it’s been rock solid. I’m hoping the issue here was simply a bad connection due to the wires being twisted. I really enjoy working with the imp, so I’m hoping this problem is behind me. The good news is that I cleaned up some of my sloppy code and learned quite a bit in the process. Resolving problems is always a great learning opportunity if you’re persistent!

Any feedback on why the i2c address is coded as “local i2c = 0x5c<<1;” instead of “local i2c = 0xB8” when they’re the same, the official address in the documentation is “0xBC” and the two are equivalent?