From 5d88189a0713921d20eb2730721bd2c89a447858 Mon Sep 17 00:00:00 2001 From: timberjoegithub Date: Tue, 21 Feb 2017 16:54:44 -0500 Subject: [PATCH] Create cloudcontrol.ino --- Particle/Photon/Firmware/cloudcontrol.ino | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Particle/Photon/Firmware/cloudcontrol.ino diff --git a/Particle/Photon/Firmware/cloudcontrol.ino b/Particle/Photon/Firmware/cloudcontrol.ino new file mode 100644 index 0000000..6680050 --- /dev/null +++ b/Particle/Photon/Firmware/cloudcontrol.ino @@ -0,0 +1,73 @@ + + +/* Includes ------------------------------------------------------------------*/ +#include "NCD8Relay.h" +#include "spark_wiring_print.h" + +SYSTEM_MODE(AUTOMATIC); +NCD8Relay relayController; +int triggerRelay(String command); + +/* This function is called once at start up ----------------------------------*/ +void setup() +{ + Serial.begin(115200); + relayController.setAddress(0,0,0); + Spark.function("controlRelay", triggerRelay); +} + +/* This function loops forever --------------------------------------------*/ +void loop() +{ +} + +int triggerRelay(String command){ + if(command.equalsIgnoreCase("turnonallrelays")){ + relayController.turnOnAllRelays(); + return 1; + } + if(command.equalsIgnoreCase("turnoffallrelays")){ + relayController.turnOffAllRelays(); + return 1; + } + if(command.startsWith("setBankStatus:")){ + int status = command.substring(14).toInt(); + if(status < 0 || status > 255){ + return 0; + } + Serial.print("Setting bank status to: "); + Serial.println(status); + relayController.setBankStatus(status); + Serial.println("done"); + return 1; + } + //Relay Specific Command + int relayNumber = command.substring(0,1).toInt(); + Serial.print("relayNumber: "); + Serial.println(relayNumber); + String relayCommand = command.substring(1); + Serial.print("relayCommand:"); + Serial.print(relayCommand); + Serial.println("."); + if(relayCommand.equalsIgnoreCase("on")){ + Serial.println("Turning on relay"); + relayController.turnOnRelay(relayNumber); + Serial.println("returning"); + return 1; + } + if(relayCommand.equalsIgnoreCase("off")){ + relayController.turnOffRelay(relayNumber); + return 1; + } + if(relayCommand.equalsIgnoreCase("toggle")){ + relayController.toggleRelay(relayNumber); + return 1; + } + if(relayCommand.equalsIgnoreCase("momentary")){ + relayController.turnOnRelay(relayNumber); + delay(300); + relayController.turnOffRelay(relayNumber); + return 1; + } + return 0; +}