I am trying to connect to my am2321 via i2c. SW base is ArduinoIDE 1.6.6 hourly and esp8266 Arduino core ver. 1.6.5-1044-g170995a, built on Aug 10, 2015
The sketch that works on my arduino mini pro (328/5V/16MHz) without any problem @5 and 3.3V in both variants (wire and softwire lib) won't read anything on esp8266. And yes, I allready added pullups to be sure.
The softwire lib won't even compile due to "type conversion problems" (something that I see on several libs).
C:\Users\me\Arduino\portable\sketchbook\libraries\SoftwareWire\SoftwareWire.cpp: In constructor 'SoftwareWire::SoftwareWire(uint8_t, uint8_t, boolean, boolean)':
C:\Users\me\Arduino\portable\sketchbook\libraries\SoftwareWire\SoftwareWire.cpp:142:16: error: cannot convert 'volatile uint32_t* {aka volatile unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
_sdaPortReg = portOutputRegister(port);
^
here's my sketch:
// activate either
// #include <Wire.h>
// or
#include <SoftwareWire.h>
SoftwareWire Wire(4,5); //like default of wire lib
// depending on wanted lib
#define I2C_ADDR 0x5C
#define CMD_READ 0x03
#define REG_HUM 0x00
#define REG_MOD 0x08
#define REG_UID 0x0b
#define MAX_DAT 0x0f
int tmp=0;
unsigned int devid=0, hum=0, ver=0, mod=0;
byte buf[MAX_DAT];
void setup() {
Serial.begin(115200);
Wire.begin();
}
void loop() {
if (readAC()) {
Serial.print("Temp: ");
Serial.print(tmp);
Serial.print("\tHum: ");
Serial.println(hum);
delay(2000);
}
if (readID()) {
Serial.println(mod);
Serial.println(ver);
Serial.println(devid);
delay(2000);
}
}
bool readRaw(byte what, byte len) {
//wakeup
Wire.beginTransmission(I2C_ADDR);
delay(1);
Wire.endTransmission();
delay(1);
//send read command
Wire.beginTransmission(I2C_ADDR);
Wire.write(byte(CMD_READ));
Wire.write(what);
Wire.write(len);
Wire.endTransmission();
// for safe
delay(15);
Wire.requestFrom(I2C_ADDR, 2 + len + 2); // COMMAND + REGCOUNT + DATA + CRCLSB + CRCMSB
int i = 0;
for (; i < 2 + len; ++i)
buf[i] = Wire.read();
unsigned short crc = 0;
crc = Wire.read(); //CRC LSB
crc |= Wire.read() << 8;//CRC MSB
if (crc != crc16(buf, i))
return false;
return true;
}
bool readID() {
if (!readRaw(REG_MOD,7))
return false;
mod = buf[2] << 8 | buf[3];
ver = buf[4];
devid = buf[5] << 24 | buf[6] << 16 | buf[7] << 8 | buf[8];
return true;
}
bool readAC() {
if (!readRaw(REG_HUM,4))
return false;
hum = buf[2] << 8 | buf[3];
tmp = buf[4] << 8 | buf[5];
return true;
}
unsigned short crc16(unsigned char *ptr, unsigned char len) {
unsigned short crc = 0xFFFF;
unsigned char i = 0;
while (len--) {
crc ^= *ptr++;
for (i = 0 ; i < 8 ; i++) {
if (crc & 0x01) {
crc >>= 1;
crc ^= 0xA001;
}
else {
crc >>= 1;
}
}
}
return crc;
};
any ideas?
thanx for your help,
schufti
Edit: also i2c_scan doesn't reveal any slave (http://forum.arduino.cc/index.php?topic=197360)