Things used in this project

Hardware components:
Photon new
Particle Photon
×2
13637 01
SparkFun Soil Moisture Sensor (with Screw Terminals)
×1
09590 01
LED (generic)
×1
11026 02
Jumper wires (generic)
×3
12002 04
Breadboard (generic)
×2
Mfr 25fbf52 221r sml
Resistor 221 ohm
×1
Software apps and online services:
Hackster
ThingSpeak API
IFTTT

Schematics

Sensing Photon
Waterbuffalooschematic h4z5xhkwz0

Code

Running The SensorC/C++
This code is what is ran on the outside Photon. This code controls the sensor and publishes data to Particle Console
//First we give representative names to the pins of the Photon we'll need special use of

int driver = D0;  //declare D0 as pin to power sensor

int sensin = A0;  //declare A0 as pin to read sensor

int threshold = 650;  //threshold of 20% moisture in soil

//Now we must set up the important aspects of the Photon pins
//Setup runs once each time the Photon is powered up

void setup() {
    
pinMode(driver, OUTPUT); //makes D0 an output pin...supplies power
    
pinMode(sensin, INPUT);  //makes A0 an input pin...receives power
}

//Now the action portion of the code must be written
//Loops technically run indefinitely until something makes them stop, which isn't what is wanted for this project, but it will be made to run once anyway by making it go to sleep.

void loop() {    //initiates the loop
    
digitalWrite(driver, HIGH);   //turns the power on to sensor

delay(1000);   //wait for sensor to stabilize

int waterval;   //create new numerical variable 'waterval'

waterval = analogRead(sensin);  //read sensor and set waterval to that value

delay(1000);   //wait one second to ensure proper read of sensor

digitalWrite(driver, LOW);  //turn power off to sensor

    if (waterval < threshold){   //if the value of waterval is less than the threshold
       
       Particle.publish("WaterBuffaloo", "wateron");   //publish that the water needs to be on
       delay(1000);                                    //wait one second for Console to update
       String watercont = String(waterval);            //declare waterval value as a string for transport with new name 'watercont'
       Particle.publish("buffwatercontent", watercont );   //publish the actual value of waterval
       
    }
   
   else {                                               //otherwise when waterval is greater than threshold
       Particle.publish("WaterBuffaloo", "wateroff");   //publish that the water needs to be off
       delay(1000);                                     //wait one second for Console to update
       String watercont = String(waterval);             //declare waterval value as a string for transport with new name 'watercont'
       Particle.publish("buffwatercontent", watercont );    //publish the actual value of waterval
   }
   delay(2000);   //wait a couple seconds for everything to send
   
   System.sleep(SLEEP_MODE_DEEP, 3600);  //put the Photon in deep sleep mode for 30 minutes

} //closes the loop, but this is never actually reached in code.


// When the Photon wakes up after 30 minutes, it will rerun the entire code until it gets to the command to go to sleep.  At that point it will stop running code and power off.
Turn on LEDC/C++
This code turns on an LED on a remote Photon.
// Every program based on Wiring (programming language used by Arduino, and Particle devices) has two essential parts:
// setup - runs once at the beginning of your program
// loop - runs continuously over and over

// You'll see how we use these in a second. 

// This program will turn on an led on for 3 seconds when it reads the "wateron" command from Particle.io that reminds us to on the water.
// It turns on the D7 LED on your Particle device. If you have an LED wired to D0, it will turn on that LED as well.

// First, we're going to define some variables.
// This is our "shorthand" that we'll use throughout the program:

int led1 = D0; // Instead of writing D0 over and over again, we'll write led1
// You'll need to wire an LED to this one to see it illuminate.

int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
// This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.

// Having declared these variables, let's move on to the setup function.
// The setup function is a standard part of any microcontroller program.
// It runs only once when the device boots up or is reset.

void setup() {
    
// First we need to find our command to turn on the LED.
// Here we are telling the program to look in Particle to find a specific event. 
// The event we are looking for holds our "wateron" command that we need to turn on our LED to remind us to water the lawn.
// We use a Particle.subscibe function to search Particle for the event "WaterBuffaloo".

  Particle.subscribe("WaterBuffaloo", MyHandler);
// Inside the function we call to a sub-function, MyHandler, that holds our command word "wateron". More about that later.

// Next, we are going to tell our device that D0 and D7 (which we named led1 and led2 respectively) are going to be output pins.
// (That means that we will be sending voltage to them, rather than monitoring voltage that comes from them)

// It's important you do this here, inside the setup() function rather than outside it or in the loop function.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}
// Next we have the loop function, the other essential part of a microcontroller program.
// This routine gets repeated over and over, as quickly as possible and as many times as possible, after the setup function is called.
void loop(){
}

// Now for our LED turn on function.
void LED_on() {

// To turn on the LED we need to tell the pins to activate. This supplies power to the desired pins. We will use a digitalWrite command.
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);

// We'll leave it on for 3 seconds with a delay command.
  delay(3000);

// Then we'll turn it off.
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
} // It should be noted that you can make this LED blink on and off as many times as you want if you copy and paste this portion of the code.

// Finally our sub-function. 
// This is where our program looks during set-up to see if any action is needed. i.e. turning on the LED.
void MyHandler(const char *event, const char *data){
// Particle.subscribe handlers are void functions, which means they don't return anything.
// They take two variables---the name of your event, and any data that goes along with your event.
// In this case, the event and data are arbitrary.
    
// Since the input here is a char, we can't do data=="wateron" chars just don't play that way.
// Instead we're going to use strcmp(), (string compare) which compares two chars.
// If they are the same, strcmp will return 0 and turn on the LED by reading the "if" command LED_on.

  if (strcmp(data,"wateron")==0) {

    LED_on();
   }

   
  }
// By flashing this code to your Photon,
// your LED should turn on for 3 seconds and turn off when it reads the "wateron" command from the Particle event "WaterBuffaloo". 

Credits

1cdccbf6932d0f3bf365ef0b2f98e2c9
Dalton Johnson
1 project • 0 followers
Contact
0fdc32c6299f99ad332c1acfc1fed657
Scott Willard
1 project • 0 followers
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login