Published Tuesday 26 June 2012
Categories: Android | Projects | Solar Power |

Share this Share on Facebook Share on Twitter Share on Google Plus
Post View Counter 37

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 to our online reporting website.

The code below is used to get the sensor data from the Microchip MCP9800/1/2/3 sensor via the I2C interface. The code below is used with the IOIOActivity based on the HelloIOIO application code:

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

Initmcp9803(0x48, twi);

0x48 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

float sensortemp = Readmcp9803(0x48, twi);

The code below are the functions which initialise the sensor and also get the sensor values

// temperature sensors
	    
	    public boolean Initmcp9803(int address, TwiMaster port) {
	    	byte[] request_on = new byte[] { 0x01 };
	        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) 0x00 };
					    
					    try {
							if(port.writeRead(address, false, request_setGain,request_on.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 float Readmcp9803(int address, TwiMaster port) {
	    	
				
	    	float returnval = 0;
	        	byte[] request = new byte[] { 0x00 };
	        	//byte[] request2 = new byte[] { (byte) (address + 1) };
	        	byte[] tempdata = new byte[2];
	           	try {
	           		port.writeRead(address, false, request,request.length,tempdata,tempdata.length);
	           		
	           		//port.writeRead(address, false, request2,request2.length,tempdata,tempdata.length);
	           		float ambienttemp = tempdata[0];
	              	ambienttemp += (float)tempdata[1] / 256. ;
	              	return ambienttemp;
				} catch (ConnectionLostException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {}
	    	   
	    	   return returnval;
	    }

Permalink