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();
}
}
}
나는 당신의 예를 사용했고 매우 잘 작동했습니다. 작동시키기 위해 한 가지 더 추가해야했습니다. 오류가 발생했습니다. 솔루션'averagePH + = Float.parseFloat (Integer.toString (in.read()))를 찾을 기초 작업을 도와 줘서 고마워. –