基于arduino制作农业大棚监控系统(采集)

所需硬件:

  • arduino uno
  • esp8266-01s x2
  • dh11温湿度传感器
  • 光敏传感器
  • 土壤湿度传感器
  • 继电器
  • 灯泡
  • 5伏电源

环境

  • Arduino IDE
  • 一个esp8266烧录程序,作为环境采集单元WiFi,参照下面链接
  • https://blog.csdn.net/m0_37738838/article/details/83999561
  • esp8266实现远程继电器控制参照下面链接
  • https://blog.csdn.net/m0_37738838/article/details/89396244

接线

arduino 传感器
A2 光敏信号引脚
A3 土壤湿度信号引脚
9 dh11信号引脚

实物及视频演示


视频链接:
https://v.youku.com/v_show/id_XNDE0MzYyODczMg==.html?spm=a2h3j.8428770.3416059.1

网页链接:
https://open.iot.10086.cn/iotbox/appsquare/appview?openid=1a00abf555d05daba8241432f806b256

程序(采集)

#include               //引用dht11库文件
#include 
#define DHT11PIN 9              //定义温湿度针脚号为9号引脚
#define SOILPIN A3  //土壤湿度传感器引脚为A2
#define LIGHTPIN A2  //光线传感器

dht11 DHT11; 
Metro metro_post=Metro(1500);
int tem,hum,light,soil;
int tem_value,hum_value,light_value,soil_value;//过滤后的值


void setup() {
  Serial.begin(115200);           //设置波特率参数
  pinMode(DHT11PIN,INPUT);  //设置引脚为输入模式
  pinMode(SOILPIN,INPUT); 
  pinMode(LIGHTPIN,INPUT); 
  pinMode(RELAYPIN,OUTPUT);
//  Serial.println(F("reading Dh11 begin. \n"));
// mySerial.begin(115200);           //设置波特率参数
}

void loop() {
  static unsigned long sensortStamp = 0;
  if(millis()-sensortStamp>1000){
    sensortStamp=millis();
    int chk = DHT11.read(DHT11PIN);                 //将读取到的值赋给chk
    tem=(int)DHT11.temperature;               //将温度值赋值给tem
    hum=(int)DHT11.humidity;                   //将湿度值赋给hum
    soil=analogRead(SOILPIN);
    light=analogRead(LIGHTPIN)/10;
    light>99?99:light;
    tem_value=Filter(tem);
    hum_value=Filter(hum);
    light_value=Filter(light);
    soil_value=Filter(soil);
    
    if(metro_post.check()){
      post_onenet(tem_value,hum_value,light_value,soil_value);
    }
//    Serial.print(F("Real Time tem: "));  
//    Serial.println(tem);  
//    Serial.print(F("Real Time hum: "));  
//    Serial.println(hum);  
//    Serial.print("Soil:");
//    Serial.println(soil);
//    Serial.print("LIGHT:");
//    Serial.println(light);
  }

}



void post_onenet(int tem,int hum,int light,int soil){
    Serial.println("POST http://api.heclouds.com/devices/522974357/datapoints?type=3 HTTP/1.1");     //打印出http协议post命令
    Serial.println("api-key: =A8iSVYS8dlMGU0nz7=ibOtlsnM= "); //apikey OneNet提供的key
    Serial.println("Host:api.heclouds.com");                //主机
    Serial.println("Connection:close");                     //断开连接  
    Serial.println("Content-Length:38");                    //传送的字节
 
    Serial.print("\r\n{\"TEM\":");                          //换行 传送格式{"TEM":tem,"HUM":hum}
    Serial.print(tem);                                //打印温度数据
    Serial.print(",\"HUM\":");                              //传送格式
    Serial.print(hum);                                //打印湿度数据
    Serial.print(",\"LIG\":");                              //传送格式
    Serial.print(light);                                //打印温度数据
    Serial.print(",\"SOI\":");                              //传送格式
    Serial.print(soil);                                //打印湿度数据
    Serial.print("}");                                      //打印出}
    Serial.println();
    
  }
 
  // 中位值平均滤波法(又称防脉冲干扰平均滤波法)(算法1)
#define FILTER_N 6
int Filter(int GetData2) {
  int i, j;
  int filter_temp, filter_sum = 0;
  int filter_buf[FILTER_N];
  for(i = 0; i < FILTER_N; i++) {
    filter_buf[i] = GetData2;
    delay(1);
  }
  // 采样值从小到大排列(冒泡法)
  for(j = 0; j < FILTER_N - 1; j++) {
    for(i = 0; i < FILTER_N - 1 - j; i++) {
      if(filter_buf[i] > filter_buf[i + 1]) {
        filter_temp = filter_buf[i];
        filter_buf[i] = filter_buf[i + 1];
        filter_buf[i + 1] = filter_temp;
      }
    }
  }
  // 去除最大最小极值后求平均
  for(i = 1; i < FILTER_N - 1; i++) filter_sum += filter_buf[i];
  return filter_sum / (FILTER_N - 2);
}


你可能感兴趣的:(物联网,Arduino,Maker)