int sensor_pin = A5; /* Soil moisture sensor O/P pin */
int ledPin= D2;
int sensor_analog=0;
int moisture_percentage;
int last_published = -1;//event publish record
int servoPin = A2;
Servo myServo;
int servoPos = 90;
bool delayOrNot=true;
void setup() {
// Serial.begin(9600); /* Define baud rate for serial communication */
Particle.variable( "moisture" , sensor_analog);
// Serial.begin(9600);
myServo.attach( A2 );
//Register our Particle to control the servo
Particle.function("servo", servoControl);
// Keep a cloud variable for the current position
Particle.variable( "servoPos" , servoPos);
pinMode(sensor_pin,INPUT);
pinMode(ledPin,OUTPUT);
analogWrite(ledPin,HIGH);
}
void loop() {
sensor_analog = analogRead(sensor_pin);
Serial.println(sensor_analog);
if (sensor_analog > 3500){
servoControl("40"); //when dry
delayOrNot=true;
analogWrite(ledPin,200);
sendmessage ( );
}
else{
if(delayOrNot==true){
servoControl("71");
}
delay(300);
servoControl("76");
delayOrNot=false;
analogWrite(ledPin,0);
}
moisture_percentage =sensor_analog/10000;
}
int servoControl(String command)
{
// Convert
int newPos = command.toInt();
// Make sure it is in the right range
// And set the position
servoPos = constrain( newPos, 0 , 180);
// Set the servo
myServo.write( servoPos );
// done
return 1;
}
void sendmessage ( )
{if( last_published + 60000 < millis() ){
Particle.publish("SoilStatus", "not enough water!",60,PUBLIC);
last_published = millis();
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .