智慧大棚web页面-大棚温湿度监控告警

通过JQUERY和JASON进行前后的传递。

首先是进行servlet的创建,在数据库中获取1号和6号大棚的各项参数。然后封装为JASON格式发送给前端

package mysqlLogin.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Home")
public class Home extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Home() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		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 sql1 = "select * from `emp`.`greenhouse` "+ "where houseid = '0001'" +" order by id desc limit 0,1 ";
		String sql2 = "select * from `emp`.`greenhouse` "+ "where houseid = '0006'" +" order by id desc limit 0,1 ";
		ArrayList<GreenHouse> greenList = new ArrayList<GreenHouse>();
		
		String gsObject = null;
		boolean t = false;

		try {
			Class.forName(driverMysql);
			Connection conn = DriverManager.getConnection(urlMysql, userNameMysql, passWordMysql);
			PreparedStatement ps = conn.prepareStatement(sql1);
			ResultSet rs = ps.executeQuery();
			rs.next();
            //�¶�
			String temp1 = rs.getString("temp");
			//ʪ��
			String humity1 = rs.getString("humity");
			//����
			String bright1 = rs.getString("bright");
			//smoke
			String smoke1 = rs.getString("smoke");
			//co2
			String co21 = rs.getString("co2");
			//planttype
			String planttype1 = rs.getString("planttype");
			 
			//ʱ��
			String time1 = rs.getString("time");
			//��
			String secondIot1 = rs.getString("secondIot");
			//������
			String houseid1 = rs.getString("houseid");
			
			GreenHouse gh1 = new GreenHouse(temp1,humity1,bright1,smoke1,co21,planttype1, time1, secondIot1, houseid1);
			
			
			PreparedStatement ps2 = conn.prepareStatement(sql2);
			ResultSet rs2 = ps2.executeQuery();
			rs2.next();
            //�¶�
			String temp2 = rs2.getString("temp");
			//ʪ��
			String humity2 = rs2.getString("humity");
			//����
			String bright2 = rs2.getString("bright");
			//smoke
			String smoke2 = rs2.getString("smoke");
			//co2
			String co22 = rs2.getString("co2");
			//planttype
			String planttype2 = rs2.getString("planttype");
			 
			//ʱ��
			String time2 = rs2.getString("time");
			//��
			String secondIot2 = rs2.getString("secondIot");
			//������
			String houseid2 = rs2.getString("houseid");
			
			GreenHouse gh2 = new GreenHouse(temp2,humity2,bright2,smoke2,co22,planttype2, time2, secondIot2, houseid2);
						
			greenList.add(gh1);
			greenList.add(gh2);
						
			Gson gs = new Gson();
			gsObject = gs.toJson(greenList);
			rs.close();
			rs2.close();
			conn.close();
			
			  
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setContentType("text/html;charset=UTF-8 ");
		response.getWriter().print(gsObject);
		
	

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
package mysqlLogin.servlet;

public class GreenHouse {
	
	   //�¶�
		String temp;
		//ʪ��
		String humity;		
		//����
		String bright;
		//smoke
		String smoke;
		//co2
		String co2;
		//planttype
		String planttype;				
		//ʱ��
		String time;
		//��
		String secondIot;
		//������
		String houseid ;
		public GreenHouse(String temp, String humity, String bright, String smoke, String co2, String planttype,
				String time, String secondIot, String houseid) {
			super();
			this.temp = temp;
			this.humity = humity;
			this.bright = bright;
			this.smoke = smoke;
			this.co2 = co2;
			this.planttype = planttype;
			this.time = time;
			this.secondIot = secondIot;
			this.houseid = houseid;
		}
		
		
		
		
		public GreenHouse(String temp, String humity, String time, String secondIot, String houseid) {
			super();
			this.temp = temp;
			this.humity = humity;
			this.time = time;
			this.secondIot = secondIot;
			this.houseid = houseid;
		}




		public String getTemp() {
			return temp;
		}
		public void setTemp(String temp) {
			this.temp = temp;
		}
		public String getHumity() {
			return humity;
		}
		public void setHumity(String humity) {
			this.humity = humity;
		}
		public String getBright() {
			return bright;
		}
		public void setBright(String bright) {
			this.bright = bright;
		}
		public String getSmoke() {
			return smoke;
		}
		public void setSmoke(String smoke) {
			this.smoke = smoke;
		}
		public String getCo2() {
			return co2;
		}
		public void setCo2(String co2) {
			this.co2 = co2;
		}
		public String getPlanttype() {
			return planttype;
		}
		public void setPlanttype(String planttype) {
			this.planttype = planttype;
		}
		public String getTime() {
			return time;
		}
		public void setTime(String time) {
			this.time = time;
		}
		public String getSecondIot() {
			return secondIot;
		}
		public void setSecondIot(String secondIot) {
			this.secondIot = secondIot;
		}
		public String getHouseid() {
			return houseid;
		}
		public void setHouseid(String houseid) {
			this.houseid = houseid;
		}




		@Override
		public String toString() {
			return "GreenHouse [temp=" + temp + ", humity=" + humity + ", bright=" + bright + ", houseid=" + houseid
					+ "]";
		}
		
		
		

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是基于STM32的蔬菜大棚环境监控系统DHT11温湿度传感器程序设计的代码示例: ``` #include "stm32f10x.h" #include "dht11.h" GPIO_InitTypeDef GPIO_InitStructure; void Delay_us(uint32_t us) { uint32_t i; while(us--) { i = 7; while(i--); } } void DHT11_IO_OUT(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void DHT11_IO_IN(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } uint8_t DHT11_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); DHT11_IO_OUT(); GPIO_SetBits(GPIOA, GPIO_Pin_0); Delay_us(1000); GPIO_ResetBits(GPIOA, GPIO_Pin_0); Delay_us(18000); GPIO_SetBits(GPIOA, GPIO_Pin_0); Delay_us(30); DHT11_IO_IN(); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1); return 1; } uint8_t DHT11_Read_Byte(void) { uint8_t i, dat; dat = 0; for(i = 0; i < 8; i++) { while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0); Delay_us(30); if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1) { dat |= 1 << (7 - i); } while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1); } return dat; } uint8_t DHT11_Read_Data(uint8_t* temp, uint8_t* humi) { uint8_t buf[5], i; DHT11_Init(); if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1) { return 0; } while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1); for(i = 0; i < 5; i++) { buf[i] = DHT11_Read_Byte(); } if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) { *humi = buf[0]; *temp = buf[2]; return 1; } return 0; } ``` 这段代码包含了DHT11初始化、读取温湿度数据等功能。需要注意的是,这段代码中使用了延时函数Delay_us(),需要根据自己的实际情况进行调整。同时,也需要根据实际使用的GPIO口进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值