---
# System prepended metadata

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

---

---
title: 農業科技2-暗渠排水
tags: 農業科技
---
裝switch開關-有過會有 "ㄆㄧㄚˋ" 


DC+接VCC
DC-接GND

D10接NI

CODE要動要加入函式庫-草稿-匯入函式庫ZIP檔
感知器接觸不良濕度就會沒反應

````#include <SoftwareSerial.h>
#include <Wire.h>
#include <avr/wdt.h>
//lcd需要的涵數
#include <motoLiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
void setup()
{
 Serial.begin(9600);
 pinMode(13, OUTPUT);
//濕度感測
 pinMode(A0, INPUT);
 //switch 開關 1
 pinMode(10, OUTPUT);
 //lcd
 mylcd.init();
 mylcd.backlight();
}
/*循環區*/
void loop()
{
 digitalWrite(13,HIGH);
 Serial.println("on");
 Serial.println(analogRead(A0));

  int value_a0 = 1023 - analogRead(A0);
 Serial.println(value_a0);
 //設定LCD要列印在第0個位置，第0列。
 mylcd.setCursor(0,0);
 mylcd.print("SM:");
 mylcd.setCursor(4,0);
 mylcd.print(value_a0);
if (value_a0 > 30 && value_a0 < 450) {
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 ");
}
 digitalWrite(13,LOW);
 //Serial.println("off");
 delay(1000);
}


````

剪下來的電-正接正(紅)-com/負接負(黑)

![](https://i.imgur.com/BHPgytp.jpg)


棕-正極-5-24V 黃OUT=信號線 藍=GND=負極

土壤溼度要滿足/開水
````#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);

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);
  
   //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);
   
//開關控制-土壤濕度-----------------------
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(8,0);
  mylcd.print("WT:");
  mylcd.setCursor(11,0);
  mylcd.print(Liquid_level);
  Serial.println(value_a0);
  Serial.println(Liquid_level);

delay(1000);

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

````
wdt_enable(WDTO_8S);      //當機8秒後重開機


空氣溫溼度感知器
````#include <SoftwareSerial.h>
#include <Wire.h>
#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 02 D10
#define DHTPIN2 10 // what pin we're connected to
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22
DHT dht2(DHTPIN2, DHTTYPE);
//DHT 03 D11
#define DHTPIN3 11 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht3(DHTPIN3, DHTTYPE);

//lcd需要的涵數
#include <motoLiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
/* LED 閃爍，延遲 1 秒 */
/*宣告區*/
void setup()
{
 //在9600 bps開啟串列傳輸 erial.println 要可用需先加下列這段
 Serial.begin(9600);
 pinMode(13, OUTPUT); //設定第 13 pin 腳模式為輸出
 //溫濕度
 //Serial.begin(9600);
 Serial.println("DHT11 01");
 dht.begin();
 //lcd
 mylcd.init();
 mylcd.backlight();
}
/*循環區*/
void loop()
{

 digitalWrite(13,HIGH); //給數位腳位 13 pin 輸出高電壓 (LED 通電就亮)
 Serial.println("on");
 delay(2000); //延遲 1 秒鐘 (1000 毫秒)
 //溫濕度1
 // 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");
 }

  //溫濕度2
 float h2 = dht2.readHumidity();
 //讀取攝氏溫度
 float t2 = dht2.readTemperature();
 //讀取華氏溫度
 float f2 = dht2.readTemperature(true);
 if (isnan(t2) || isnan(h2)) {
 Serial.println("Failed to read from DHT");
 } else {
 //濕度
 Serial.print("Humidity: ");
 Serial.print(h2);
 Serial.print(" %\t");
 //溫度
 Serial.print("Temperature: ");
 Serial.print(t2);
 Serial.println(" *C");
 }
 //溫濕度3
 float h3 = dht3.readHumidity();
 //讀取攝氏溫度
 float t3 = dht3.readTemperature();
 //讀取華氏溫度
 float f3 = dht3.readTemperature(true);
 if (isnan(t3) || isnan(h3)) {
 Serial.println("Failed to read from DHT");
 } else {
 //濕度
 Serial.print("Humidity: ");
 Serial.print(h3);
 Serial.print(" %\t");
 //溫度
 Serial.print("Temperature: ");
 Serial.print(t3);
 Serial.println(" *C");
 }
 //設定LCD要列印在第0個位置，第0列。
 mylcd.setCursor(0,0);
 mylcd.print("h1:");
 mylcd.setCursor(3,0);
 mylcd.print(h1);
 mylcd.setCursor(0,1);
 mylcd.print("t1:");
 mylcd.setCursor(3,1);
 mylcd.print(t1);
 //設定LCD要列印
 mylcd.setCursor(9,0);
 mylcd.print("t2:");
 mylcd.setCursor(12,0);
 mylcd.print(t2);
 //設定LCD要列印
 mylcd.setCursor(9,1);
 mylcd.print("t3:");
 mylcd.setCursor(12,1);
 mylcd.print(t3);
 digitalWrite(13,LOW); //給數位腳位 13 pin 輸出低電壓 (LED 不通電就暗)
 //Serial.println("off");
 delay(1000);
}

