function onInit() {
//连接wifi
pinMode(D27, 'input_pullup');
pinMode(D14, 'input_pullup');
pinMode(D39, "analog");
var ssid = 'wifi账号';
var password = 'wifi密码';
var wifi = require('Wifi');
wifi.connect(ssid, {password: password}, function() {
console.log('Connected to Wifi. IP address is:', wifi.getIP().ip);
wifi.save(); // 测试网络是否可用,如可用则输出IP
});
setWatch(function(e) {
console.log("Button pressed 27");
//请求数据
//gm
var data = " ";
var sensor={wendu:23, //封装成json格式
deviceid:"h7hs7h",
sidu:78,
gm:154
};
var gm=analogRead(D39)*100;
gm= Math.round(gm);
sensor.gm=gm; //保存实际光敏传感器值
var json1=JSON.stringify(sensor);
console.log(sensor);
var json1=JSON.stringify(sensor);
var request = require("http").request(
{host:"服务器IP", method:"POST",port: 8080,path:"/工程名/servlet的名字", headers:{
"Content-Type":"application/json;charset=UTF-8",
"content-length": json1.length,
"connection":"close"
}},
response=>{
var content = "";
response.on("data", data=>{
content += data;
});
response.on("close", ()=>{
console.log(content);
});
response.on("error", err=>{
console.log("error", err);
});
});
request.write(json1);
request.end();
//button end
}, D27, { repeat: true, edge: 'falling', debounce: 50 });
//
setWatch(function(e) {
console.log("Button pressed 14");
//请求数据
//button end
}, D14, { repeat: true, edge: 'falling', debounce: 50 });
}
save();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
StringBuffer jb=new StringBuffer();
String line=null;
String result="";
//获取请求内容
try {
BufferedReader reader=request.getReader();
while((line=reader.readLine())!=null){
jb.append(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str=jb.toString();
System.out.println("strL"+str);
//将json字符串中的数据写入我们写的sonsor对象
try {
sensor=gson.fromJson(str, Sensor.class);
System.out.println("sensor:"+sensor);
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensor.setDate(new Date());//获取当前时间并写入sensor对象中
int insertSensor = service.InsertSensor(sensor);//调用service层中的插入数据库方法
if (insertSensor>=1) {
System.out.println("win");
}else {
System.out.println("lose");
}
response.getWriter().append("OK");
}
在servlet中的代码中主要分为三个部分
(1) 获取传入的json数据
(2)将获取到的json数据写入我们bean层中的sensor类
(3)调用dao层方法写入数据库
public class SensorService {
SensorDao sensorDao;
public SensorService() {
// TODO Auto-generated constructor stub
sensorDao=new SensorDao();
}
public int InsertSensor(Sensor sensor) { //插入数据库方法
return sensorDao.InsertSensor(sensor);
}
public List SelectSensor(){ //显示数据方法
return sensorDao.SelectSensor();
}
}
public class SensorDao {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public int InsertSensor(Sensor sensor){
int i=0;
String sql="insert into sensor values(null,?,?,?,?,?)";
try {
conn = DBUtils.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, sensor.getDeviceid());
ps.setInt(2, sensor.getWd());
ps.setInt(3, sensor.getSd());
ps.setInt(4, sensor.getGm());
SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sFormat.format(sensor.getDate());
//格式化sensor对象中的时间后方可写入数据库
ps.setString(5, currentTime);
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtils.close(rs, ps, conn);
}
return i;
}
}
要导的包
gson-2.8.4.jar
mysql-connector-java-5.1.18-bin.jar