Java 如何将数据从Arduino导入mysql数据库,并计算平均值
对于我的小组的最后一个项目,我们使用的是带有ph传感器的Arduino。我有ph传感器制造商提供的代码,它工作得很好。我有从Arduino到java的通信,并在输出中运行代码时显示信息。mysql语句是正确的,并且添加了所有字段。但是,当for循环中的数据写入数据库时,它会执行类似于求和的操作,并创建一个大数字。我在考虑使用数组列表获取数据,然后执行平均值。我是否需要在.read()中输入;分配给数组Java 如何将数据从Arduino导入mysql数据库,并计算平均值,java,mysql,arrays,arduino,average,Java,Mysql,Arrays,Arduino,Average,对于我的小组的最后一个项目,我们使用的是带有ph传感器的Arduino。我有ph传感器制造商提供的代码,它工作得很好。我有从Arduino到java的通信,并在输出中运行代码时显示信息。mysql语句是正确的,并且添加了所有字段。但是,当for循环中的数据写入数据库时,它会执行类似于求和的操作,并创建一个大数字。我在考虑使用数组列表获取数据,然后执行平均值。我是否需要在.read()中输入;分配给数组 package Control; import Model.fluidTe
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();
}
}
}
包装控制;
导入Model.fluidTest;
导入java.io.*;
导入java.util.*;
导入com.fazecast.jSerialComm.SerialPort;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
公共类ComPortReader{
/**
*
*@param args
*@抛出FileNotFoundException
*@抛出不支持的DencodingException
*/
公共静态void main(字符串[]args)引发FileNotFoundException,
UnsupportedEncodingException、ClassNotFoundException、SQLException、,
IOException{
//**********************************************************************
/**
*这些是连接数据库和写入数据的准备工作
*到数据库。
*
*/
PreparedStatement reading=null;
连接con=null;
//**********************************************************************
/**
*这段代码将用于从
*for循环,并将其放入数组列表中,以执行
*来自Arduino的ph输入。
*
*在上传到MYSQL时,输入被添加到一起
*产生错误ph值读数的数据库。
*/
//浮动平均
//**********************************************************************
/**
*这是为了打开计算机上的通信端口与
*输入设备。
*/
SerialPort comPort=SerialPort.getCommPorts()[0];
comPort.openPort();
comPort.SetComportTimeout(SerialPort.TIMEOUT\u READ\u SEMI\u阻塞,100,
0);
InputStream in=comPort.getInputStream();
试一试{
对于(int j=0;j<100;j++){
System.out.print((char)in.read());
}
in.close();
}捕获(例外e){
e、 printStackTrace();
}
/**
*这将开始查询我们连接到的MYSQL数据库
*使用用户定义的用户登录时使用默认参数的数据库
*和密码。
*/
尝试{//try-catch将数据写入mysql数据库。
con=
getConnection(“jdbc:mysql://localhost:3306/phlevel?
relaxAutoReconnect=false&useSSL=false&relaxAutoCommit=true“,
String sql=“插入到
输入(id、phInput、javaReading)值(?,?)”;
reading=con.preparest陈述(sql);
reading.setInt(1,0);//为执行的测试创建一个新的ID字段。
reading.setFloat(2,in.read());
//为VELVEL创建新的浮点字段
reading.setString(3,“新读数-->”;
[在此处输入图像描述][1]//创建标签以供阅读
reading.executeUpdate();
//结束MYSQL更新查询
con.commit();//结束到mysql的连接
reading.close();//将Finish语句转换为mysql
comPort.closePort();//关闭连接到ardiuno的串行端口。
}catch(SQLException-ex){
例如printStackTrace();
}
}
}
首先不应该有id(主键)列应设置为自动递增,插入数据时无需指定它。我使用了您的示例,效果非常好。我只需再添加一个内容即可使其工作。我收到一个错误。感谢大家帮助我奠定基础,找到解决方案
averagePH+=Float.parseFloat(Integer.toString(in.read());
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);