// This #include statement was automatically added by the Particle IDE. #include SYSTEM_THREAD(ENABLED); // Allows multithreading - Wifi goes out, board still runs STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // Uses external attena /* RainBuddy - June 2017 - Alec Miller What it does: The RainBuddy converts a standard sprinkler controller into a WiFi connected controller that accesses weather data via IFTTT to stop the sprinklers from running before and after rain events. Additionally, the RainBuddy allows the sprinklers to be turned off for the winter and back on for the the summer from an app. This function can also be triggered by temperature. The system works by interupting the COM line which is the common return line for all sprinkler valves on the same controller. By interupting the COM line, the sprinkler controller will keep running the existing run cycle, but the RainBuddy will prevent the sprinkler valves from turning on when it rains, or during winter months etc. Hardware includes: 1) Particle Photon (Particle.io) - 3.3V Microcontroller from particle.io w/External Antenna 2) Sainsmart 5V Relay Note: Relay is connected as normally closed - so if the system fails the sprinkler can still operate 3) Existing sprinkler controller (Any brand) Connections are: D1: Relay External Functions: 1) Sprinkonoff - External Function that the user calls directly to indicate user preference for on/off. 2) Rainonoff - External Function linked to Weather Underground via IFTTT 3) Winteronoff - External Function linked to IFTTT. */ // Setup timer elapsedMillis timeElapsed; //timer for pubishing variables // Variables int pinstaterelay; // Variable to read D1 bool rainstate = false; // Flag for in rain is coming, just came etc. bool winter = false; // Flag for long term shut off for winter bool useron = true; // Flag for user to turn off Sprinklers - overrides all other states. bool sprinklerstate = true; // Flag for state of Sprinklers // Pin Variables int relaypin = D4; void setup() { // External Functions Particle.function("Sprinkonoff",Sprinkonoff); // Sets up function for IFTTT Etc. Particle.function("Winter",Winteronoff); // Sets up function for IFTTT Etc. Particle.function("Rain",Rainonoff); // Sets up function for IFTTT Etc. // System Setups Serial.begin(9600); // use the serial port // Pin Setups pinMode(relaypin,OUTPUT); digitalWrite(relaypin, HIGH); // Set it high to keep it closed - Connected via NC - Normally Closed relay contacts } // Ends Setup void Sprinkleroff() { // Controls relay digitalWrite (relaypin, LOW);} void Sprinkleron() { // Controls relay digitalWrite (relaypin, HIGH);} void Getpinstate() { // Gets state of pins pinstaterelay = digitalRead(relaypin); } void Publishpins() { // Used for remote status checking char VarString2[40]; sprintf(VarString2, "%d", pinstaterelay); Particle.publish("Sprinklers are ", VarString2); Serial.println(pinstaterelay); sprintf(VarString2, "%d", sprinklerstate); Particle.publish("Sprinkler states are ", VarString2); Serial.println(sprinklerstate);} void loop() { if (timeElapsed >= 10000) { // Updates Pinstates, publishes variables Getpinstate(); // Updates pin states Publishpins(); // Publishes pin states timeElapsed = 0;} if (((winter == true) || (rainstate == true)) && (useron == true)) { sprinklerstate = false;} if (((winter == false) && (rainstate == false)) && (useron == true)) { sprinklerstate = true;} if (sprinklerstate == false){ Sprinkleroff();} if (sprinklerstate == true){ Sprinkleron();} } // Ends Loop int Sprinkonoff(String command) { // Used for on/off from IFTTT DO Button Particle.publish("Sprinkonoff", "FunctionCalled",60,PRIVATE); if(command == "On"){ sprinklerstate = true; useron = true;} if (command == "Off"){ useron = false; sprinklerstate = false;} } int Rainonoff(String command) { // Called by IFTTT Aplet using Weather Underground data Particle.publish("Rainonoff", "FunctionCalled",60,PRIVATE); if(command == "Rain"){ rainstate = true; } if (command == "Dry"){ rainstate = false;} } int Winteronoff(String command) { Particle.publish("Winteronoff", "FunctionCalled",60,PRIVATE); if(command == "Winter"){ winter = true; } if (command == "Summer"){ winter = false;} }