---
# System prepended metadata

title: 農業科技5-暗渠排水
tags: [農業科技]

---

---
title: 農業科技5-暗渠排水
tags: 農業科技
---

Mega 2560才可以用的程式SD+指紋+RFID



---

燒入看門狗的程式:

不用網路就可以有感知器



---

````
#include <Wire.h>
#include <SoftwareSerial.h>

//看門狗測試，如果秒數超過會重開機
#include <avr/wdt.h>

//lcd需要的涵數
#include <motoLiquidCrystal_I2C.h>

//LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C mylcd(0x27, 16, 2);

//DHT-------------------------------------------------
#include "motoDHT.h"
//DHT 01 D9
#define DHTPIN1 9 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN1, DHTTYPE);
//DHT-------------------------------------------------

int Liquid_level = 0;
void setup()
{
  //數位輸出13支腳，LED
  pinMode(13, OUTPUT);

  Serial.begin(9600);

  //濕度感測
  pinMode(A0, INPUT);

  //switch開關1
  pinMode(10, OUTPUT);

  //水位傳感器
  pinMode(5, INPUT);

  //溫濕度
  Serial.println("DHT11");
  dht.begin();

  //lcd
  mylcd.init();
  mylcd.backlight();

  //----------------------------------------------------------看門狗
  wdt_enable(WDTO_8S);      //當機8秒後重開機
  // Valid delays:
  //  WDTO_15MS
  //  WDTO_30MS
  //  WDTO_60MS
  //  WDTO_120MS
  //  WDTO_250MS
  //  WDTO_500MS
  //  WDTO_1S
  //  WDTO_2S
  //  WDTO_4S
  //  WDTO_8S
  //-------------------------------------------------------------

}

void loop()
{

  //變數宣告
  int value_a0 = 1023 - analogRead(A0);
  //int value_a0 = analogRead(A0);
  Serial.print("土壤濕度：");
  Serial.println(value_a0);
  Liquid_level = digitalRead(5);
  Serial.print("水位訊號：");
  Serial.println(Liquid_level);

  //溫濕度---------------------------------------------------------------
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht.readHumidity();
  //讀取攝氏溫度
  float t1 = dht.readTemperature();
  //讀取華氏溫度
  float f1 = dht.readTemperature(true);
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT");
  } else {
    //濕度
    Serial.print("Humidity: ");
    Serial.print(h1);
    Serial.print(" %\t");
    //溫度
    Serial.print("Temperature: ");
    Serial.print(t1);
    Serial.println(" *C");
  }
  //溫濕度---------------------------------------------------------------

  //開關控制-土壤濕度-----------------------
  if (value_a0 > 20 && value_a0 < 600) {

    //判斷水位
    if (Liquid_level == 1) {

      digitalWrite(10, HIGH);

      //設定LCD要列印在第0個位置，第一列。
      mylcd.setCursor(0, 1);
      mylcd.print("SWITCH:");
      mylcd.setCursor(8, 1);
      mylcd.print("ON ");

    } else {
      digitalWrite(10, LOW);

      //設定LCD要列印在第0個位置，第一列。
      mylcd.setCursor(0, 1);
      mylcd.print("SWITCH:");
      mylcd.setCursor(8, 1);
      mylcd.print("OFF");
    }


  } else {


    digitalWrite(10, LOW);

    //設定LCD要列印在第0個位置，第一列。
    mylcd.setCursor(0, 1);
    mylcd.print("SWITCH:");
    mylcd.setCursor(8, 1);
    mylcd.print("OFF");

  }
  //開關控制-土壤濕度-----------------------

  digitalWrite(13, LOW);
  delay(300);
  digitalWrite(13, HIGH);
  delay(300);
  digitalWrite(13, LOW);

  //同步顯示在LCD與LED
  //設定LCD要列印在第0個位置，第0列。
  mylcd.setCursor(0, 0);
  mylcd.print("SM:");
  mylcd.setCursor(3, 0);
  mylcd.print("    ");
  mylcd.setCursor(3, 0);
  mylcd.print(value_a0);
  mylcd.setCursor(7, 0);
  mylcd.print("WT:");
  mylcd.setCursor(10, 0);
  mylcd.print(Liquid_level);
  Serial.println(value_a0);
  Serial.println(Liquid_level);

   //設定LCD要列印在第0個位置，第0列。
 mylcd.setCursor(12,0);
 mylcd.print("h:");
 mylcd.setCursor(14,0);
 mylcd.print(h1);
 mylcd.setCursor(12,1);
 mylcd.print("t:");
 mylcd.setCursor(14,1);
 mylcd.print(t1);


  delay(1000);

  //看門狗測試，如果秒數超過會重開機
  wdt_reset(); // reset watch dog timer

}


````


