Arduino自動灌溉系統

作用: 一般的灑水系統是時間自動到時灑水,但與實際狀況不符。可以利用土壤溼度計偵測土壤的溼度再予以灌溉。
材料: Arduino *1 、麵包版*1、16*2 LCD*1 、土壤溼度計*1、溫溼度計*1、繼電器*1、太陽能電子板*1、抽水馬達*1
程式:
#include
#include
#define dht_dpin A1 // 溫濕度資料腳位
LiquidCrystal_I2C lcd(0x27, 16, 2); // 顯示器
byte bGlobalErr;
byte dht_dat[5];
int relay = 10; //繼電器腳位為10

void setup() {
InitDHT();
delay(300);
lcd.begin();
lcd.backlight();
lcd.print(“Lin Shou Shan”);
pinMode(relay,OUTPUT); //定義繼電器腳位10為輸出
}

void loop() {
ReadDHT();
lcd.clear();
switch (bGlobalErr){
case 0:
String TT,HH;
HH = String(dht_dat[0])+’.’+String(dht_dat[1]);
TT = String(dht_dat[2])+’.’+String(dht_dat[3]);
lcd.setCursor(0, 0);
lcd.print(“H:”+HH+” T:”+TT); // 第一行顯示空氣溼度/溫度
}
lcd.setCursor(0, 1);
int aa;
aa = analogRead(0);
lcd.print(“Value:”+String(analogRead(0))); // 第二行顯示土壤溼度值
if (aa < 300){ // 濕度小於範圍就啟動澆水 autowatering(); } delay(10000); // 10秒執行一次 } void autowatering(){ digitalWrite(relay,HIGH); //開啟馬達 delay(5000); // 抽水三秒 digitalWrite(relay,LOW); //關閉繼電器 delay(1000); } void InitDHT(){ // 初始化溫溼度計 pinMode(dht_dpin,OUTPUT); digitalWrite(dht_dpin,HIGH); } void ReadDHT(){ bGlobalErr=0; byte dht_in; byte i; digitalWrite(dht_dpin,LOW); delay(20); digitalWrite(dht_dpin,HIGH); delayMicroseconds(40); pinMode(dht_dpin,INPUT); dht_in=digitalRead(dht_dpin); if(dht_in){ bGlobalErr=1; return; } delayMicroseconds(80); dht_in=digitalRead(dht_dpin); if(!dht_in){ bGlobalErr=2; return; } delayMicroseconds(80); for (i=0; i<5; i++) dht_dat[i] = read_dht_dat(); pinMode(dht_dpin,OUTPUT); digitalWrite(dht_dpin,HIGH); byte dht_check_sum = dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3]; if(dht_dat[4]!= dht_check_sum) {bGlobalErr=3;} }; byte read_dht_dat(){ byte i = 0; byte result=0; for(i=0; i< 8; i++){ while(digitalRead(dht_dpin)==LOW); delayMicroseconds(30); if (digitalRead(dht_dpin)==HIGH) result |=(1<<(7-i)); while (digitalRead(dht_dpin)==HIGH); } return result; }

Arduino教學-使用ESP8266 wifi模組+DHT 溫溼度感測器上傳thingspeak

這是透過Arduino+ESP8266(wifi)+DHT(溫溼度感測器),實現每60秒上傳溫溼度到thingspeak網站的記錄。
一、上 Thingspeak 申請帳號,然後建立一個Channel後,然後取得Write API Key。(https://thingspeak.com)
二、DHT連線
DHT → Arduino
S → A0
+ → 5V (VCC)
– → GND
三、ESP8266
ESP8266 → Arduino
GND → GND
UTXD → RX(0)
CH_PD → 3.3V (VCC 注意:連接USB時不要接上,不然會抓不到,程式上傳後,直接用變壓器)
URXD → TX(1)
VCC → 3.3V (VCC)
四、USB轉TTL (作為監測用)
USBTTL → Arduino
TXD(綠)→ 2
RXD(白)→ 3
l_140820105419
腳位圖
20691_10152621979171541_6306184665770254413_n

11024717_10152621978966541_3990729037825987143_n

未命名 - 5
程式碼

#include <SoftwareSerial.h>
#define DEBUG
#define _baudrate   9600

// USB TTL 偵錯用
#define _rxpin      2
#define _txpin      3
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX

// DHT溫溼度感測器
#define dht_dpin A0
byte bGlobalErr;
byte dht_dat[5];

// ESP8266 Wifi晶片 
#define SSID "[無線分享器名稱]" 
#define PASS "[無線分享器密碼]"
#define IP "184.106.153.149" // ThingSpeak的IP
// 使用 GET 傳送資料的格式
String GET = "GET /update?key=[Write API Key]";

void setup() {
    InitDHT();
    Serial.begin( _baudrate );
    debug.begin( _baudrate );
    delay(300);
    sendDebug("AT");
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("RECEIVED: OK\nData ready to sent!");
        connectWiFi();
    }
    delay(700);
}

void loop() {
  ReadDHT();
  switch (bGlobalErr){
     case 0:
        String TT,HH;
        HH = String(dht_dat[0])+'.'+String(dht_dat[1]);
        TT = String(dht_dat[2])+'.'+String(dht_dat[3]);
        updateDHT11( TT, HH );        
        #ifdef DEBUG
            debug.print("Humidity: "); 
            debug.print( HH );
            debug.print(" %\t");
            debug.print("Temperature: "); 
            debug.print( TT );
            debug.println(" *C\t");
        #endif
    
  }
  delay(60000);   // 一分鐘一次
}

void updateDHT11( String T, String H )
{
    // 使用AT指令上傳
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    sendDebug(cmd);
    delay(2000);
    if( Serial.find( "Error" ) )
    {
        debug.print( "RECEIVED: Error\nExit1" );
        return;
    }

    cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
    Serial.print( "AT+CIPSEND=" );
    Serial.println( cmd.length() );
    if(Serial.find( ">" ) )
    {
        debug.print(">");
        debug.print(cmd);
        Serial.print(cmd);
    }
    else
    {
        sendDebug( "AT+CIPCLOSE" );
    }
    if( Serial.find("OK") )
    {
        debug.println( "RECEIVED: OK" );
    }
    else
    {
        debug.println( "RECEIVED: Error\nExit2" );
    }
}

void sendDebug(String cmd)
{
    // 傳到 USB TTL
    debug.print("SEND: ");
    debug.println(cmd);
    Serial.println(cmd);
} 
 
boolean connectWiFi()
{
    // 連線到Wifi
    Serial.println("AT+CWMODE=1");
    delay(2000);
    String cmd="AT+CWJAP=\"";
    cmd+=SSID;
    cmd+="\",\"";
    cmd+=PASS;
    cmd+="\"";
    sendDebug(cmd);
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("RECEIVED: OK");
        return true;
    }
    else
    {
        debug.println("RECEIVED: Error");
        return false;
    }

    cmd = "AT+CIPMUX=0";
    sendDebug( cmd );
    if( Serial.find( "Error") )
    {
        debug.print( "RECEIVED: Error" );
        return false;
    }
}
void InitDHT(){
   // 初始化DHT
   pinMode(dht_dpin,OUTPUT);
   digitalWrite(dht_dpin,HIGH);
}

void ReadDHT(){
bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(20);

digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);

dht_in=digitalRead(dht_dpin);

if(dht_in){
   bGlobalErr=1;
   return;
   }
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);

