2017-05-08 1 views
0

nimmt Für meine Gruppenabschlussprojekt verwenden wir einen Arduino mit pH-Sensor angebracht. Ich habe den mitgelieferten Code von der ph-Sensor-Hersteller zur Verfügung gestellt, die gut funktioniert. Ich habe Kommunikation von Arduino zu Java und zeigt auch die Informationen an, wenn der Code in der Ausgabe ausgeführt wird. Die mysql-Anweisung ist korrekt und alle Felder sind hinzugefügt. Wenn die Daten aus der for-Schleife in die Datenbank geschrieben werden, wird jedoch eine Summe gebildet und eine große Zahl erstellt. Ich dachte daran, eine Array-Liste zu verwenden, um die Daten aufzunehmen und dann einen Durchschnitt durchzuführen. Würde ich das in.read() brauchen; dem Array zugewiesen?Wie man Daten von Arduino in eine MySQL-Datenbank mit einem berechneten Durchschnitt

 package Control; 

    import Model.fluidTest; 
    import java.io.*; 
    import java.util.*; 
    import com.fazecast.jSerialComm.SerialPort; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 


    public class ComPortReader { 

/** 
* 
* @param args 
* @throws FileNotFoundException 
* @throws UnsupportedEncodingException 
*/ 
    public static void main(String[] args) throws FileNotFoundException, 
    UnsupportedEncodingException, ClassNotFoundException, SQLException, 
    IOException { 
    //********************************************************************** 
    /** 
    * These are preparation for the database to be connected and write data 
    * to database. 
    * 
    */ 
    PreparedStatement reading = null; 
    Connection con = null; 
    //********************************************************************** 
    /** 
    * This section of code will be used to take the information from the 
    * for loop and place it into a array list to perform the average of the 
    * ph input from the Arduino. 
    * 
    * The input is being added together while uploading to the MYSQL 
    * database which is producing incorrect ph level readings. 
    */ 

    // float average 
    //********************************************************************** 
    /** 
    * This is to open the communication port on the computer to talk to the 
    * input device. 
    */ 
    SerialPort comPort = SerialPort.getCommPorts()[0]; 
    comPort.openPort(); 
    comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 
    0); 
    InputStream in = comPort.getInputStream(); 
    try { 
     for (int j = 0; j < 100; j++) { 
     System.out.print((char) in.read()); 
     } 
     in.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    /** 
    * This begins the query to the MYSQL database we are connecting to the 
    * database using default parameters while using user defined user login 
    * and password. 
    */ 




    try {// Try catch to possibly write data to a mysql database. 
     con = 
    DriverManager.getConnection("jdbc:mysql://localhost:3306/phlevel? 
    relaxAutoReconnect=false&useSSL=false&relaxAutoCommit=true", 

     String sql = "INSERT INTO 
    phlevel.inputreadings(id,phInput,javaReading) values(?,?,?)"; 
     reading = con.prepareStatement(sql); 
     reading.setInt(1, 0); // Creates a new ID Field For performed test. 
     reading.setFloat(2, in.read()); 
     // Creates new Float Field for phLevel 
     reading.setString(3, "New Reading---->"); 
     [enter image description here][1]// Creates Label for reading   
     reading.executeUpdate(); 

     // End MYSQL update QUERY 

     con.commit();//End connection to mysql 
     reading.close();// Finish statements to mysql 
     comPort.closePort(); // Close the Serial port connected to ardiuno. 

    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 

} 
} 

Antwort

0
 final int SAMPLE_DATA_COUNT = 100; 

     float averagePH = 0; 

     for (int j = 0; j < SAMPLE_DATA_COUNT; j++) { 
     System.out.print((char) in.read()); 
     averagePH += Float.parseFloat(in.read()); 
     } 

     averagePH /= SAMPLE_DATA_COUNT; // Insert this value to SQL 
     ... 
     reading.setFloat(2, averagePH); 

Ich denke, diese Linie

reading.setInt(1, 0); 

dort nicht in erster Linie sein, die ID (Primärschlüssel) Spalte sollte auf Auto Increment eingestellt werden und Sie brauchen nicht zu Spezifizieren Sie es beim Einfügen von Daten.

+0

Ich habe Ihr Beispiel verwendet und es hat sehr gut funktioniert. Ich musste nur noch eine Sache hinzufügen, damit es funktioniert. Ich habe einen Fehler erhalten. Danke, ein Haufen, der mir hilft, Bodenarbeit zu machen, um die Lösung zu finden 'averagePH + = Float.parseFloat (Integer.toString (in.read()));' –

Verwandte Themen