BH1750 with SoftI2C

(deleted)

don't know which softI2C lib you uses but you should check the examples and build something like

#include <softI2C.h>

softI2C softWire(SDA, SCL);  // 2 pins

...
softWire.begin();

change wire -> softwaire in the lib.

might get you in the direction,

Posting a link to the softI2C lib used will also help

(deleted)

(deleted)

Can you post your code with the softI2C lib?

An idea is to start creating a sniffer for softi2c to check if the lib works and if it can see the sensor

(deleted)

  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = i2c_read();  // receive one byte
    i++;
  }

requests to read 2 bytes from the address

This snippet does read 3 bytes from BMAADDR

boolean readBma(void)
{
  xval = 0xFFFF;
  yval = 0xFFFF;
  zval = 0xFFFF;
  if (!i2c_start(BMAADDR | I2C_WRITE)) return false;
  if (!i2c_write(0x02)) return false;
  if (!i2c_rep_start(BMAADDR | I2C_READ)) return false;
  xval = readOneVal(false);
  yval = readOneVal(false);
  zval = readOneVal(true);
  i2c_stop();
  return true;
}

should not be to difficult to rewrite ?

(deleted)

buff[0] = 0xFFFF;
buff[1] = 0xFFFF;

is worng as buff is an array of bytes

buff[0] = 0xFF;
buff[1] = 0xFF;


BH1750_Read should return the number of bytes read or an error code, not true or false

int BH1750_Read2Bytes(int address)   
{
  buff[0] = 0xFF;
  buff[1] = 0xFF;
  
  if ( !i2c_start(address | I2C_WRITE) ) return -1;
  if ( !i2c_write(0x02)) return -2;
  if  (!i2c_rep_start(address | I2C_READ)) return -3;

  buff[0] = readOneVal(false);
  buff[1] = readOneVal(true);  
  // i2c_stop();  // not needed is handled by the true flag

  return 2;
}

int readOneVal(boolean last)
{
  int value = i2c_read(false);          // LSB
  value +=  256 * i2c_read(last);    // MSB
  if (last) i2c_stop();
  return value/64;
}

(deleted)

(deleted)

What do you see?

time to add print statements to see where the code blocks...

(deleted)

Yes, does read return any value / error code or does it block??

try this? explicitly dump return value..

void process(YunClient client) 
{
  uint16_t val = 0;
  BH1750_Init(client);
  delay(200);
    client.println("before value");

  int x = BH1750_Read(client);
  if (2 == x)
  {
    val = ((buff[0] << 8) | buff[1]) / 1.2;
    client.print(val, DEC);
    client.println("[lx]");
  }
  else
  {
    client.print("X = ");
    client.println(x, DEC);
    client.print("buff[0] = ");
    client.println(buff[0]);
    client.print("buff[1] = ");
    client.println(buff[1]);
  }
}

(deleted)

(deleted)

So this line is triggered

if ( !i2c_start(BH1750address|I2C_WRITE) ) return -1;

that can imply the address is wrong

Give it a try with address

int BH1750address = 0x46;

I do not have a yun but could you try to run this

//
//    FILE: soft_I2C_scanner.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: test
//    DATE: 
//     URL:
//
// Released to the public domain
//

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <SoftI2CMaster.h>

#include <math.h> 

int BH1750address = 0x23; 
byte buff[2];


#define SCL_PIN 5
#define SCL_PORT PORTD
#define SDA_PIN 4
#define SDA_PORT PORTD

// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;

void setup() 
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  server.listenOnLocalhost();
  server.begin();
  
  i2c_init();
}

void loop() 
{
  YunClient client = server.accept();
  if (client) 
  {
    scanI2C(client);
    client.stop();
  }
  delay(500);
}

void scanI2C(YunClient client) 
{
  for (int address = 0; i < 128; i++)
  {
    int result = i2c_start( address | I2C_WRITE);
    if (result != 0) 
    {
      client.print("ADDR: \t");
      client.println(address);
    }
    i2c_stop();
    delay(10);
  }
}

(deleted)