---
# System prepended metadata

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

---

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

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

板子換用Mega 2560壞掉再換回UNO

![](https://i.imgur.com/mmWYRX4.png)

SD買最小容量

我是DHT11-藍色

上傳要選Mega 2560

沒聯網可以變單機


程式碼:

````
#include <Wire.h>
#include "motoWiFiEsp.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);

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

long  H_Threshold;
String  message;

//SoftwareSerial esp8266_Serial(3,2);
//WiFiEspClient esp_client;

#define esp8266_Serial Serial3  //mega2560 VS  ESP8266   TX3(14 黃 8266_RX)、RX3(15 綠 8266_TX) VS  TX、RX
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/ledGet.php";
String PHP_PATH = "/oceanduino/ledGet202001.php";

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

//wifi判斷用
int wifi_here =0;
//判斷水位用
int Liquid_level=0;
void setup()
{
  //數位輸出13支腳，LED
  pinMode(13, OUTPUT);

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

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

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

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

    H_Threshold = 100;
  message = "";

  
  //esp8266_Serial.listen();
  WiFi.init(&esp8266_Serial); 
  
  if(WiFi.status()==WL_NO_SHIELD) {
   Serial.println(F("Esp8266 module no present"));
   //關掉，不燃8266移除會一直TIMEOUT
   //while(true);
   }

   //如果沒連上網路，就會一直連線，直到wifi_here = 3。
   //回傳WL_CONNECTED表已連上WiFi AP
  while(connect_status != WL_CONNECTED && wifi_here < 3) {
  //while(connect_status != WL_CONNECTED) {
   Serial.println(F("Connect to router..."));
   
   //連結WIFI網路，並將結果存在connect_status變數中
   connect_status = WiFi.begin("Xperia 10_4026","t5275520");
   //連線一次，+1 (連第三次後 wifi_here = 3
   wifi_here++;
   Serial.print("wifi_here");
   Serial.println(wifi_here);
  }
  
//小於三代表連線成功------------------------------------
  
   Serial.println(WiFi.localIP());
  
   //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  
  //-------------------------------------------------------------
  
}

//delay_time_count每次加1，如果delay_time = 設定的秒數，將數據上網
int delay_time = 3;
int delay_time_count = 0;

//預設io 0 的模式
String io_mode0 = "0";

//switch singback-------------------------------------------
//swtich跳回計數1~4
int switch_count1 = 0;

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

//自動重抓wifi，變數
int wifiget = 0;
void loop()
{

//設定LCD要列印在第0個位置，第一列。
/*
  mylcd.setCursor(8,0);
  mylcd.print("        ");
*/
//自動重抓wifi，變數加1
  wifiget += 1;
  
   //變數宣告
    int value_a0 = 1023 - analogRead(A0);
    //int value_a0 = analogRead(A0);

    Liquid_level=digitalRead(5);
   Serial.print("水位訊號：");
   Serial.println(Liquid_level);

//如果網路控制1沒有打開，就自動判斷   
Serial.print("io_mode0 :");
Serial.println(String(io_mode0));

//網路沒手動打開時-----------------------
if (String(io_mode0) == "0") {
//開關控制-土壤濕度,有插在土上才作動
if (value_a0 > 20 && value_a0 < 450) {

  //判斷水位有水狀態才能打開
  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");
 */
}
//開關控制-土壤濕度-----------------------
}
//網路沒手動打開時-----------------------

   //小於三代表連線成功
    if (wifi_here < 3) {
      
  //delay_time_count每次加1，如果delay_time = 設定的秒數，將數據上網
   //送參數  
   delay_time_count++;
   Serial.print("delay_time_count:");
   Serial.println(delay_time_count);
   if (delay_time_count == delay_time) {

   //設定LCD要列印在第0個位置，第一列。
   /*
  mylcd.setCursor(8,0);
  mylcd.print("        ");
  mylcd.setCursor(8,0);
  mylcd.print("UP data");
  */
    Serial.print("Connecting to");
    
   //如果沒有連到這個網址
    if (!esp_client.connect("iot.oceanbox.com.tw",80)) {
    Serial.println("connection failed");

    //自動重抓wifi，如果連線後又中斷wifi，變數修正
        wifi_here++;
        wifiget = 0;
        
    delay(3000);
    //return;
    
    //relink
    Serial.println("connection relink");
     
    }

    //工作站
    int workstation = 1;

    
    
   //送參數  
   //加入signback_zero
   Serial.println("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + Liquid_level + "&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");
   
   esp_client.print("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + Liquid_level + "&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;
   //switch singback-------------------------------------------
   String web_get = clientDataReceive();
   Serial.println(web_get);
   //web_get[0]
   //web_get[1]
   //web_get[2]
   //web_get[3]
   Serial.println("sotped esp_client");
   
   //數據送出，計數歸零
   delay_time_count = 0;
   
   //開關一判斷
  //如果狀態不同
  if (String(io_mode0) != String(web_get[0])) {

  if (String(web_get[0]) == "1") {
   //開關一
   digitalWrite(10,HIGH);
   Serial.println("switch 1 open");
   //設定LCD要列印在第0個位置，第一列。
   /*
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("ON ");
  */
  delay(1000);
   }
  
   if (String(web_get[0]) == "0") {
   
   //開關一 D10
   digitalWrite(10,LOW);
   Serial.println("switch 1 close");
   //設定LCD要列印在第0個位置，第一列。
   /*
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("OFF");
  */
   }
   
  io_mode0 =String(web_get[0]);
  }

  //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;
  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;
   //設定LCD要列印在第0個位置，第一列。
   /*
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("OFF");
  */
  }

  }
    //如果開關1為開且跳回功能有開，跳回計數+loog的秒數--------------------------------
    
 //switch singback-------------------------------------------
    
  //開關一判斷-----------------------------------
   //switch開關1~4
   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");

Serial.println(web_get[4]);
   }
   }

   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);
*/
//等於三代表連線失敗
//如果沒有wifi----------------------------------
  if (wifi_here == 3) {
  Serial.print("wifiget:");
  Serial.println(wifiget);
       //自動重抓wifi，600 loop 600次約10分，重抓一次wifi--------------------------------------------
      if (wifiget > 600) {
      wifiget = 0;
      wifi_here = 1;
      while (connect_status != WL_CONNECTED && wifi_here < 2) {
        Serial.println(F("Connect to router..."));
        connect_status = WiFi.begin("oceanbox", "28131989");
        wifi_here++;
        Serial.print("wifi_here");
        Serial.println(wifi_here);
      }
      }
      //自動重抓wifi------------------------------------------------
         
  }
//如果沒有wifi----------------------------------
//Serial.print("wifi_here1:");
//Serial.println(wifi_here);


delay(1000);

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

````

讓LCD顯示-程式碼
````
#include <Wire.h>
#include "motoWiFiEsp.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);

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

long  H_Threshold;
String  message;

//SoftwareSerial esp8266_Serial(3,2);
//WiFiEspClient esp_client;

#define esp8266_Serial Serial3  //mega2560 VS  ESP8266   TX3(14 黃 8266_RX)、RX3(15 綠 8266_TX) VS  TX、RX
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/ledGet.php";
String PHP_PATH = "/oceanduino/ledGet202001.php";

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

//wifi判斷用
int wifi_here =0;
//判斷水位用
int Liquid_level=0;
void setup()
{
  //數位輸出13支腳，LED
  pinMode(13, OUTPUT);

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

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

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

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

    H_Threshold = 100;
  message = "";

  
  //esp8266_Serial.listen();
  WiFi.init(&esp8266_Serial); 
  
  if(WiFi.status()==WL_NO_SHIELD) {
   Serial.println(F("Esp8266 module no present"));
   //關掉，不燃8266移除會一直TIMEOUT
   //while(true);
   }

   //如果沒連上網路，就會一直連線，直到wifi_here = 3。
   //回傳WL_CONNECTED表已連上WiFi AP
  while(connect_status != WL_CONNECTED && wifi_here < 3) {
  //while(connect_status != WL_CONNECTED) {
   Serial.println(F("Connect to router..."));
   
   //連結WIFI網路，並將結果存在connect_status變數中
   connect_status = WiFi.begin("Xperia 10_4026","t5275520");
   //連線一次，+1 (連第三次後 wifi_here = 3
   wifi_here++;
   Serial.print("wifi_here");
   Serial.println(wifi_here);
  }
  
//小於三代表連線成功------------------------------------
  
   Serial.println(WiFi.localIP());
  
   //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  
  //-------------------------------------------------------------
  
}

//delay_time_count每次加1，如果delay_time = 設定的秒數，將數據上網
int delay_time = 3;
int delay_time_count = 0;

//預設io 0 的模式
String io_mode0 = "0";

//switch singback-------------------------------------------
//swtich跳回計數1~4
int switch_count1 = 0;

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

//自動重抓wifi，變數
int wifiget = 0;
void loop()
{

//設定LCD要列印在第0個位置，第一列。

  mylcd.setCursor(8,0);
  mylcd.print("        ");

//自動重抓wifi，變數加1
  wifiget += 1;
  
   //變數宣告
    int value_a0 = 1023 - analogRead(A0);
    //int value_a0 = analogRead(A0);

    Liquid_level=digitalRead(5);
   Serial.print("水位訊號：");
   Serial.println(Liquid_level);

//如果網路控制1沒有打開，就自動判斷   
Serial.print("io_mode0 :");
Serial.println(String(io_mode0));

//網路沒手動打開時-----------------------
if (String(io_mode0) == "0") {
//開關控制-土壤濕度,有插在土上才作動
if (value_a0 > 20 && value_a0 < 450) {

  //判斷水位有水狀態才能打開
  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");
 
}
//開關控制-土壤濕度-----------------------
}
//網路沒手動打開時-----------------------

   //小於三代表連線成功
    if (wifi_here < 3) {
      
  //delay_time_count每次加1，如果delay_time = 設定的秒數，將數據上網
   //送參數  
   delay_time_count++;
   Serial.print("delay_time_count:");
   Serial.println(delay_time_count);
   if (delay_time_count == delay_time) {

   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(8,0);
  mylcd.print("        ");
  mylcd.setCursor(8,0);
  mylcd.print("UP data");
  
    Serial.print("Connecting to");
    
   //如果沒有連到這個網址
    if (!esp_client.connect("iot.oceanbox.com.tw",80)) {
    Serial.println("connection failed");

    //自動重抓wifi，如果連線後又中斷wifi，變數修正
        wifi_here++;
        wifiget = 0;
        
    delay(3000);
    //return;
    
    //relink
    Serial.println("connection relink");
     
    }

    //工作站
    int workstation = 1;

    
    
   //送參數  
   //加入signback_zero
   Serial.println("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + Liquid_level + "&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");
   
   esp_client.print("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + Liquid_level + "&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;
   //switch singback-------------------------------------------
   String web_get = clientDataReceive();
   Serial.println(web_get);
   //web_get[0]
   //web_get[1]
   //web_get[2]
   //web_get[3]
   Serial.println("sotped esp_client");
   
   //數據送出，計數歸零
   delay_time_count = 0;
   
   //開關一判斷
  //如果狀態不同
  if (String(io_mode0) != String(web_get[0])) {

  if (String(web_get[0]) == "1") {
   //開關一
   digitalWrite(10,HIGH);
   Serial.println("switch 1 open");
   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("ON ");
  
  delay(1000);
   }
  
   if (String(web_get[0]) == "0") {
   
   //開關一 D10
   digitalWrite(10,LOW);
   Serial.println("switch 1 close");
   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("OFF");
  
   }
   
  io_mode0 =String(web_get[0]);
  }

  //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;
  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;
   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("OFF");
  
  }

  }
    //如果開關1為開且跳回功能有開，跳回計數+loog的秒數--------------------------------
    
 //switch singback-------------------------------------------
    
  //開關一判斷-----------------------------------
   //switch開關1~4
   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");

Serial.println(web_get[4]);
   }
   }

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

//等於三代表連線失敗
//如果沒有wifi----------------------------------
  if (wifi_here == 3) {
  Serial.print("wifiget:");
  Serial.println(wifiget);
       //自動重抓wifi，600 loop 600次約10分，重抓一次wifi--------------------------------------------
      if (wifiget > 600) {
      wifiget = 0;
      wifi_here = 1;
      while (connect_status != WL_CONNECTED && wifi_here < 2) {
        Serial.println(F("Connect to router..."));
        connect_status = WiFi.begin("oceanbox", "28131989");
        wifi_here++;
        Serial.print("wifi_here");
        Serial.println(wifi_here);
      }
      }
      //自動重抓wifi------------------------------------------------
         
  }
//如果沒有wifi----------------------------------
//Serial.print("wifi_here1:");
//Serial.println(wifi_here);


delay(1000);

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

````
再加入溫濕度

````
#include <Wire.h>
#include "motoWiFiEsp.h"
#include <SoftwareSerial.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);

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

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

long  H_Threshold;
String  message;

//SoftwareSerial esp8266_Serial(3,2);
//WiFiEspClient esp_client;

#define esp8266_Serial Serial3  //mega2560 VS  ESP8266   TX3(14 黃 8266_RX)、RX3(15 綠 8266_TX) VS  TX、RX
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/ledGet.php";
String PHP_PATH = "/oceanduino/ledGet202001.php";

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

//wifi判斷用
int wifi_here =0;
//判斷水位用
int Liquid_level=0;
void setup()
{
 //溫濕度
 //Serial.begin(9600);
 Serial.println("DHT11 01");
 dht.begin();
  
  //數位輸出13支腳，LED
  pinMode(13, OUTPUT);

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

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

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

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

    H_Threshold = 100;
  message = "";

  
  //esp8266_Serial.listen();
  WiFi.init(&esp8266_Serial); 
  
  if(WiFi.status()==WL_NO_SHIELD) {
   Serial.println(F("Esp8266 module no present"));
   //關掉，不燃8266移除會一直TIMEOUT
   //while(true);
   }

   //如果沒連上網路，就會一直連線，直到wifi_here = 3。
   //回傳WL_CONNECTED表已連上WiFi AP
  while(connect_status != WL_CONNECTED && wifi_here < 3) {
  //while(connect_status != WL_CONNECTED) {
   Serial.println(F("Connect to router..."));
   
   //連結WIFI網路，並將結果存在connect_status變數中
   connect_status = WiFi.begin("Xperia 10_4026","t5275520");
   //連線一次，+1 (連第三次後 wifi_here = 3
   wifi_here++;
   Serial.print("wifi_here");
   Serial.println(wifi_here);
  }
  
//小於三代表連線成功------------------------------------
  
   Serial.println(WiFi.localIP());
  
   //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  
  //-------------------------------------------------------------
  
}

//delay_time_count每次加1，如果delay_time = 設定的秒數，將數據上網
int delay_time = 3;
int delay_time_count = 0;

//預設io 0 的模式
String io_mode0 = "0";

//switch singback-------------------------------------------
//swtich跳回計數1~4
int switch_count1 = 0;

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

//自動重抓wifi，變數
int wifiget = 0;
void loop()
{
//溫濕度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");
 }


//設定LCD要列印在第0個位置，第一列。

  mylcd.setCursor(8,0);
  mylcd.print("        ");

//自動重抓wifi，變數加1
  wifiget += 1;
  
   //變數宣告
    int value_a0 = 1023 - analogRead(A0);
    //int value_a0 = analogRead(A0);

    Liquid_level=digitalRead(5);
   Serial.print("水位訊號：");
   Serial.println(Liquid_level);

//如果網路控制1沒有打開，就自動判斷   
Serial.print("io_mode0 :");
Serial.println(String(io_mode0));

//網路沒手動打開時-----------------------
if (String(io_mode0) == "0") {
//開關控制-土壤濕度,有插在土上才作動
if (value_a0 > 20 && value_a0 < 450) {

  //判斷水位有水狀態才能打開
  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");
 
}
//開關控制-土壤濕度-----------------------
}
//網路沒手動打開時-----------------------

   //小於三代表連線成功
    if (wifi_here < 3) {
      
  //delay_time_count每次加1，如果delay_time = 設定的秒數，將數據上網
   //送參數  
   delay_time_count++;
   Serial.print("delay_time_count:");
   Serial.println(delay_time_count);
   if (delay_time_count == delay_time) {

   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(8,0);
  mylcd.print("        ");
  mylcd.setCursor(8,0);
  mylcd.print("UP data");
  
    Serial.print("Connecting to");
    
   //如果沒有連到這個網址
    if (!esp_client.connect("iot.oceanbox.com.tw",80)) {
    Serial.println("connection failed");

    //自動重抓wifi，如果連線後又中斷wifi，變數修正
        wifi_here++;
        wifiget = 0;
        
    delay(3000);
    //return;
    
    //relink
    Serial.println("connection relink");
     
    }

    //工作站
    int workstation = 1;

    
    
   //送參數  
   //加入signback_zero
   Serial.println("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + Liquid_level + "&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");
   
   esp_client.print("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + Liquid_level + "&sing3=" + h1 + "&sing4=" + t1 + "&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;
   //switch singback-------------------------------------------
   String web_get = clientDataReceive();
   Serial.println(web_get);
   //web_get[0]
   //web_get[1]
   //web_get[2]
   //web_get[3]
   Serial.println("sotped esp_client");
   
   //數據送出，計數歸零
   delay_time_count = 0;
   
   //開關一判斷
  //如果狀態不同
  if (String(io_mode0) != String(web_get[0])) {

  if (String(web_get[0]) == "1") {
   //開關一
   digitalWrite(10,HIGH);
   Serial.println("switch 1 open");
   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("ON ");
  
  delay(1000);
   }
  
   if (String(web_get[0]) == "0") {
   
   //開關一 D10
   digitalWrite(10,LOW);
   Serial.println("switch 1 close");
   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("OFF");
  
   }
   
  io_mode0 =String(web_get[0]);
  }

  //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;
  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;
   //設定LCD要列印在第0個位置，第一列。
   
  mylcd.setCursor(0,1);
  mylcd.print("SWITCH:");
  mylcd.setCursor(8,1);
  mylcd.print("OFF");
  
  }

  }
    //如果開關1為開且跳回功能有開，跳回計數+loog的秒數--------------------------------
    
 //switch singback-------------------------------------------
    
  //開關一判斷-----------------------------------
   //switch開關1~4
   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");

Serial.println(web_get[4]);
   }
   }

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

//等於三代表連線失敗
//如果沒有wifi----------------------------------
  if (wifi_here == 3) {
  Serial.print("wifiget:");
  Serial.println(wifiget);
       //自動重抓wifi，600 loop 600次約10分，重抓一次wifi--------------------------------------------
      if (wifiget > 600) {
      wifiget = 0;
      wifi_here = 1;
      while (connect_status != WL_CONNECTED && wifi_here < 2) {
        Serial.println(F("Connect to router..."));
        connect_status = WiFi.begin("oceanbox", "28131989");
        wifi_here++;
        Serial.print("wifi_here");
        Serial.println(wifi_here);
      }
      }
      //自動重抓wifi------------------------------------------------
         
  }
//如果沒有wifi----------------------------------
//Serial.print("wifi_here1:");
//Serial.println(wifi_here);


delay(1000);

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

 //設定LCD要列印在第0個位置，第0列。
 mylcd.setCursor(0,0);
 mylcd.print("h1:");
 mylcd.setCursor(3,0);
 mylcd.print(h1);

  mylcd.setCursor(8,0);
 mylcd.print("t1:");
 mylcd.setCursor(12,0);
 mylcd.print(t1);
 
}


````

![](https://i.imgur.com/litCKXG.png)
![](https://i.imgur.com/bkmf8Yz.png)
