Community
cancel
Showing results for 
Search instead for 
Did you mean: 
New Contributor II
1,100 Views

AM2315 and JavaScript

I got the AM2315 to work without library in arduino, I only need to make it work on javascript.

Can anyone help me there.

Here is my current arduino code.

# define AM2315_I2CADDR 0x5C

# define AM2315_READREG 0x03

# include

float humidity, temp;

void setup()

{

Wire.begin(); // join i2c bus (address optional for master)

Serial.begin(115200); // start serial for output

delay(100);

}

void readData()

{

uint8_t reply[10];

// Wake up the sensor

Wire.beginTransmission(AM2315_I2CADDR);

delay(2);

Wire.endTransmission();

// OK lets ready!

Wire.beginTransmission(AM2315_I2CADDR);

Wire.write(AM2315_READREG);

Wire.write(0x00); // start at address 0x0

Wire.write(4); // request 4 bytes data

Wire.endTransmission();

delay(10); // add delay between request and actual read!

Wire.requestFrom(AM2315_I2CADDR, 8);

for (uint8_t i=0; i<8; i++) {

reply[i] = Wire.read();

//Serial.println(reply[i], HEX);

}

humidity = reply[2];

humidity *= 256;

humidity += reply[3];

humidity /= 10;

Serial.print("Humidity: "); Serial.println(humidity);

temp = reply[4] & 0x7F;

temp *= 256;

temp += reply[5];

temp /= 10;

Serial.print("Temp: "); Serial.println(temp);

}

void loop()

{

readData();

delay(5000);

}

Tags (1)
1 Reply
Highlighted
Community Manager
31 Views

Hello KimLorentz,

 

 

I suggest you to try using mraa. This library can control I2C and it has several examples ( https://github.com/intel-iot-devkit/mraa/blob/master/examples/javascript/bmp85.js and https://github.com/intel-iot-devkit/mraa/blob/master/examples/javascript/rgblcd.js) and documentation ( http://iotdk.intel.com/docs/master/mraa/node/classes/i2c.html) that will be of much help for you when trying to "translate" your script to JavaScript.

 

 

I hope this information helps you.

 

-Peter.
0 Kudos