This discussion is archived
5 Replies Latest reply: Apr 3, 2013 10:44 AM by A Tael RSS

HELP:DAAPI on RPI (Java ME 3.3)

A Tael Journeyer
Currently Being Moderated
Hello,
I am a beginner when it comes to HW and understanding documentation for that so I'll see if someone on this list can help me. What I am trying to accomplish is to read temperature using a TMP36 sensor (http://www.adafruit.com/products/165). As RPI doesn't have a analog input I am using a MCP3008 ADC (http://www.adafruit.com/products/856) to convert that analog value into a digital value.

The idea is to use the DAAPI SPI APIs to communicate with the MCP3008, and here is where I don't understand how to do this. The MCP3008 doucmentation is here: http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf

The code I am using looks like this (this is not the full source, it is just the part where I am trying to get the temp) and the parts I really would appreciate if someone told me how to is the initialization string (txBuf) and the getTemperature method. As you probably can see I have no clue what I am doing :-)
import com.oracle.deviceaccess.PeripheralManager;
import com.oracle.deviceaccess.PeripheralNotAvailableException;
import com.oracle.deviceaccess.PeripheralNotFoundException;
import com.oracle.deviceaccess.PeripheralTypeNotSupportedException;
import com.oracle.deviceaccess.spibus.SPIDevice;
import java.io.IOException;

/**
 *
 * @author atael
 */
public class TMP36 {
    
    private static SPIDevice mcp3008 = null;
    private static final String SPIDEVICE_NAME = "SPI_Slave";
    
