4 Replies Latest reply on Nov 19, 2015 4:10 PM by 3032508

    Identify and read data from multiple sensors

    3032508


      Hi,

       

      I need help to identify best approach for below requirement:

       

      I have created One midlet (Oracle Java ME embedded) which read sensor data connected to raspberry pi2 over i2c bus. Now if i add more sensors to I2C bus on different address, i need to run multiple Midlet with different sensor address to read sensor data or is there any possibility to read multiple sensor data in one midlet (Oracle Java ME embedded)?

        • 1. Re: Identify and read data from multiple sensors
          Jose Cruz

          Dear Friend

           

          You may use a listeners in Midlet class and reads different sensors connected to Raspberry Pi I2C interface.

           

          I invite you to see my article Java ME 8 + Raspberry Pi + Sensors = IoT World (Part 2) in that you could see examples of that.

           

          Best Regards

          • 2. Re: Identify and read data from multiple sensors
            3032508

            Hi Joe,

             

            Thank you for answer.

             

            I do not want to connect different sensors to Pi2, instead i want multiple temperature sensor to PI and then read that data in Midlet.

             

            Is it possible with listener? how can we identify which data of which device?

             

            Best Regards,
            Kalpesh

            • 3. Re: Identify and read data from multiple sensors
              Boris Kvartskhava-Oracle

              Hi Kalpesh,

               

              You _can_ do this on raspberry Pi. Given that both sensors can physically talk to Raspberry Pi,

              you can communicate to both sensors from same MIDlet.

               

              First thing to do for you is to make sure that both sensors connected to the bus are answering properly.

              I my case, I've put two i2c sensors on same bus (si1145 and MCP9808)

               

              To make sure that both is working properly, use i2cdetect utility (which is a part of i2c tools package)

               

              issue the command (i2c bus number is 1 in my case because I have rPI B revision 2): sudo i2cdetect -y 1
              The output should be:
                
                       0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
                 00:          -- -- -- -- -- -- -- -- -- -- -- -- --
                10: -- -- -- -- -- -- -- -- -- -- -- UU 1c -- -- --
                20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                70: -- -- -- -- -- -- -- --

               

               

              Now you have to:

              - Request I2CPermission  in MIDlet Manifest to get access to both sensors.

              - Create two I2CConfig-s (using I2CConfig constructors or, better, use I2CConfig.Builder-s): in your case,

                  depending on rPI revision, i2c bus number is either 0 or 1 (you can check it vis "ls /dev/i2c*",

                  the number at the end of file name identifies i2c bus number (controllerNumber in terms of DIO specification)

                  i2c slaves' addesses, in my case, are 0x1c and 0x60 hex (keep an eye on this, you provide addresses in numerical form)

               

              Now, given that you just created I2CDeviceConfig-s, you can use DeviceManager.open(DeviceConfig) to access both.

               

              Note though, given that both sensors share the bus, you must not overlap obtained I2CDevice-s' lifecycles.

              In particular, use shared mode = false when opening your I2CDevice-s with I2CDeviceConfig-s.

               

              My example above worked on both B and B+.

               

               

              Update: Given that I see the picture above, I do the following to make sure that both sensors are alive and are connected

              properly:
              for MCP9808:

                   - Command: sudo i2cget -y 1 0x1c 0x06 w
                  - Output: 0x5400


              for SI1145:

                    - Command: sudo i2cget -y 1 0x60 0x00
                   - Output: 0x45

              Now, given that both sensors are alive and are able to talk to my rPI, I do the following:

              To access mcp9808, I use following I2CDeviceConfig

              DeviceManager.open(

                 I2CDevice d = new I2CDeviceConfig.Builder().

                 setControllerNumber(1).  // Because on my rPI the bus corresponds to /dev/i2c-1

                 setAddress(0x1c,I2CDeviceConfig.ADDR_SIZE_7).build());

                           // this sensor works with default clock Freq on me8.2/rpi impl.

                           // Note, this does not apply in general, in this case you will have to

                           // choose the frequency that is supported by both rPI's and sensor's i2c interfaces.

               

              Now n the call to:

              I2CCombinedMessage mcp9808cm = d.getBus().createCombinedMessage().

                appendWrite(d, bOut).appendRead(bIn).transfer();

              ,  given that bOut is one-remaining-element ByteBuffer, where remaining element value is 0x06,

                bIn will contain two bytes answer =  0x00, 0x54

               

              Now, before opening si1145, I have to call to make sure the bus is not used anymore.
              So, call to

               

              d.close();

               

              (Note though, this requirement does not apply to the setups
              where the sensors are put on different buses.)

               

               

              To access si1145, I do:

              I2CDevice DeviceManager.open(

                 new I2CDeviceConfig.Builder().setControllerNumber(1).

                setAddress(0x60,I2CDeviceConfig.ADDR_SIZE_7).build());

               

              Similarly to mcp9808, you can talk to si1145

              in my example I use two i2c transactions (see si1145 datasheet for details) to do that:

               

              d.write(bOut);

              d.read(bIn);

               

              and given that bOut's remaining byte element value is 0x00,

              bIn will include one byte answer, equal to 0x45

               

               

               


              • 4. Re: Identify and read data from multiple sensors
                3032508

                Hi Boris,

                 

                Surely i will try and get back to you on this.

                 

                Best Regards,
                Kalpesh Darji