---
# System prepended metadata

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

---

---
title: 農業科技4-暗渠排水
tags: 農業科技
---
插錯電源燒壞的插頭



到這裡看數據
https://web.oceanbox.com.tw/iot_list-9d90b4f4815e5e196770a3e9aed4b6f5-jzs2home-1.html

感知器起始應該是0不然就是線接錯(會燒掉)
螢幕沒亮就是線沒插

---
新增溫濕度感知器#define DHTTYPE DHT11 // DHT 11

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

改成
esp_client.print("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + t1+ "&sing3=" + h1 +"&user=" + user + "&workstation=" + workstation + "&signback_zero=" + signback_zero + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
   

---
ardunio按ctrl+a就是全選



可用記憶體低下，可能會出現穩定性問題
嘗試把LCD的地方關閉


---
https://web.oceanbox.com.tw/member-edit.php
可以設定是否mail通知

結果卡在網路的問題...會跳

如果設定打開開關不會自動跳回
再按遠端遙控把他關掉

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


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

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

//LiquidCrystal_I2C mylcd(0x27,16,2);

//使用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)
{
}

//wifi判斷用
int wifi_here =0;

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

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

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

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

 dht.begin();
 
  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());

   //小於三代表連線成功
   /*
   if (wifi_here < 3) {
  Serial.println(clientDataReceive());
  //換行
  Serial.println("connected");
   }else {
   Serial.println("no wifi");
   }
   */
   
   //lcd
 //mylcd.init();
 //mylcd.backlight();
    
}

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

//預設io 0 的模式
int io_mode0 = 0;

//switch singback-------------------------------------------
//swtich跳回計數1~4
int switch_count1 = 0;
/*
int switch_count2 = 0;
int switch_count3 = 0;
int switch_count4 = 0;
*/
//swtich signback1 跳回值
int signback = 0;
int signback_zero = 0;
//switch singback-------------------------------------------
void loop()
{

//設定LCD要列印在第0個位置，第一列。
  //mylcd.setCursor(8,0);
  //mylcd.print("        ");
  
   //變數宣告
    int value_a0 = 1023 - analogRead(A0);
    //int value_a0 = analogRead(A0);

     //溫濕度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();

   //小於三代表連線成功
    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");
    
    //設定LCD要列印在第0個位置，第一列。
  //mylcd.setCursor(8,0);
  //mylcd.print("        ");
  //mylcd.setCursor(8,0);
  //mylcd.print("UP data");

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

    //工作站
    int workstation = 1;

   //送參數  
   //加入signback_zero
   esp_client.print("GET "+ PHP_PATH + "?sing1=" + value_a0 + "&sing2=" + h1 + "&sing3=" + 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);
   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 "); 
   }
  
   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 =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/1000;
  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]);
//更新頻率
  //int delay_time = 3000;

  if (String(web_get[4]) == "1") {
  //3秒
  delay_time = 3;
  Serial.println("delay1");
  Serial.println(delay_time);
}
if (String(web_get[4]) == "2") {
  //10秒
    delay_time = 10;
    Serial.println("delay2");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "3") {
  //15秒
    delay_time = 15;
    Serial.println("delay3");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "4") {
  //30秒
    delay_time = 30;
    Serial.println("delay4");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "5") {
  //1分
    delay_time = 60;
    Serial.println("delay5");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "6") {
    //2分
    delay_time = 120;
    Serial.println("delay6");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "7") {
    //5分
    delay_time = 300;
    Serial.println("delay7");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "8") {
    //10分
    delay_time = 600;
    Serial.println("delay8");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "9") {
    //15分
    delay_time = 900;
    Serial.println("delay9");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "10") {
    //20分
    delay_time = 1200;
    Serial.println("delay10");
    Serial.println(delay_time);
}
if (String(web_get[4]) == "11") {
    //30分
    delay_time = 1800;
    Serial.println("delay11");
    Serial.println(delay_time);
}
   }
   }

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

if (value_a0 > 50 && 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");
 
}
    
  }
//如果沒有wifi----------------------------------
//Serial.print("wifi_here1:");
//Serial.println(wifi_here);


delay(1000);
 
}




````

