---
# System prepended metadata

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

---

---
title: 農業科技3-暗渠排水
tags: 農業科技
---
````//載入所需的涵式庫
#include <SoftwareSerial.h>
//---------------------------------------------------------------------------------------------------add
#include <Wire.h>
#include "motoWiFiEsp.h"
#include <motoMQTTClient.h>

//設定 WIFI 晶片使用 D2 D3 的數位腳位 D2   RX  /  D3   TX
SoftwareSerial esp8266_Serial(3,2);

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("Xperia 10_4026","t5275520");
 }

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


````
![](https://i.imgur.com/jTNYfCC.png)
中華電信連網https://iot.cht.com.tw/iot/login
密碼:love+阿罵家電話
![](https://i.imgur.com/eqt2z4Z.png)

物聯網用中華電信MQTT丟-精簡
http://-老師

未來目標NB-IOT 一個月10元

CODE裡面要有專案金鑰
//帳號與密碼：PK3CTEM7KG2FYC439C為專案的金鑰(找金鑰設定)
 while (!mqtt_client.connect("huang","","")) 

````

````

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

要自己放設備編號
![](https://i.imgur.com/yxCQjEp.png)

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

感知器
![](https://i.imgur.com/IzowGLJ.png)
![](https://i.imgur.com/yZNoATV.png)

看到感知器在變動
![](https://i.imgur.com/VVtObFD.png)


````
//載入所需的涵式庫
#include <SoftwareSerial.h>
//---------------------------------------------------------------------------------------------------add
#include <Wire.h>
#include "motoWiFiEsp.h"
#include <motoMQTTClient.h>

//設定 WIFI 晶片使用 D2 D3 的數位腳位 D2   RX  /  D3   TX
SoftwareSerial esp8266_Serial(3,2);

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("Xperia 10_4026","t5275520");
 }

//MQTT 伺服器連線，連到中華電信 IOT
 mqtt_client.begin("iot.cht.com.tw", esp_client);
//建立與中華電信的 MQTT 的連線，重複處理直到連線成功。
//客戶ID:oceanbox  (任意填寫)
//帳號與密碼：PK3CTEM7KG2FYC439C為專案的金鑰(找金鑰設定)
 while (!mqtt_client.connect("huang","PKP3PW3EH99Z1MM2HK","PKP3PW3EH99Z1MM2HK")) {
 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/25091449711/rawdata", (String("[{\"id\":\"sensora01\",\"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);
}

````

![](https://i.imgur.com/x55QX4D.png)
中華電信開發者
https://iot.cht.com.tw/iot/developer/api

加入有放溫濕度的code
````
#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);
````

然後再複製
````
mqtt_client.publish("/v1/device/25091449711/rawdata", (String("[{\"id\":\"sensora01\",\"value\":[\"") + String(value_a0) +
String("\"]}]")));

````
連2個感知器

````
//載入所需的涵式庫
#include <SoftwareSerial.h>
//---------------------------------------------------------------------------------------------------add
#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);


#include "motoWiFiEsp.h"
#include <motoMQTTClient.h>

//設定 WIFI 晶片使用 D2 D3 的數位腳位 D2   RX  /  D3   TX
SoftwareSerial esp8266_Serial(3,2);

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("Xperia 10_4026","t5275520");
 }

//MQTT 伺服器連線，連到中華電信 IOT
 mqtt_client.begin("iot.cht.com.tw", esp_client);
//建立與中華電信的 MQTT 的連線，重複處理直到連線成功。
//客戶ID:oceanbox  (任意填寫)
//帳號與密碼：PK3CTEM7KG2FYC439C為專案的金鑰(找金鑰設定)
 while (!mqtt_client.connect("huang","PKP3PW3EH99Z1MM2HK","PKP3PW3EH99Z1MM2HK")) {
 Serial.print(".");
 delay(1000);
 }
 Serial.println("connected");
//---------------------------------------------------------------------------------------------------end

 pinMode(13, OUTPUT); //設定第 13 pin 腳模式為輸出

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

  //switch 開關 1
 pinMode(10, OUTPUT);
  //溫濕度
 //Serial.begin(9600);
 Serial.println("DHT11 01");
 dht.begin();
}

/*循環區*/
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);

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

 //連上hinet
  mqtt_client.loop();
  //發佈下列訊號給 CLIENT 端
  //7781729950為設備編號、value_a0為要上傳的訊號
 mqtt_client.publish("/v1/device/25091449711/rawdata", (String("[{\"id\":\"sensora01\",\"value\":[\"") + String(value_a0) +
String("\"]}]")));

mqtt_client.publish("/v1/device/25091449711/rawdata", (String("[{\"id\":\"sensora02\",\"value\":[\"") + String(h1) +
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);
}


````

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

中華電信上看
![](https://i.imgur.com/G38AJv4.png)

新增儀錶板
![](https://i.imgur.com/7GjMGSB.png)

看數據
![](https://i.imgur.com/yQLeBIR.png)


事件驅動要寫code
![](https://i.imgur.com/J4KEYvW.png)

MAIL通知
![](https://i.imgur.com/oUHJO75.png)


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

觸發:
![](https://i.imgur.com/v5t9x34.png)



LINE的連結要看官方文件
![](https://i.imgur.com/i0jvd78.png)
![](https://i.imgur.com/yhb5x9d.png)


---
老師的平台:帳密不可以打中文
https://web.oceanbox.com.tw/
選物聯網

https://web.oceanbox.com.tw/member.php

物聯網連線碼:要登入才能看到
String host = "iot.oceanbox.com.tw";就是主機
String code = "物聯網連線碼";
String user = "jzs2home";物聯網帳
String PHP_PATH = "/oceanduino/ledGet202001.php";

大數據
https://web.oceanbox.com.tw/iot_list-9d90b4f4815e5e196770a3e9aed4b6f5-jzs2home-1.html

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

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

按遠端控制:

https://web.oceanbox.com.tw/iot_list-9d90b4f4815e5e196770a3e9aed4b6f5-jzs2home-1.html

開燈/關燈...但是~要記得把設定改成手動(不然會有2個參數)

要控制自動跳回~  switch_count1 += delay_time;
![](https://i.imgur.com/KXxD5Zp.png)

````

````