8 Replies Latest reply on Aug 3, 2017 4:03 PM by Intel Corporation

    Issue trying to get sensors working

    Hydrochaeris

      Hello,

       

      I am trying to work with a RFID sensor , a pollution sensor, a gas sensor and a sound sensor on an Intel Edison with extension board and a shield. I'm working on a Java Maven project with eclipse, so I tried to find libraries (upm/mraa/...) for those sensors. Unluckily I haven't found any (however I've found some libraries for the others sensors I use). For the sound and the gas sensors I've tried to use UPM library (upm_gas.Gas and upm_mic.Microphone) . However the values I get are very strange and probably not relevant. The sound value stay around 90 and the gas one around 1000 (both are raw data).
      Here is the code I'm using for them :

       

                   value = gas.getSample();

                   System.out.println(String.format("Gas value: %d", value));

       

      and

      short[] buffer = new short[128];

      public static int value;

       

       

      public void run() {

       

       

              Microphone sound = new  Microphone(Constants.SOUND_PIN);**

              Sound_Sensor sensor = new Sound_Sensor(Sound_Sensor.TYPE, 0);

       

              thresholdContext ctx = new thresholdContext();

              ctx.setAverageReading(0);

              ctx.setRunningAverage(0);

              ctx.setAveragedOver(2);

              value = sound.getSampledWindow(2, buffer);

              if (value != 0) {

                  int thresh = sound.findThreshold(ctx, 30, buffer);

                  if (thresh != 0) {

                      average = (int) ctx.getRunningAverage();

                      System.out.println("---Threshold reached---  " + average + "  "

                              + ctx.getAverageReading());

                  }

              }

              System.out.println(String.format("Sound value: %d", average));

      }

       

       

      Should I use another library ? Should I create one from scratch or use MRAA and the datasheet ?

       

      About the RFID one : I don't even know how to read data from it : I've seen some C code, but I don't know which library it's using, and what is the equivalence of some part in Java (the SoftSerial things for example). Is there a library I've missed ? If someone has any explanation on how it works and what I have to do to use it.

      And the last but not the least : the pollution sensor. I've been trying to translate some C code in Java to make it work. Here is what i've made :

       

      static Pwm led ;

      static Aio a0;

      static {

              try {

                  System.loadLibrary("mraajava");

              } catch (UnsatisfiedLinkError e) {

                  System.err.println(

                          "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +

                                  e);

                  System.exit(1);

              }

          }

      public static void setup(){

        

              mraa.init();

              led= new Pwm(iled);

              led.enable(true);

              //led.period_us(0);

              //led.pulsewidth_ms(0);

              led.write(0);

              a0 = new Aio(vout);

              a1= new Aio(vout2);

      }

       

      public static double get_density(int adcvalue){

              //int a = filter(adcvalue);

              double density;

      //        int voltage = (SYS_VOLTAGE/1024)*adcvalue*11;

      //        if(voltage>=NO_DUST_VOLTAGE){

      //            voltage-=NO_DUST_VOLTAGE;

      //            density = voltage*COV_RATIO;

      //        }

      //        else {

      //            return 0;

      //

      //        }

              density = (((SYS_VOLTAGE/1024)*adcvalue*11)-400)*0.2;

              return density; // ug/m³

          }

      public int getValue() {

              int adcvalue;

              Instant starttime = java.time.Instant.now();

              led.pulsewidth_us(280);

              adcvalue=(int) a0.read();

              adc2=(int) a1.read();

              Instant endtime = Instant.now();

              led.write(0);

        

              Duration dura = java.time.Duration.between(starttime, endtime);

              long duration = dura.toMillis();

              System.out.println("Pollution value: "+get_density(adc2)+" "+Pollution_Sensor.UNIT);

       

              System.out.println("Duration ms : "+duration);

              return adcvalue;

          }

       

      However I get irrelevant values : some runs (depending on my debugging experimentations) gave me negatives one, some gave me between ~150ug/m³ and 1500ug/m³ without any reason for the variations. When my colleague has shown me how it was working, he was using a C program and a small paper in front of the led to obstruct it to simulate dust (a lot of it!). When he did it, the values skyrocketed. In my case they skyrocket for no reason. Even when obstructing it I get random values, from 100/150 to vey high values. And I don't know why. Do you see any issue / mistake ?

        • 1. Re: Issue trying to get sensors working
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi Hydrochaeris,

          Thanks for your interest in the Intel Edison platform.

          I have been looking for Java libraries that may help you to use these sensors, however, it has been difficult to find useful information and I’d suggest you to look at these links: https://iotdk.intel.com/docs/master/upm/modules.html and https://software.intel.com/en-us/iot/hardware/sensors, there are UPM libraries for a lot of sensors and code examples, mostly in C++, JavaScript, and Python that you can use a reference to your code in Java and also to create your libraries.

          Additionally, you can look at this extra information that may help:


          Hope this information helps.

          Regards,
          -Yermi A.

          • 2. Re: Issue trying to get sensors working
            Hydrochaeris

            Thank you for your answer,

             

            I did already checked UPM and intel's sensors modules. However none of them have the libraries I need in Java, and I'm a bit lost when I have to translate part of C code to Java (especially the serial part, I don't know if the Serial part in C is the UART one in Java).

             

            About the mic, I did use this example, but as I said it gives me almost constant values.

             

            I tried to translate the RFID C code into Java but I'm not sure about some parts. Is

            var SerialPort = require("serialport").SerialPort;
            var serialPort = new SerialPort(serialPath, {
               baudrate: 9600
            });

             

            the same than

             

            Uart serial = new Uart(serialPath);

            serial.setBaudRate(9600);

             

            because I still don't know if uart=serial.

            • 3. Re: Issue trying to get sensors working
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi,
               
              Looking at the RFID sensor information, it uses the UART protocol, so the serial part is the same as the UART one. I would like to let you know that custom codes are out of our support scope so our suggestions are limited, however, you can look at the MRAA Java class list that may help in your code:  https://iotdk.intel.com/docs/master/mraa/java/annotated.html.
               
              Regarding the mic code, did you try both examples provided previously (Sound detector in Java* and Simple Analog Microphone? Have you tried using another code? I found this another example that you can take a look at: http://johnny-five.io/examples/microphone/.
               
              Regards,
              -Yermi A.
               
               

              • 4. Re: Issue trying to get sensors working
                Hydrochaeris

                Hi,

                thank you for your answer,

                 

                I've been trying to use uart for the RFID sensor, but it's not working. It doesn't detect RFID tag and uart.dataAvalaible() never goes to true.

                 

                About the mic : I tried both, but it's still very strange. Output is supposed to be dB, but I get ~900. And it doesn't increase when I'm making noise close to the sensor. It looks like i'm failing at initializing something or getting correct values.

                 

                I've tried  Have you used the dust sensor(GP2Y1010AU0F)?  for the pollution sensor. Tried with :

                 

                public int getValue() {

                        // TODO Auto-generated method stub

                        int adcvalue;

                        Instant starttime = java.time.Instant.now();

                        led.write(1);

                        while(java.time.Duration.between(starttime, java.time.Instant.now()).toNanos()<280000){

                          

                        }

                        adcvalue=(int) a1.read();

                 

                        led.write(0);

                        System.out.println("led "+led.read());

                        System.out.println("adc "+adcvalue);

                int density = (SYS_VOLTAGE/1024)*adcvalue*11*COV_RATIO;

                 

                 

                        return density;

                    }

                 

                And I'm still getting random values.

                The multichannel gas sensor is not working neither. Haven't found any code for it but I haven't searched a lot.

                 

                Edit : if there is a way to use C library in Java it could be a way to solve my issues, however the things I tried failed.

                • 5. Re: Issue trying to get sensors working
                  Intel Corporation
                  This message was posted on behalf of Intel Corporation

                  I have been able to run the MicrophoneSample.java on the Edison, these are the steps I followed:


                  Here you can find the output I got:

                  mic.png


                  Regarding the other sensors you are trying to use, my suggestion would be to test them using another programing language in which you have complete example codes and also libraries to know if they are working as they should, then when you are sure that they are working, you can try to create your code Java.

                  Regards,
                  -Yermi A.

                  1 of 1 people found this helpful
                  • 6. Re: Issue trying to get sensors working
                    Hydrochaeris

                    I tried to delete all of my sound code and to replace it from scratch with the sample and it's running as expected. I don't understand why it was not running when I tried a few days ago. Thank you !

                    The RFID sensor's green led turns on when I'm "tagging" something, but the dataAvailable() method never returns true. And the pollution one is still giving me random values.

                    I will continue to test it in java because I am lacking of time and can't deal with another language if I have to spend time to make it work.

                     

                    Thank you

                    • 7. Re: Issue trying to get sensors working
                      Hydrochaeris

                      Hi,

                       

                      I think I did find the problem with the dust sensor : we have to turn the led on for 280us, then we have 40us to read the value. However, as far as I've tested, we can't easily control the led with enough precision for this little amount of time. I tried doing a led.write(1) for 280us then reading then led.write(0). But we can't precisely wait for 280us / 40us so the values were a bit random. So I tried to set the PWM period to 320us and the pulsewidth to 40us. However I couldn't know when the led was starting it's "pulse" and when it was off. So I did show values using a While(true) without carring about when I should read or not. It appeared that the values were reacting to obstruction (got values like : 0 0 0 0 130 130 0 0 0 0 0). So what my program is doing now is reading 50 values as a sample and returning the max value of this sample. It doesn't really matter if values are not totally accurate, the only thing I need is to know when there is more dust/pollution.

                       

                      I will also get a code from a colleague for the RFID.

                       

                      However, the multichannel gas sensor still doesn't work.

                      • 8. Re: Issue trying to get sensors working
                        Intel Corporation
                        This message was posted on behalf of Intel Corporation

                        Great to know that your dust sensor is working now. In regards to the gas sensor, I do not have that to test it, so I looked for any code that may help you and I found an example, but it is in C, on this side you can find it in case you are interested in: https://developer.mbed.org/users/edamame22/code/MiCS6814_GasSensor/. Additionally, you can take a look at this other example: http://johnny-five.io/examples/grove-gas-mq2-edison/ where is used the Grove’s gas MQ2 that may help.
                         
                        Regards,
                        -Yermi A.