As part of the home solar PV and hot water project, we are now using an IOIO board with an android tablet to process the sensor data and upload it to our online reporting website. The code below is used to get the sensor data from the TSL2561 digital luminosity/lux / light sensor via the I2C interface. The sensor was sourced from http://www.adafruit.com/products/439
In the main class define the TwiMaster interface.
private TwiMaster twi;
In the setup() code inside the Looper class add
twi = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_100KHz, false);
The 0 is the port which you are using for the TwiMaster
Next, initialise the sensor with
InitTSL2561(0x39, twi);
0x39 is the address of the sensor. The initialisation code is shown in the functions below.
Inside the loop() you can use this code to obtain the sensor value
int lightsensors[] = ReadTSL2561(0x39); int visible = lightsensors[0]; int irlight = lightsensors[1];
The code below are the functions which initialise the sensor and also get the sensor values
// light sensor public boolean InitTSL2561(int address, TwiMaster port) { byte[] request_on = new byte[] { 0x0A }; byte[] response = new byte[4]; try{ try { if( port.writeRead(address, false, request_on,request_on.length,response,response.length)) { //(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain) byte[] request_setGain = new byte[] { (byte) 0x80, 0x01, 0x00 }; try { if(port.writeRead(address, false, request_setGain,request_setGain.length,response,response.length)) { return true; } } catch (ConnectionLostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { return false; } } catch (ConnectionLostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch(InterruptedException e) { return false; } return false; } public int[] ReadTSL2561(int address) { int channel0 = 99; int channel1 = 99; byte[] DataHigh = new byte[1]; byte[] DataLow = new byte[1]; byte[] DataHigh2 = new byte[1]; byte[] DataLow2 = new byte[1]; byte[] response = new byte[4]; int[] returnval = new int[2]; try { // init sensor byte[] request_on = new byte[] { 0x03 }; twi.writeRead(address, false, request_on,request_on.length,response,response.length); // get low channel byte[] request_2a = new byte[] { (byte)0x8C }; twi.writeRead(address, false, request_2a,request_2a.length,DataLow,DataLow.length); byte[] request_2b = new byte[] {(byte) 0x8D }; twi.writeRead(address, false, request_2b,request_2b.length,DataHigh,DataHigh.length); // get high channel byte[] request_higha = new byte[] { (byte)0x8E }; twi.writeRead(address, false, request_higha,request_higha.length,DataLow2,DataLow2.length); byte[] request_highb = new byte[] {(byte) 0x8F }; twi.writeRead(address, false, request_highb,request_highb.length,DataHigh2,DataHigh2.length); channel0 = ((0xFF & (int) DataHigh[0]) * 256) + ((0xFF & (int) DataLow[0])); channel1 = ((0xFF & (int) DataHigh2[0]) * 256) + ((0xFF & (int) DataLow2[0])); returnval[0] = channel0; returnval[1] = channel1; // close request byte[] request_off = new byte[] { 0x00 }; twi.writeRead(address, false, request_off,request_off.length,response,response.length); return returnval; } catch (ConnectionLostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally {} return returnval; } public int[] bytearray2intarray(byte[] barray) { int[] iarray = new int[barray.length]; int i = 0; for (byte b : barray) iarray[i++] = b & 0xff; // "and" with 0xff since bytes are signed in java return iarray; }
Comments