基于树莓派的水产养殖系统
用arduino单片机采集模拟量传感器数据
树莓派上建立网页及后端系统并使用摄像头
手机远程访问网页,实现数据和视频的实时监控
- 将PH传感器,DS18B20温度传感器,水浊度传感器放于水中适宜位置,并将三种传感器采集的数据,显示到网页上,进行实时监测。并且在水PH值过高、水温度过高、水浊度过高三种条件任意满足其中一种条件时,启动换水操作。
- 通过arduino单片机采集PH传感器,DS18B20温度传感器,水浊度传感器对应的数据,通过串口通信传输到树莓派中。
- 通过串口读取的传感器数据,并且通过Flask框架中编写的网页,将三种传感器数据和水泵的状态显示到网页中。
- 通过树莓派连接usb摄像头,通过OpenCV模块将摄像头读取并且显示到网页。实现视频监控的功能。
- arduino代码如下
/*
# This sample code is used to test the pH meter Pro V1.0.
*/
#define SensorPin A1 //pH meter Analog output to Arduino Analog Input 1
#define Offset 21.677 //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
//DS18B20
//ILOVEMCU.TAOBAO.COM
#include <Arduino.h>
#include <OneWire.h>
OneWire ds(A2); // 连接arduino2引脚
float Temp_Buffer = 0;
float TU=0.0;
float TU_value=0.0;
float TU_calibration=0.0;
float temp_data=25.0;
float K_Value=3347.19;
void setup(void)
{
pinMode(LED,OUTPUT);
Serial.begin(9600);
//Serial.println("pH meter experiment!"); //Test the serial monitor
}
void loop(void)
{
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
Serial.print("{");
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
pHValue = -5.8887*voltage+Offset;
if(pHValue<=0.0){pHValue=0.0;}
if(pHValue>14.0){pHValue=14.0;}
samplingTime=millis();
}
Serial.print("\"ph\":");
Serial.print(pHValue,1);
Serial.print(",");
//Temp_Buffer = readDs18b20();
//Serial.print(",");
Serial.print("\"temp\":");
Temp_Buffer = readDs18b20();
Serial.print(Temp_Buffer,1);
Serial.print(",");
//水浊度传感器
int sensorValue = analogRead(A0);// read the input on analog pin 0:
float TU = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
TU_calibration=-0.0192*(temp_data-25)+TU;
TU_value=-865.68*TU_calibration + K_Value;
if(TU_value<=0){TU_value=0;}
//if(TU_value>=3000){TU_value=3000;}
Serial.print("\"TU\":");
Serial.print(TU_value,1); // print out the value you read:
// Serial.print("NTU");
Serial.print("}");
delay(5000);
}
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
//Serial.println("Error number for the array to avraging!/n");
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
//DS18B20
float readDs18b20()
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
//delay(250);
// return 0;
}
//Serial.print("ROM =");
for( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
//Serial.println("CRC is not valid!");
//return 0;
}
//Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
//Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
//Serial.println("Device is not a DS18x20 family device.");
return 0;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
//delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
return celsius;
}