Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By schufti
#25766 Hi,
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).
Code: Select allC:\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:
Code: Select all// 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)
Last edited by schufti on Tue Aug 18, 2015 6:13 am, edited 1 time in total.
User avatar
By kolban
#25785 It is important to realize that just because a program built for an Arduino runs on an Arduino doesn't mean that it will compile and run on an ESP8266. The project may get there eventually, but it is a journey rather than arrived yet.

I am going to guess that SoftwareWire is a library that was built for an Arduino. Because there are differences between an Arduino and an ESP, one can't shouldn't take an Arduino library and expect it to compile and run cleanly on the ESP. In some set of cases that "might" work but the chances are high that for low level libraries, porting will be required.

I have had some success using I2C and the "Wire" library that is supplied with the Arduino ESP project. I'd suggest concentrating in that area.
User avatar
By schufti
#25791 Hi!
I am well aware about the fact that there are (more than subtle) differences between these plattforms and I dont expect libs to work per se. It is the same with my crosscompiling embedded linux for sh4/arm/mips ... packages that work on one plattform usually wont on the next, not to speak about fit for crosscompiling.

I just thought someone might allready have "the recipe" for such cases.

And since I read several postings where ppl were successfull with i2c on sensors, I considdered that I just might have missed sth obvious.

Rgds,
Schufti
User avatar
By martinayotte
#25821 Don't use any SoftwareWire, simply use Wire provided in your ~/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-1044-g170995a/libraries/Wire/ folder.
Also, make sure there is no other Wire libs, because it may conflict, look at the beginning of the compile output, you will see the path of the libraries been used.