````

只顯示一個感知器時的寫法

````#include <Wire.h>
#include <SoftwareSerial.h>#include "motoDHT.h"
//DHT 01 D9
#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);

//看門狗測試，如果秒數超過會重開機
#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);

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);

  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);
   
//開關控制-土壤濕度-----------------------
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(8,0);
  mylcd.print("WT:");
  mylcd.setCursor(11,0);
  mylcd.print(Liquid_level);
  Serial.println(value_a0);
  Serial.println(Liquid_level);

//溫濕度1
 // 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");
 }

 mylcd.setCursor(0,0);
 mylcd.print("h1:");
 mylcd.setCursor(3,0);
 mylcd.print(h1);

 mylcd.setCursor(0,1);
 mylcd.print("t1:");
 mylcd.setCursor(3,1);
 mylcd.print(t1);

delay(1000);

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

````
連網https://iot.cht.com.tw/iot/login

ESP8266-只有wifi功能

電話線：
紅：VCC
黑：GND
黃：D2   RX
綠：D3   TX
//mega2560 VS  ESP8266   TX3(14 黃 8266_RX)、RX3(15 綠 8266_TX) VS  TX、RX

設定帳號+密碼
connect_status = WiFi.begin("帳號","密碼");

````//載入所需的涵式庫
#include <SoftwareSerial.h>
//---------------------------------------------------------------------------------------------------add
#include <Wire.h>
#include "motoWiFiEsp.h"
#include <motoMQTTClient.h>
//設定 WIFI 晶片使用 D2 D3 的數位腳位
SoftwareSerial esp8266_Serial(3,2); //D2 RX / D3 TX
WiFiEspClient esp_client;
int connect_status = WL_IDLE_STATUS;
//回傳 IP 位址的陣列，組成字串 ipAddressToString
String ipAddressToString(const IPAddress& ipAddress ) {
 return String(ipAddress[0]) + String(".")+
 String(ipAddress[1]) + String(".")+
 String(ipAddress[2]) + String(".")+
 String(ipAddress[3]);
}
MQTTClient mqtt_client;
void messageReceived(String topic, String payload, char * bytes, unsigned int length)
{
}
//---------------------------------------------------------------------------------------------------end
void setup()
{
 //在9600 bps開啟串列傳輸 erial.println 要可用需先加下列這段
 Serial.begin(9600);
//---------------------------------------------------------------------------------------------------add
 //Baud Rate: 9600bps (出廠預設)
 esp8266_Serial.begin(9600);
 esp8266_Serial.listen();
 WiFi.init(&esp8266_Serial);
 if(WiFi.status()==WL_NO_SHIELD) {
 Serial.println(F("Esp8266 module no present"));
 while(true);
 }
//連接 wifi ，重複處理直到連線成功。
 while(connect_status != WL_CONNECTED) {
 Serial.println(F("Connect to router..."));
 //設定wifi的帳密
 connect_status = WiFi.begin("oceanbox","28131989");
 }
//MQTT 伺服器連線，連到中華電信 IOT
 mqtt_client.begin("iot.cht.com.tw", esp_client);
//建立與中華電信的 MQTT 的連線，重複處理直到連線成功。
//客戶ID:oceanbox (任意填寫)
//帳號與密碼：PK3CTEM7KG2FYC439C為專案的金鑰(找金鑰設定)
 while (!mqtt_client.connect("oceanbox","PK3CTEM7KG2FYC439C","PK3CTEM7KG2FYC439C")) {
 Serial.print(".");
 delay(1000);
 }
 Serial.println("connected");
//---------------------------------------------------------------------------------------------------end

 pinMode(13, OUTPUT); //設定第 13 pin 腳模式為輸出
 //濕度感測
 pinMode(A0, INPUT);
 //switch 開關 1
 pinMode(10, OUTPUT);
}
/*循環區*/
void loop()
{
 digitalWrite(13,HIGH); //給數位腳位 13 pin 輸出高電壓 (LED 通電就亮)
 Serial.println("LED HIGH");
 delay(1000); //延遲 1 秒鐘 (1000 毫秒)
 //因為預設為1023，愈濕會愈低，改成濕度愈高，數值愈高
 //先定義變數value_a0為整數
 int value_a0 = 1023 - analogRead(A0);
 //連上hinet
 mqtt_client.loop();
 //發佈下列訊號給 CLIENT 端
 //7781729950為設備編號、value_a0為要上傳的訊號
 mqtt_client.publish("/v1/device/7781729950/rawdata", (String("[{\"id\":\"sensor1\",\"value\":[\"") + String(value_a0) +
String("\"]}]")));
 //印出目前的上網ip
 Serial.println(ipAddressToString(WiFi.localIP()));
 Serial.println(value_a0);
 //如果濕度 > 50 就啟動SWITCH
 if (value_a0 > 50) {
digitalWrite(10,HIGH);
}else {
digitalWrite(10,LOW);
}
 digitalWrite(13,LOW); //給數位腳位 13 pin 輸出低電壓 (LED 不通電就暗)
 Serial.println("LED LOW");
 delay(1000);
}


````