if(!dht_in){
   bGlobalErr=2;
   return;
   }
delayMicroseconds(80);
for (i=0; i<5; i++)
   dht_dat[i] = read_dht_dat();
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
byte dht_check_sum =
       dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
if(dht_dat[4]!= dht_check_sum)
   {bGlobalErr=3;}
};

byte read_dht_dat(){
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
      while(digitalRead(dht_dpin)==LOW);
      delayMicroseconds(30);
      if (digitalRead(dht_dpin)==HIGH)
     result |=(1<<(7-i));
    while (digitalRead(dht_dpin)==HIGH);
    }
  return result;
}

Delphi控制Arduino紅綠燈與LCD

最近開始學著使用Arduino來做一些簡單的控制,對於只學商完全不懂電子、電路的人真的是一大福音!很快就能上手~
當然最終還是希望能與電腦做搭配控制,寫了個簡單的程式控制紅、黃、綠燈,並在LCD液晶顯示器顯示目前亮的是Red/Yellow/Blue LED.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
int redled =10; 
int yellowled =7; 
int blueled =4; 
void setup()
{
  Serial.begin(9600);
  pinMode(redled, OUTPUT);
  pinMode(yellowled, OUTPUT);
  pinMode(blueled, OUTPUT); 
  lcd.begin();
  lcd.clear();
  lcd.backlight();
  lcd.print("Lin Shou Shan");
        
}

void loop()
{
  char s;
  if (Serial.available()>0){
    s=(char) Serial.read();
    lcd.clear();
      digitalWrite(redled, LOW);
      digitalWrite(yellowled, LOW);
      digitalWrite(blueled, LOW);       
    if(s=='R'){
      digitalWrite(redled, HIGH);     
      lcd.print("Red LED");
    }
    if(s=='Y'){
      digitalWrite(yellowled, HIGH);     
      lcd.print("Yellow LED");      
    }
    if(s=='B'){
      digitalWrite(blueled, HIGH);      
      lcd.print("Blue LED");        
    } 
  }
}
procedure TForm1.Button1Click(Sender: TObject);
begin
  Comm1.StopComm;
  COMM1.CommName := 'COM6';
  COMM1.StartComm;
end;

procedure TForm1.Button2Click(Sender: TObject);
var s:AnsiString;
begin
  s:='';
  s:= TButton(Sender).Hint;
  Comm1.WriteCommData(PAnsiChar(s),Length(s)); //送出指令給遠端
end;