二、接線圖
(一)土壤溼度感測器
感測器腳位 D1 mini 接法
VCC→5V(電源腳位)
GND→GND(接地腳位)
AO→A0
(二)2x16LCD顯示器
D1 Mini LCD1602
GND GND
3v3 VCC
D2 SDA
D1 SCL
(三)繼電器
感測器腳位 d1 mini 接法
1. DC+→3.3V或5V(電源腳位)
2. DC-→GND(接地腳位)
3. IN→D6(數位腳位)
com:接馬達負極
NO:接電源負極
三、程式碼
1.整合成智能澆灌系統
#include <Wire.h> #include <esp_LiquidCrystal_I2C.h> esp_LiquidCrystal_I2C mylcd(0x27, 16, 2); const int AnalogIn = A0; //設定類比腳位 int readingIn = 0; //測量值 int no = 0;//計數器 int relayPin = 12; //繼電器引腳 int limitValue = 700; //限值(需要調整), //在空氣中的溼度感測器時是1024 或是 0 void setup() { Serial.begin(9600); pinMode(relayPin , OUTPUT); mylcd.init(); mylcd.backlight(); mylcd.setCursor(0, 0); mylcd.print("Welcome to"); mylcd.setCursor(0, 1); mylcd.print("My sensor"); delay(1000); } void loop() { no++; readingIn = analogRead(AnalogIn); //讀取數值 Serial.println(readingIn); mylcd.clear(); mylcd.setCursor(0, 0);//移到第一行第一個位置 mylcd.print("NO:"); mylcd.print(no); mylcd.setCursor(0, 1);//移到第二行第一個位置 mylcd.print(readingIn); if (readingIn > limitValue) { digitalWrite(relayPin , HIGH); mylcd.setCursor(7, 1);//移到第二行第8個位置 mylcd.print("Pump ON"); delay(1000); } else { digitalWrite(relayPin , LOW); mylcd.setCursor(7, 1);//移到第二行第8個位置 mylcd.print("Pump Off"); delay(1000); } delay(1000);//每秒測量一次 }
2.將資料送至雲端記錄
#include <Wire.h> #include <esp_LiquidCrystal_I2C.h> #include <ESP8266WiFi.h> const char* ssid = "mywifi"; //wifi 帳號 const char* password = "aaaa";// wifi 密碼 const char* safekey = "1234";//檢查碼 const uint16_t port = 80; //上傳的主機port const char* host = "192.168.0.100"; //上傳的主機ip const char* php_url = "/myfarm/t2.php";//上傳在主機的程式 byte sensor_id = 1; //偵測器ID esp_LiquidCrystal_I2C mylcd(0x27, 16, 2); const int AnalogIn = A0; //設定類比腳位 int readingIn = 0; //測量值 int no = 0;//計數器 int relayPin = 12; //繼電器引腳 int limitValue = 800; //限值(需要調整)0 WiFiClient client; void setup() { Serial.begin(9600); pinMode(relayPin , OUTPUT); mylcd.init(); mylcd.backlight(); mylcd.clear(); mylcd.setCursor(0, 0); mylcd.print("System start!"); WiFi.begin(ssid, password); Serial.println(""); int t = 0; mylcd.setCursor(0, 1); while (WiFi.status() != WL_CONNECTED) { t++; if (t > 15) break; Serial.print("."); mylcd.print("."); delay(500); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); mylcd.setCursor(0, 1); mylcd.print(WiFi.localIP()); delay(2000); mylcd.clear(); } void loop() { no++; readingIn = analogRead(AnalogIn); //讀取數值 Serial.println(readingIn); if (readingIn > 0) { no++; String things_request = "GET " + String(php_url); things_request += "?safekey=" + String(safekey); things_request += "&sensor_id=" + String(sensor_id); things_request += "&hh=" + String(readingIn) + "\r\n\r\n"; Serial.println(things_request); if (client.connect(host, port)) { client.print(things_request); Serial.println("Request: [ok]"); client.stop(); } else { Serial.println("Request: [error]"); } mylcd.clear(); mylcd.setCursor(0, 0); mylcd.print(no); mylcd.setCursor(7, 0); mylcd.print(String("H:") + String(readingIn)); if (readingIn > limitValue) { digitalWrite(relayPin , HIGH); mylcd.setCursor(0, 1);//移到第二行第8個位置 mylcd.print("Pump ON"); delay(1000); } else { digitalWrite(relayPin , LOW); mylcd.setCursor(0, 1);//移到第二行第8個位置 mylcd.print("Pump Off"); delay(1000); } delay(5000);//間隔5秒送一次資料,建議修改 } }四、執行結果
沒有留言:
張貼留言