购买了windows版本的腾讯云服务器后,在云服务器上通过JAVA的Socket 进行各个数据的接受,并将这些数据保存到数据库中。
package zjiot;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Server {
public static void main(String args[]) {
int port = 9990;
ServerSocket server = null;
try {
server = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (true) {
Socket socket = null;
try {
socket = server.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread(new Task(socket)).start();
}
}
/**
*
*/
static class Task implements Runnable {
String driverMysql = "com.mysql.jdbc.Driver";
String urlMysql = "jdbc:mysql://localhost:3306/emp?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String userNameMysql = "root";
String passWordMysql = "asdf1234";
String sql = "insert into greenhouse values (null,?,?,?,?,?,?,?,?,?) ";
Connection conn = null;
PreparedStatement ps = null;
private Socket socket;
private double tempIot;
private double humity;
String timeIot;
public Task(Socket socket) {
this.socket = socket;
try {
Class.forName(driverMysql);
conn = DriverManager.getConnection(urlMysql, userNameMysql, passWordMysql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
try {
handleSocket();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ���ͻ���Socket����ͨ��
*
* @throws Exception
*/
private void handleSocket() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
StringBuilder sb = new StringBuilder();
String temp;
int index;
while ((temp = br.readLine()) != null) {
System.out.println(temp);
sb.append(temp);
}
System.out.println("收集数据串: " + sb);
String sbIot = sb.toString();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
timeIot = dateFormat.format(date);
int mao1=sbIot.indexOf(":");
int fen1=sbIot.indexOf(";");
String temprature=sbIot.substring(mao1+1, fen1);
System.out.println(temprature);
int mao2=sbIot.indexOf(":",mao1+1);
int fen2=sbIot.indexOf(";",fen1+1);
String humity=sbIot.substring(mao2+1, fen2);
System.out.println(humity);
int mao3=sbIot.indexOf(":",mao2+1);
int fen3=sbIot.indexOf(";",fen2+1);
String bright=sbIot.substring(mao3+1, fen3);
System.out.println(bright);
int mao4=sbIot.indexOf(":",mao3+1);
int fen4=sbIot.indexOf(";",fen3+1);
String smoke=sbIot.substring(mao4+1, fen4);
System.out.println(smoke);
int mao5=sbIot.indexOf(":",mao4+1);
int fen5=sbIot.indexOf(";",fen4+1);
String co2=sbIot.substring(mao5+1, fen5);
System.out.println(co2);
int mao6=sbIot.indexOf(":",mao5+1);
int fen6=sbIot.indexOf(";",fen5+1);
String planttype=sbIot.substring(mao6+1, fen6);
System.out.println(planttype);
int mao7=sbIot.indexOf(":",mao6+1);
int fen7=sbIot.indexOf(";",fen6+1);
String houseID=sbIot.substring(mao7+1, fen7);
System.out.println(houseID);
String secondIot = timeIot.substring(18);
if (secondIot.equals("00") ||secondIot.equals("05") || secondIot.equals("10") || secondIot.equals("15")|| secondIot.equals("20") ||secondIot.equals("25") || secondIot.equals("30")
|| secondIot.equals("40") || secondIot.equals("45")|| secondIot.equals("50")|| secondIot.equals("55")) {
//Temperature:29.20;Humidity:53.30;Bright:472.00;Smoke:168.00;Co2:4.45;Planttype:?.??�??;houseId: 0001
ps = conn.prepareStatement(sql);
ps.setString(1, new Double(temprature).toString());
ps.setString(2, new Double(humity).toString());
ps.setString(3, new Double(bright).toString());
ps.setString(4, new Double(smoke).toString());
ps.setString(5, new Double(co2).toString());
ps.setString(6, planttype);
ps.setString(7, timeIot);
ps.setString(8, secondIot);
ps.setString(9, houseID);
ps.execute();
ps.close();
}
conn.close();
br.close();
socket.close();
}
}
}
在这个代码中需要注意的是,要用线程进行客户端的创建,不然端口就会阻塞,这样智慧大棚发几次数据后,端口就阻塞住了,云服务器就收不到数据了。
其次要注意数据库Mysql 要进行最大连接数的修改,Mysql默认的最大连接数是非常小的,智慧大棚发送一段时间后就无法将数据写到数据库中。这个时候就需要修改数据库的最大连接数了。
具体命令如下:
show variables like 'max_connections';
set GLOBAL max_connections=90000;
为了让服务器上的云数据库能够在其他地方进行访问,就设置了Mysql数据库的所有权限,面向所有访问IP地址。这样就可以轻松在自己的电脑上进行数据库的访问了。
GRANT ALL PRIVILEGES ON *.* TO 'lily'@'%' IDENTIFIED by '123456';
FLUSH PRIVILEGES;
CREATE TABLE `greenhouse` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`temp` char(100) DEFAULT NULL,
`humity` char(100) DEFAULT NULL,
`bright` char(100) DEFAULT NULL,
`smoke` char(100) DEFAULT NULL,
`co2` char(100) DEFAULT NULL,
`planttype` varchar(255) DEFAULT NULL,
`time` char(100) DEFAULT NULL,
`secondIot` char(100) DEFAULT NULL,
`houseid` char(20) NOT NULL,
PRIMARY KEY (`id`,`houseid`) USING BTREE,
UNIQUE KEY `time` (`time`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=59995 DEFAULT CHARSET=utf8;