如何製作植物監測儀
本專案主要製作一款植物監測儀,工能是:水分感測器提示LED閃爍,並在植物需要用水時使用IFTTT向手機發送相關資訊。
該專案需要使用的東西:
硬體
光子(Particle Photon)物聯網開發板(http://www.dfrobot.com.cn/goods-1158.html)
Particle Electron
電容式土壤溼度感測器(http://www.dfrobot.com.cn/goods-1215.html)
軟體
IFTTT Maker服務
ThingSpeak應用程式介面
故事
大部分人的房子裡面都有一種或多種盆栽植物需要定期進行澆水。問題是,除非您是開專業花店的,否則您並不知道每棵植物何時需要澆水。為了解決這個問題,我們開發了一個系統,當您的植物需要水分時,系統會通過手機向您傳送通知。
首先,水分感測器連線Photon並放置在盆栽植物中。然後,當感測器讀取溼度值低於20時,Photon會通過程式設計方式釋出一個名為“moisturePercentage”的事件。同時,Electron會訂閱和釋出該事件,以及釋出自己的事件,兩個事件被稱為“plantStatus”和“handshake”;屆時它將會把名為thirsty的變數的值從true更改為false。IFTTT將訂閱“plantStatus”並向用戶的IFTTT應用程式傳送相關通知,而Photon將訂閱“handshake”事件並在聽到它時停止釋出“moisturePercentage”。在electron的程式碼中,某個while迴圈將導致其LED開始閃爍,並對變數進行賦值thirsty = true。這為使用者提供了另一種通知方式,防止使用者未帶手機的情況。當植物淹泡在水中時,水分含量將升至45以上,這導致Photon釋出另一個名為“plantWatered”的事件,Electron會訂閱該事件。這使得植物缺水的變數設定為false,防止電子LED閃爍。經過一段時間後,這些水分將被植物所吸收,並且整個過程將重新開始。
我們決定觀察植物吸收水分時,水分含量如何變化,這個過程將非常有趣。因此,每次Photon對其進行記錄時,我們都會通過一些事物對水含量進行記錄。我們發現我們所選的血根草植物能夠很快吸收我們供給的水,並且必須每隔一天至少澆一次水。
原理圖
Particle Photon的程式碼:
int boardLed = D7; //LED D7 for testing purposes
int moisture_pin = A1; //connection point for moisture sensor
bool messagesent = false; //variable for checking if notification has been sent to phone
String plantWatered = “Your plant has been watered and is moist”;
void setup() {
pinMode(boardLed,OUTPUT); //output to turn on LED for setup
pinMode(moisture_pin,INPUT); //Input from moisture sensor
//Flashes LED to indicate that flash is successful.
digitalWrite(boardLed,HIGH);
delay(2000);
digitalWrite(boardLed,LOW);
delay(2000);
digitalWrite(boardLed,HIGH);
delay(2000);
digitalWrite(boardLed,LOW);
delay(2000);
}
void loop() {
//digitalWrite(boardLed,HIGH);
// Now we’ll take some readings…
int moisture_analog = analogRead(moisture_pin); // read capacitive sensor
float moisture_percentage = (100 – ((moisture_analog/4095.00) * 100 ));
Particle.subscribe(“handshake”, handShake, “34002f000a47373336373936”); //Subscribe command to listen to other device
if (moisture_percentage <= 22 && messageSent == false){ //Checks if moisture percentage is below threshold and also checks if the water message has been sent
Particle.publish(“moisturePercentage”, String(moisture_percentage),60,PUBLIC); //Publishes for message to be sent
}
if (moisture_percentage > 40 && messageSent == true){ //Checks if moisture levels have gone back up and also checks if message has been sent
Particle.publish(“plantWatered”, plantWatered, 60, PUBLIC);
messageSent = false; //If message sent, and plant watered, it resets the program so that the function above will run
}
Particle.publish(“plantStatus_percentage”, String(moisture_percentage),60,PUBLIC); //Publish command for logging data and sending it to thingspeak
//digitalWrite(boardLed,LOW);
delay(15000);
}
void handShake( const char *event, const char *data){ //If the message has been sent, it sets messageSent to true
messageSent = true;
}
Particle Electron 的程式碼:
int led=D7;//led that indicates the plant needs water
bool sent=false;// has the notification to water the plant been sent
bool thirsty=false;//whether or not the plant needs water
void setup()
{
pinMode(led,OUTPUT);
}
void loop()
{
Particle.subscribe(“moisturePercentage”,waterPlant,”3c0026001747373335333438″);
while(thirsty==true)
{//while the plant needs water the LED flashes and the electron listens for when the water level goes up
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(500);
Particle.subscribe(“plantWatered”,breakloop,”3c0026001747373335333438″);// checks for when the plant has been waterd.
}
delay(5000);// this loop every five minutes
}
void waterPlant( const char *event, const char*data)
{
Particle.publish(“plantStatus”,”thirsty”,60,PUBLIC);//sends a message to IFTTT to notify me to water my plants.
sent=true;
Particle.publish(“handshake”,”sent”,60,PUBLIC);// notifies the photon that the message has been sent
thirsty=true;
}
void breakloop( const char *event, const char*data)// changes thirsty to false to break the while loop and stop the led from flashing
{
thirsty=false;
sent=false;
}