    public static boolean connect() {
        boolean isConnected = false;
        try {
            mcp3008 = (SPIDevice) PeripheralManager.open(SPIDEVICE_NAME,SPIDevice.class, null);
            // Configuration - from the docs reading CH0 (TMP 36 connected there)
            // .... ...S S210 xxxx xxxx xxxx
            // 0000 0001 1000 0000 0000 0000
            byte[] txBuf = { (byte)0x01, (byte)0x40, (byte)0x00 }; 
            mcp3008.begin();
            mcp3008.write(txBuf, 0, txBuf.length);
            mcp3008.end();
            isConnected = true;
        } catch (PeripheralNotAvailableException ex) {
            ex.printStackTrace();
        } catch (PeripheralNotFoundException ex) {
            ex.printStackTrace();
        } catch (PeripheralTypeNotSupportedException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return isConnected;
    }
    
    public static void disconnect () {
        if (mcp3008 != null) {
            try {
                mcp3008.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public static int getTemperature () {
        try {
            mcp3008.begin();
            mcp3008.write(0x00);
            int value = mcp3008.read();
            mcp3008.end();
            return value;
        } catch (PeripheralNotAvailableException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return 0;
    }
}
Edited by: A Tael on Mar 28, 2013 12:16 PM

Edited by: A Tael on Mar 28, 2013 12:21 PM

Edited by: A Tael on Mar 28, 2013 12:22 PM
  • 1. Re: HELP:DAAPI on RPI (Java ME 3.3)
    sergey_n Explorer
    Currently Being Moderated
    Hi A Tael,

    could you please provide output generated by your application?

    Currently I see a the only problem in the code when you call spi.write and then spi.read. From hardware point of view it is 2 separate session started by acquiring CE0 signal and ended with CE0 release. I've not looked at protocol but if the device sends answer to command in the same session spi.writeAndRead has to be used instead.

    /Sergey
  • 2. Re: HELP:DAAPI on RPI (Java ME 3.3)
    A Tael Journeyer
    Currently Being Moderated
    Hello Sergey,
    you are probably correct, so I changed the code to look like this:
    package tmp36rpi;
    
    import com.oracle.deviceaccess.PeripheralManager;
    import com.oracle.deviceaccess.PeripheralNotAvailableException;
    import com.oracle.deviceaccess.PeripheralNotFoundException;
    import com.oracle.deviceaccess.PeripheralTypeNotSupportedException;
    import com.oracle.deviceaccess.spibus.SPIDevice;
    import java.io.IOException;
    
    /**
     *
     * @author atael
     */
    public class TMP36 {
        
        private static SPIDevice mcp3008 = null;
        private static final String SPIDEVICE_NAME = "SPI_Slave";
        
        public static boolean connect() {
            boolean isConnected = false;
            try {
                mcp3008 = (SPIDevice) PeripheralManager.open(SPIDEVICE_NAME,SPIDevice.class, null);
                byte[] rxBuf = new byte[3];
                byte[] txBuf = new byte[3];
                txBuf[0] = (byte) 1;
                txBuf[1] = (byte) ((8 + 0) << 4);
                txBuf[2] = (byte) 0;
                mcp3008.begin();
                mcp3008.writeAndRead(txBuf, 0, txBuf.length, rxBuf, 0, rxBuf.length);
                mcp3008.end();
                isConnected = true;
            } catch (PeripheralNotAvailableException ex) {
                ex.printStackTrace();
            } catch (PeripheralNotFoundException ex) {
                ex.printStackTrace();
            } catch (PeripheralTypeNotSupportedException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return isConnected;
        }
        
        public static void disconnect () {
            if (mcp3008 != null) {
                try {
                    mcp3008.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        public static int getTemperature () {
            try {
                byte[] rxBuf = new byte[3];
                byte[] txBuf = new byte[3];
                txBuf[0] = (byte) 1;
                txBuf[1] = (byte) ((8 + 0) << 4);
                txBuf[2] = (byte) 0;
                mcp3008.begin();
                mcp3008.writeAndRead(txBuf, 0, txBuf.length, rxBuf, 0, rxBuf.length);
                mcp3008.end();
                System.out.println("0: " + (int)rxBuf[0]);
                System.out.println("1: " + (int)rxBuf[1]);
                System.out.println("2: " + (int)rxBuf[2]);
                return ((rxBuf[1] & 3) << 8) + rxBuf[2];
            } catch (PeripheralNotAvailableException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return 0;
        }
    }
    And when I run my IMLet I get the following in the console:
    ***************************************
    * MCP3008/TMP36 Thermometer Sample    *
    ***************************************
    Trying...
    0: 1
    1: 1
    2: 1
    MCP3008Checker start is OK.
    0: -1
    1: -1
    2: -1
    Temp: 767
    [AMS-TRACE] MIDlet:TMP36rpi status=2
    [AMS-TRACE] MIDlet:TMP36rpi status=1
    0: 1
    1: 1
    2: 1
    Temp: 0
    0: 1
    1: 1
    2: 1
    Temp: 0
    0: 1
    1: 1
    2: 1
    Temp: 0
    0: 1
    1: 1
    2: 1
    Temp: 0
    0: 1
    1: 1
    2: 1
    Temp: 0
    [AMS-TRACE] MIDlet:TMP36rpi status=3
    [AMS-INFO] MIDlet:TMP36rpi terminated
    Not sure what I am doing wrong here at all. I have added the rest of the code below:
    package tmp36rpi;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    /**
     *
     * @author atael
     */
    public class MCP3008Checker extends TimerTask {
    
        private Timer timer;
        private static int INTERVAL_MS = 5000;
        private int temperature;
        private String name;
        
        
        MCP3008Checker( String name){    
            this.name = name;
        }
        
        public boolean start () {
            boolean isStarted = false;
            System.out.println ("Trying...");
            if (TMP36.connect()) {
                timer = new Timer();
                timer.schedule(this, 0, INTERVAL_MS);
                temperature = TMP36.getTemperature();
                isStarted = true;
            }
            return isStarted;
        }
        
        public void run() {
            int newTemperature = TMP36.getTemperature();
            if (newTemperature != temperature) {
                temperature = newTemperature;
            }
            System.out.println("Temp: " + temperature );
        }
        
        public void stop () {
            if (timer != null) {
                timer.cancel ();
            }
            TMP36.disconnect();
        }
    }
    and
    package tmp36rpi;
    
    import javax.microedition.midlet.*;
    
    /**
     * @author atael
     */
    public class IMlet extends MIDlet {
    
        private MCP3008Checker mcp3008Checker;
        private static final String name = "MCP3008";
        
        public void startApp() {
                   
            System.out.println("***************************************");
            System.out.println("* MCP3008/TMP36 Thermometer Sample    *");
            System.out.println("***************************************");
            
            mcp3008Checker = new MCP3008Checker(name);
            
            if (mcp3008Checker.start() ) {
                System.out.println("MCP3008Checker start is OK.");
            } else {
                System.out.println("MCP3008Checker start has failed.");
            }
        }
        
        public void pauseApp() {
        }
        
        public void destroyApp(boolean unconditional) {
            if (mcp3008Checker != null) {
                mcp3008Checker.stop();
            }
        }
    }
    --Andy

    Edited by: A Tael on Apr 2, 2013 1:12 PM
  • 3. Re: HELP:DAAPI on RPI (Java ME 3.3)
    sergey_n Explorer
    Currently Being Moderated
    Hi Andy,

    the command sequence looks correct. What I can only suggest is to reduce clock frequency down to 75kHz and check if clock phase and clock polarity are correct (see jwc_propeties.ini for deviceaccess.spi.slave0.*). Try to set clockMode = 3

    /Sergey
  • 4. Re: HELP:DAAPI on RPI (Java ME 3.3)
    A Tael Journeyer
    Currently Being Moderated
    Sergey,
    I made the change as you proposed but it doesn't seem to work anyway. The output I get is:
    Network: getHostByname: 10.154.114.43
    Got 0x1BE7C08, len 4
    Network: getHostByname: 10.154.114.43
    Got 0x1BE7C08, len 4
    Network: getHostByname: 10.154.114.43
    Got 0x1BE7C08, len 4
    Network: getHostByname: 10.154.114.43
    Got 0x1BE7C08, len 4
    [AMS-TRACE] MIDlet:TMP36rpi status=2
    ***************************************
    * MCP3008/TMP36 Thermometer Sample    *
    ***************************************
    Trying...
    0: 1
    1: -128
    2: 0
    MCP3008Checker start is OK.
    0: 1
    1: -64
    2: 0
    Temp: 0
    [AMS-TRACE] MIDlet:TMP36rpi status=1
    0: 1
    1: -128
    2: 0
    Temp: 0
    0: 1
    1: -64
    2: 0
    Temp: 0
    0: -1
    1: -1
    2: -1
    Temp: 767
    0: 1
    1: -64
    2: 0
    Temp: 0
    0: 1
    1: -128
    2: 0
    Temp: 0
    [AMS-TRACE] MIDlet:TMP36rpi status=3
    [AMS-INFO] MIDlet:TMP36rpi terminated
    The way everything is connected is like this:

    This MCP3008 is connected like this:
    MCP3008 VDD -> 3.3V
    MCP3008 VREF -> 3.3V
    MCP3008 AGND -> GND
    MCP3008 CLK -> SCLK (GPIO11)
    MCP3008 DOUT -> MISO (GPIO9)
    MCP3008 DIN -> MOSI (GPIO10)
    MCP3008 CS -> CE0 (GPIO8)
    MCP3008 DGND -> GND

    The TMP36 sensor is connected like this:
    Pin 1: 2.7-5.5V In -> 3.3V
    Pin 2: Analog Voltage Out -> CH0 on MCP3008
    Pin 3: Ground -> GND

    The Raspberry Pi is connected to the Oracle NW if you'd like to take a look and is an Oracle employee.

    Thanks
    Andy
  • 5. Re: HELP:DAAPI on RPI (Java ME 3.3)
    A Tael Journeyer
    Currently Being Moderated
    Sergey,
    thanks a lot for your help.

    --Andy                                                                                                                                                                                                                   

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points