Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Justin Fox
Published © GPL3+

Plant Monitor

Moisture sensor tells an LED to blink and uses IFTTT to send an message to your phone when your plant needs water.

IntermediateFull instructions provided2 hours1,800
Plant Monitor

Things used in this project

Story

Read more

Schematics

Circuit Diagram for our IOT project.

This shows connect the moisture sensor to the Photon.

Code

Photon Code

C/C++
This is the photon's half of the code for the Plant Monitor project.
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;
}

Electron Code

C/C++
This is the Electron's half of the code for the Plant Monitor Project.
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;
     }

Credits

Justin Fox

Justin Fox

1 project • 1 follower

Comments