Back to Parent

int tempPin = A4;
int led1 = D2;
int led2 = D4;
int soilSensor = A1;
int led4 = D7;
int tweeted = 0;
int speakerPin = D6;

// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {131,131,131,147,165,0,
165,147,165,175,196,0,
261,261,196,196,165,165,131,0,
196,175,165,147,131}; 

// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,4,2,8,4,8,2,8,4,4,4,4,4,4,4,4,4,4,4,8,4,4,4,4,4};

// store the time when you last published
int last_published = -1;



// Create a variable that will store the temperature value
double temperature = 0.0;
double temperatureF = 0.0;


void setup()
{
  // Register a Particle variable here
  Particle.variable("temperature", &temperature, DOUBLE);
  Particle.variable("temperatureF", &temperatureF, DOUBLE);

  // Connect the temperature sensor to A0 and configure it
  // to be an input
  //Connect led1 and led2 to be the output
  pinMode(tempPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  
  //connect the soil moisture sensor to A1 to be an input
  //connect led3 and led4 to be the output
   Serial.begin(9600);
    Particle.variable("soilSensor", &soilSensor, INT);
    pinMode(A1, INPUT);
    pinMode(led4, OUTPUT);
    
//Piezos:setup sketch and play notes
  pinMode( speakerPin, OUTPUT );
  playNotes();
}

void playNotes()
{
    // iterate over the notes of the melody:
    for (int thisNote = 0; thisNote < 8; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000/noteDurations[thisNote];
      tone(speakerPin, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:
      noTone(speakerPin);
    }
}

void loop()
{
  // Keep reading the sensor value so when we make an API
  // call to read its value, we have the latest one
  int reading = analogRead(tempPin);

  // The returned value from the device is going to be in the range from 0 to 4095
  // Calculate the voltage from the sensor reading
  double voltage = (reading * 3.3) / 4095.0;

  // Calculate the temperature and update our static variable
  temperature = (voltage - 0.5) * 100;

  // Now convert to Farenheight
  temperatureF = ((temperature * 9.0) / 5.0) + 32.0;
  
  //Low temp notification
  if (temperature < 18)
  {
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  }
  
  //High temp notification
  if (temperature > 24)
  {
  digitalWrite(led2,HIGH);
  digitalWrite(led1,LOW);
  }
  
  //Publish temp data to particle
  Particle.publish("TemperatureReadingC", String(temperature));
  delay(10000);
  //Particle.publish("TemperatureReadingF", String(temperatureF));
  
  //soil sensor record and notification
  soilSensor = analogRead(A1);
    if (soilSensor < 1000 && tweeted == 0) {
     // Generic copy for the tweet
     String data = "Hey, @Ariel__Yu__! I need some water! #";
     // Twitter won't post identical tweets. I generate a random integer between 1 and 30 and append it.
     data += random (30);
     // Trigger the integration
     Particle.publish("tweet", data, PRIVATE);
     // Set tweeted variable to 1
     tweeted = 1;
     // Wait 60 seconds
     delay(6000);
 }
 
 else if (soilSensor < 1000 && tweeted != 0){
   // Let's blink the D7 LED, in case I missed the tweet
   playNotes();
   digitalWrite(led1, HIGH);
   delay(1000);
   digitalWrite(led1, LOW);
   delay(1000);
 }
 
 else {
 // Moisture is OK, let's set the tweeted variable to 0
 tweeted = 0;
 digitalWrite(led4,HIGH);
 // Let's wait for another 10 minutes before reading A0 again
 delay(10000);
 }
    //publish soil sensor data to particle
    Particle.publish("SoilSensorReading", String(soilSensor));
    delay(10000);
    to_spreadsheet();
    delay(10000);
}



void to_spreadsheet(){
   // Particle.publish("Test_lastpub", String(last_published));
    //Particle.publish("Test_milli", String(millis()));
 //check if 1 minute has elapsed
//	if( last_published + 6000 < millis() ){
		Particle.publish( "log_to_spreadsheet", String(temperature));
		//Particle.publish( "log_to_spreadsheet", String(temperatureF));
		//Particle.publish( "log_to_spreadsheet", String(soilSensor));
//		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!

0