![](https://i.imgur.com/ZfXmapz.png)
![](https://i.imgur.com/qxaCRvZ.jpg)



---
網路版程式-先只有測土壤濕度

````
#include <Wire.h>
#include "motoWiFiEsp.h"
#include <SoftwareSerial.h>

//使用WiFiClient會用到
#include <motoMQTTClient.h>

SoftwareSerial esp8266_Serial(3,2);
WiFiEspClient esp_client;

int connect_status = WL_IDLE_STATUS;

String ipAddressToString(const IPAddress& ipAddress ) {
       return String(ipAddress[0]) + String(".")+
       String(ipAddress[1]) + String(".")+
       String(ipAddress[2]) + String(".")+
       String(ipAddress[3]);
}

String clientDataReceive( ) {
 String szMyData="";
 int j=0;
 int arr_count = 0;
 while(1) {
    if (esp_client.available()) {
        char c = esp_client.read();
       if (arr_count >= 179 && arr_count <= 184) {
       szMyData += c;
       }
        arr_count++;
    }else{
       delay(10);
       j++;
    }
    if(j>100)
       break;
  }
  Serial.println(szMyData);
  return szMyData;
}

//網址
String host = "iot.oceanbox.com.tw";
String code = "9d90b4f4815e5e196770a3e9aed4b6f5";
String user = "jzs2home";
String PHP_PATH = "/oceanduino/ledGet202001.php";

void messageReceived(String topic, String payload, char * bytes, unsigned int length)
{
}

void setup()
{
  //數位輸出13支腳，LED
  pinMode(13, OUTPUT);

  Serial.begin(9600); 
  
  //上網晶片序列
  esp8266_Serial.begin(9600);

  //濕度感測
  pinMode(A0, INPUT);

  //switch開關1
  pinMode(10, OUTPUT);

  esp8266_Serial.listen();
  WiFi.init(&esp8266_Serial); 
  
  if(WiFi.status()==WL_NO_SHIELD) {
   Serial.println(F("Esp8266 module no present"));
   while(true);
   }

   //如果沒連上網路，就會一直連線。
  while(connect_status != WL_CONNECTED) {
   Serial.println(F("Connect to router..."));
   
   //連結WIFI網路，並將結果存在connect_status變數中
   connect_status = WiFi.begin("Xperia 10_4026","t5275520");
  }
  
   Serial.println(WiFi.localIP());
  Serial.println(clientDataReceive());
  //換行
  Serial.println("connected");
  

}

int delay_time = 3;

//swtich signback1 跳回值
int switch_count1 = 0;
int signback = 0;
int signback_zero = 0;
//switch singback-------------------------------------------

void loop()
{
  
    Serial.print("Connecting to");

   //如果沒有連到這個網址
    if (!esp_client.connect("iot.oceanbox.com.tw",80)) {
    Serial.println("connection failed");
    delay(3000);
    //return;
    
    //relink
    Serial.println("connection relink");
     
    }

    //工作站
    int workstation = 1;

    //變數宣告
    int value_a0 = 1023 - analogRead(A0);    

   //送參數  
   //Serial.println("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&code=" + code + "&user=" + user + "&workstation=" + workstation + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
   esp_client.print("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&code=" + code + "&user=" + user + "&workstation=" + workstation + "&signback_zero=" + signback_zero + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

   //switch singback-------------------------------------------
   //帶signback_zero參數，送出後會將switch有關掉的寫回資料庫，寫入後，下面把signback_zero設為0
   //送signback_zero = 0就可以把sw1的控制設為0
   signback_zero = 0;
   //switch singback-------------------------------------------
   
   String web_get = clientDataReceive();
   Serial.println(web_get);
   Serial.println("sotped esp_client");

   Serial.print("value_a0:");
   Serial.println(value_a0);
   
   digitalWrite(13,LOW);
   delay(300);
   digitalWrite(13,HIGH);
   delay(300);
   digitalWrite(13,LOW);
   
  //同步顯示在LCD與LED

  //開關一判斷，加入雨量預測
  if (String(web_get[0]) == "1" && String(web_get[4]) == "0") {
   //開關一
   digitalWrite(10,HIGH);
   Serial.println("switch 1 open");
   }
   
   if (String(web_get[0]) == "0") {
   
   //開關一 D10
   digitalWrite(10,LOW);
   Serial.println("switch 1 close");
   }
  //開關一判斷-----------------------------------

//switch singback-------------------------------------------
   if (String(web_get[5]) == "1") {
    //5秒
    signback = 5;
   }
   if (String(web_get[5]) == "2") {
    //10秒
    signback = 10;
   }
   if (String(web_get[5]) == "3") {
    //30秒
    signback = 30;
   }
   if (String(web_get[5]) == "4") {
    //60秒
    signback = 60;
   }
   if (String(web_get[5]) == "5") {
    //300秒
    signback = 300;
   }
   if (String(web_get[5]) == "6") {
    //600秒
    signback = 600;
   }
   if (String(web_get[5]) == "7") {
    //1800秒
    signback = 1800;
   }
   if (String(web_get[5]) == "8") {
    //3600秒
    signback = 3600;
   }
   if (String(web_get[5]) == "9") {
    //7200秒
    signback = 7200;
   }
   
  //如果開關1為開且跳回功能有開，跳回計數+loog的秒數--------------------------------
  if (String(web_get[0]) == "1" && String(web_get[5]) != "0"){
  //累加loog的秒數
  //switch_count1 += delay_time/1000;
  switch_count1 += delay_time;
  Serial.println("switch_count1 and signback");
  Serial.println(switch_count1);
  Serial.println(signback);
  
  //如果累積的秒數 > 開關跳回的秒數，則switch1關閉
  if (switch_count1 > signback){
  //開關一 D10
   digitalWrite(10,LOW);
   switch_count1 = 0;
   signback = 0;
   signback_zero = 1;
  }

  }
    //如果開關1為開且跳回功能有開，跳回計數+loog的秒數--------------------------------
    
 //switch singback-------------------------------------------
 
   Serial.println(web_get[0]);
   Serial.println(web_get[1]);
   Serial.println(web_get[2]);
   Serial.println(web_get[3]);
   //下雨預測
   Serial.println(web_get[4]);
   //switch開關跳回
   Serial.println(web_get[5]);
    
   esp_client.stop(); 
   Serial.println("closing connection");
delay(1000);

}

````



---
腳位![](https://i.imgur.com/1uT9zbH.png)
