Back to Parent

Code for Fan to Work with Webhook Data
//Code for Fan
String temperature_2m;
String rain;
String snowfall;
String weather_code;

int fanPin = D2;

void getData(){
	// Publish an event to trigger the webhook
  Particle.publish( "get-forecast" );
}

// This function will handle data received back from the webhook
void handleForecastReceived(const char *event, const char *data) {
  // Handle the integration response
    if (data) {
        String receivedStr = String(data);
        int loc1 = 0, loc2 = 0;

        loc1 = receivedStr.indexOf("~");
        temperature_2m = receivedStr.substring(0, loc1);

        loc2 = receivedStr.indexOf("~", loc1 + 1);
        rain = receivedStr.substring(loc1 + 1, loc2);

        loc1 = receivedStr.indexOf("~", loc2 + 1);
        snowfall = receivedStr.substring(loc2 + 1, loc1);

        weather_code = receivedStr.substring(loc1 + 1);
        
        if (rain == "1" || snowfall == "1") {
            controlFan(true);
        } else {
            controlFan(false);
        }
    }
    
    Particle.publish("snowData", snowfall, PUBLIC);
}


void controlFan(bool state) {
    if (state) {
        digitalWrite(fanPin, HIGH); // Turn the fan on
    } else {
        digitalWrite(fanPin, LOW); // Turn the fan off
    }
}

void setup() {
    Particle.subscribe("hook-response/get-forecast", handleForecastReceived );
    pinMode(fanPin, OUTPUT); 
    getData();
    
}

void loop() {

}
Click to Expand

Content Rating

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

0