-3

Hi guys once again I got serious problem with my project.

I wanted to make a system which register measurement from tilt sensor(which can be high or low) after that it is saved in txt file and then scanning from there data is sent to database.

Unfortunately I don't know why but when i click once my txt file saves plenty of result with the same measure. Is it possible that my sensor also like buttons has got problems with joint vibration?

public class MainClass 
{
    static String[] scan_function;
    static int data_base;
    static int save_file=0;
    static boolean check=true;
    static int which_row=0;
    static int a=0;


    public static  void main(String[] args) throws InterruptedException, IOException
    {

          for(int f=0; f<scan_function.length; f++)
           {  

               scan_function[f]=null; 

           }

        final GpioController Controller = GpioFactory.getInstance();
        final GpioPinDigitalInput controller=  Controller.provisionDigitalInputPin(RaspiPin.GPIO_02,PinPullResistance.PULL_DOWN);
        String Sector="SAF154";
        String path_file= "/home/pi/Desktop/Engineer_project.txt";

        //String sciezka="C:/Users/Paweł/Desktop/Projekt_inzynierski.txt";
    int id_sensor=5;
       DateFormat format_time = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date time_calibration = new Date();

       controller.setDebounce(30000);

       controller.addListener(new GpioPinListenerDigital()
           {
                @Override
                public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event)
                {   




                                Date time_measure = new Date();   
                                System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
                                Bufor.save_to_txt(id_sensor,event.getState(),format_time, time_measure,time_calibration,Sector );


                                //save_file=3;
                                                try 
                                                {


                                                        Thread.sleep(300);      
                                                        data_base=Baza_danych.Connecting();

                                                        if(data_base==1)
                                                        {   
                                                                if(check==false)
                                                                {
                                                                    for(int g=1; g<=save_file+1; g++)
                                                                    {

                                                                        scan_function=Bufor.read_file(path_file,check,which_row);
                                                                        System.out.println(scan_function);                                                                      
                                                                        Baza_danych.Baza_danych2(Integer.parseInt (scan_function[0]), scan_function[1],scan_function[2],scan_function[3],scan_function[4]);
                                                                        which_row++;


                                                                    }
                                                                    check=true;
                                                                    a=0;
                                                                }

                                                                else
                                                                {
                                                                    scan_function=Bufor.read_file(path_file,check,which_row);
                                                                    Baza_danych.Baza_danych2(Integer.parseInt (scan_function[0]), scan_function[1],scan_function[2],scan_function[3],scan_function[4]);
                                                                    which_row=0;
                                                                    save_file=0;
                                                                    a=0;
                                                                }   

                                                        }


                                                        else
                                                        {


                                                                    if(a==0)
                                                                    {   save_file=Bufor.Count();
                                                                        System.out.println("Tyle wynosi liczba do zbioru");
                                                                        System.out.println(save_file);
                                                                        which_row=save_file;    
                                                                        check=false;    
                                                                    }
                                                                    else
                                                                    {
                                                                        check=false;    
                                                                        save_file=Bufor.Count();
                                                                        System.out.println(save_file);
                                                                        System.out.println("save_file w elsie");
                                                                    }
                                                            a++;    
                                                        }

                                                } 

                                                    catch (IOException  | ClassNotFoundException | InterruptedException e) 
                                                {
                                                    System.out.println("Something went wrong while file was charging");
                                                    e.printStackTrace();
                                                }




                                                System.out.println(which_row);
                                                System.out.println(save_file);

                }});



        while(true) 
           {

                Thread.sleep(500);
           }










    }

}

Second Class with buffer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.util.Date;
import java.util.stream.Stream;

import com.pi4j.io.gpio.PinState;


public class Bufor
{

    static int J_charge=0;
    static int J_save=0;

     public static void save_to_txt(int id_sensor,PinState data_measure, DateFormat format_time, Date time_measure , Date time_calibration, String Sector  )
    {




                try
                {       
                        File projekt = new File("/home/pi/Desktop/Engineer_project.txt");
                        PrintWriter zapis = new PrintWriter(new FileWriter(projekt,true));
                        zapis.println(id_sensor);
                        zapis.println(data_measure); 
                        zapis.println(format_time.format(time_measure));
                        zapis.println(format_time.format(time_calibration));
                        zapis.println(Sector);
                        zapis.println();
                        zapis.close();
                        System.out.println("Data was saved in file");



                }


                catch (IOException data_meausure1)
                {
                        System.out.println("No data was saved in file");
                }
    J_save++;           
    }

     public static int Count()
     {
         return J_save;
     }


     public static String[] read_file (String path, boolean check,int number_blocks ) throws IOException, InterruptedException 
     {  
          FileReader read_file = new FileReader(path);
          BufferedReader read_bufor = new BufferedReader(read_file);
          String[] nums = new String[5];

          String text;
          int i;


          for(i=0; i<5; i++)
                    {
                        nums[i]=null;
                    }


         if(check==true)
          {
                                        try {
                                                    for(i=0; i<5; i++)
                                                    {

                                                        text=Files.readAllLines(Paths.get(path)).get((i+6*J_charge));
                                                        nums[i]=text;

                                                    }

                                                    J_charge++;


                                                    } 




                                    finally { 

                                                read_bufor.close();
                                            }
          }     
          else
          {

                System.out.println("wszedlem tutaj");

              for(i=0 ;i<5; i++)
              {     

                    if(number_blocks==1)
                    {
                        text=Files.readAllLines(Paths.get(path)).get((i+6*(number_blocks-1)));
                        nums[i]=text;
                    }   
                    else
                    {   text=Files.readAllLines(Paths.get(path)).get((i+6*(number_blocks-1)));
                        nums[i]=text;
                    }


              }
              read_bufor.close();

          }




          return nums;



     }


}

If you don't understand something in the code I will naturally explain, also if you need I will put last class which is responsible for sending info to database, but i think that there is no problem.

Any ideas?

Regards!

  • 2
    Few people are likely going to want to read this giant wall of code to try and help you solve you issue. Have you taken any steps to try and debug this on your own? If so, what were the results? How do you know the other code isn't responsible for the issue? – Jacobm001 Feb 14 '17 at 21:11
  • Yes I've debug it but it's hard to measure the result because i couldn't check how it works when an external event appears. So debug didnt help much Sorry for this wall.. – Zugbur Feb 14 '17 at 23:40
1

Run the following script to check to see if your sensor needs debouncing or your program is generating spurious events.

http://abyz.me.uk/rpi/pigpio/examples.html#Python_monitor_py

The script requires the pigpio daemon to be running.

sudo pigpiod
./monitor.py

You will get one report for each GPIO state change.

  • I've already used deboucing but because this listener function it doesnt work, it seems like this Listener saving somewhere this data and after debounce finished his work he prints old measures in bash.. – Zugbur Feb 14 '17 at 23:44
  • Have you established some facts? 1) Do you need to debounce? 2) How long does the switch take to debounce? – joan Feb 15 '17 at 8:45
  • What i established is a fact that, it's probably not the fault of the program, but the sensor or buttom, no idea how to solve it. I tried to connect capasitor, but with no results... I ve notcied it when i ran my program in the morning- everything worked well after coming home situation started again – Zugbur Feb 15 '17 at 17:47
  • @Zugbur If you run the program I suggested and tripped the switch the output would indicate the amount of debounce needed. – joan Feb 15 '17 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.