2017-05-08 49 views
0

對於我的組最終項目,我們使用的是帶有ph傳感器的Arduino。我有從ph傳感器製造商提供的提供的代碼,其工作良好。我有從Arduino到Java的通信,並且還顯示代碼在輸出中運行時的信息。 mysql語句是正確的,所有的字段都被添加。但是,當來自for循環的數據寫入數據庫時​​,它會執行一些操作,並創建一個大數。我正在考慮使用數組列表來獲取數據,然後平均執行。我需要有in.read();分配給數組?如何將數據從Arduino轉換爲平均計算的mysql數據庫

 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(); 
    } 

} 
} 

回答

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); 

我覺得這條線

reading.setInt(1, 0); 

不應該有擺在首位,該ID(主鍵)列應設置爲自動遞增,你不需要在插入數據時指定它。

+0

我用你的例子,它工作得很好。我只需要再添加一件東西就可以工作。我收到一個錯誤。感謝大家幫助我奠定基礎,找到解決方案'averagePH + = Float.parseFloat(Integer.toString(in.read()));' –

相關問題