Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <DFRobotDFPlayerMini.h>

// button variables
int buttonPin1 = D0;
bool buttonPressed;
int state = 0;

// sensor variables
int pirPin = D2;
int pirState = LOW;
int motionReading = 0;
int calibrateTime = 1000;

// whether variables
String weatherIcon;
double temperature;
double precipProbability;
double precipIntensity;

// player variables
DFRobotDFPlayerMini myDFPlayer;

// time variables
unsigned long startTime;
unsigned long waitLength;
int duration = 30000;

// set up the photon
void setup() {
    Particle.publish("forecast", "");
    Particle.subscribe("hook-response/forecast", handleForecastReceived, MY_DEVICES);
    getData();

    pinMode(buttonPin1, INPUT_PULLUP);
    pinMode(pirPin, INPUT);
    Serial.begin(9600);
    Serial1.begin(9600);

    Particle.function("PlayTrack:", playSound);
    Particle.publish("system","START");

    Serial.println("Serial Monitor Initialized");
    Serial.println();
    Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

    if (!myDFPlayer.begin(Serial1)) {
        Serial.println(F("Unable to begin:"));
        Serial.println(F("1.Please recheck the connection!"));
        Serial.println(F("2.Please insert the SD card!"));
        while(true){
            delay(0);
        }
    }
    Serial.println("DFPlayer Initialized");
    myDFPlayer.volume(30);

}

void loop () {
    startTime = millis();
    waitLength = startTime + duration;

    if(waitLength < millis()){
        getData();
        startTime = millis();
    }

    reportTheData();

    buttonPressed = digitalRead(buttonPin1);

    if (buttonPressed == LOW){
        state += 1;
        state = state % 2;
        Serial.println("State is: ");
        Serial.println(state);
        switch(state){
            case 0:
            myDFPlayer.pause();
            delay(250);
            break;
            case 1:
            myDFPlayer.loop(1);
            delay(250);
        }
    }
  delay(100);
}

// get whether data for a specific location
void getData() {
    // Publish an event to trigger the webhook
    Particle.publish("forecast", "19.0896,72.8656", PRIVATE);
}

// respond to the whether by making 1 of 4 sound tracks
// send the whether info to other devices by publishing it to the particle cloud
void reportTheData() {
    motionReading = digitalRead(pirPin);
    if (motionReading == HIGH) {
        if (pirState == LOW) {
            Particle.publish("Motion", "Detected");
            pirState = HIGH;
            Particle.publish("Whether", weatherIcon + ", " + String(temperature) + ", " + String(precipProbability) + ", " + String(precipIntensity));
            if (weatherIcon == "clear-day" || "clear-night") {
                myDFPlayer.play(2);
                Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "clear" );
            }
            if (weatherIcon == "rain" || "thunderstorm") {
                myDFPlayer.play(4);
                Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "rain" );
            }
            if (weatherIcon == "wind" || "tornado") {
                myDFPlayer.play(6);
                Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "wind" );
            }
            if (weatherIcon == "fog" || "cloudy" || "partly-cloudy-day" || "partly-cloudy-night" || "sleet") {
                myDFPlayer.play(5);
                Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "cloud" );
            }
        }
    } else {
        if (pirState == HIGH){
            Particle.publish("Motion", "LOW");
            pirState = LOW;
            myDFPlayer.pause();
        }
    }
}

// manually control the output for all three devices
int playSound(String command) {
       if (command == "1") {
    	   myDFPlayer.play(2);
    	   Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "clear" );
       }
       if (command == "2") {
    	   myDFPlayer.play(4);
    	   Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "rain" );
       }
       if (command == "3") {
    	   myDFPlayer.play(5);
    	   Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "cloud" );
       }
       if (command == "4") {
    	   myDFPlayer.play(6);
    	   Particle.publish("diot/2018/connected/atHome" + System.deviceID(), "wind" );
       }
       if (command == "0") {
    	   myDFPlayer.pause();
       }
    return 1;
}

// parse the data received into readable text
void handleForecastReceived(const char *event, const char *data) {
    // Handle the integration response
    String receivedStr =  String( data );
     Particle.publish("data", receivedStr );
    int loc1 = 0;
    int loc2 = 0;
    int loc3 = 0;
    int loc4 = 0;

    loc1 = receivedStr.indexOf("~");

    weatherIcon = receivedStr.substring(0,loc1);

    loc2 = receivedStr.indexOf("~",loc1+1);
    temperature = (double) String(receivedStr.substring(loc1+1,loc2)).toFloat();

    loc3 = receivedStr.indexOf("~",loc2+1);
    precipProbability = (double) String(receivedStr.substring(loc2+1,loc3)).toFloat();

    loc4 = receivedStr.indexOf("~",loc3+1);
    precipIntensity = (double) String(receivedStr.indexOf(loc3+1)).toFloat();
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0