Advertisement
Guest User

KT ST arduino sketch code

a guest
Mar 2nd, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.31 KB | None | 0 0
  1. //******************************************************************************************
  2. // File: ST_Anything_Doors_ThingShield.ino
  3. // Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
  4. //
  5. // Summary: This Arduino Sketch, along with the ST_Anything library and the revised SmartThings
  6. // library, demonstrates the ability of one Arduino + SmartThings Shield to
  7. // implement a multi input/output custom device for integration into SmartThings.
  8. // The ST_Anything library takes care of all of the work to schedule device updates
  9. // as well as all communications with the SmartThings Shield.
  10. //
  11. // ST_Anything_Doors implements the following:
  12. // - 2 x Door Control devices (used as Garage Doors)
  13. // - 4 x Contact Sensor devices (used to monitor magnetic door sensors)
  14. // - 1 x Motion device (used to detect motion in the garage)
  15. // - 1 x Temperature/Humidity device (unsed to monitor temp & humidity in the garage)
  16. //
  17. // During the development of this re-usable library, it became apparent that the
  18. // Arduino UNO R3's very limited 2K of SRAM was very limiting in the number of
  19. // devices that could be implemented simultaneously. A tremendous amount of effort
  20. // has gone into reducing the SRAM usage, including siginificant improvements to
  21. // the SmartThings Arduino library. The SmartThings library was also modified to
  22. // include support for using Hardware Serial port(s) on the UNO, MEGA, and Leonardo.
  23. // During testing, it was determined that the Hardware Serial ports provide much
  24. // better performance and reliability versus the SoftwareSerial library. Also, the
  25. // MEGA 2560's 8K of SRAM is well worth the few extra dollars to save your sanity
  26. // versus always running out of SRAM on the UNO R3. The MEGA 2560 also has 4 Hardware
  27. // serial ports (i.e. UARTS) which makes it very easy to use Hardware Serial instead
  28. // of SoftwareSerial, while still being able to see debug data on the USB serial
  29. // console port (pins 0 & 1).
  30. //
  31. // Note: We did not have a Leonardo for testing, but did fully test on UNO R3 and
  32. // MEGA 2560 using both SoftwareSerial and Hardware Serial communications to the
  33. // Thing Shield.
  34. //
  35. // Change History:
  36. //
  37. // Date Who What
  38. // ---- --- ----
  39. // 2015-01-03 Dan & Daniel Original Creation
  40. // 2015-01-07 Dan Ogorchock Modified for Door Monitoring and Garage Door Control
  41. // 2015-03-28 Dan Ogorchock Removed RCSwitch #include now that the libraries are split up
  42. // 2015-03-31 Daniel O. Memory optimizations utilizing progmem
  43. // 2017-02-12 Dan Ogorchock Revised to use the new SMartThings v2.0 library
  44. //
  45. //******************************************************************************************
  46.  
  47. //******************************************************************************************
  48. // SmartThings Library for Arduino Shield
  49. //******************************************************************************************
  50. #include <SmartThingsThingShield.h> //Library to provide API to the SmartThings Shield
  51.  
  52. //******************************************************************************************
  53. // ST_Anything Library
  54. //******************************************************************************************
  55. #include <Constants.h> //Constants.h is designed to be modified by the end user to adjust behavior of the ST_Anything library
  56. #include <Device.h> //Generic Device Class, inherited by Sensor and Executor classes
  57. #include <Sensor.h> //Generic Sensor Class, typically provides data to ST Cloud (e.g. Temperature, Motion, etc...)
  58. #include <InterruptSensor.h> //Generic Interrupt "Sensor" Class, waits for change of state on digital input
  59. #include <PollingSensor.h> //Generic Polling "Sensor" Class, polls Arduino pins periodically
  60. #include <Everything.h> //Master Brain of ST_Anything library that ties everything together and performs ST Shield communications
  61.  
  62.  
  63. #include <PS_TemperatureHumidity.h> //Implements a Polling Sensor (PS) to measure Temperature and Humidity via DHT library
  64. #include <IS_Motion.h> //Implements an Interrupt Sensor (IS) to detect motion via a PIR sensor
  65. #include <IS_Contact.h> //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
  66. #include <IS_DoorControl.h> //Implements an Interrupt Sensor (IS) and Executor to monitor the status of a digital input pin and control a digital output pin
  67.  
  68. //******************************************************************************************
  69. //Define which Arduino Pins will be used for each device
  70. // Notes: -Serial Communications Pins are defined in Constants.h (avoid pins 0,1,2,3
  71. // for inputs and output devices below as they may be used for communications)
  72. // -Always avoid Pin 6 as it is reserved by the SmartThings Shield
  73. //
  74. //******************************************************************************************
  75. //"RESERVED" pins for SmartThings ThingShield - best to avoid
  76. #define PIN_O_RESERVED 0 //reserved by ThingShield for Serial communications OR USB Serial Monitor
  77. #define PIN_1_RESERVED 1 //reserved by ThingShield for Serial communications OR USB Serial Monitor
  78. #define PIN_2_RESERVED 2 //reserved by ThingShield for Serial communications
  79. #define PIN_3_RESERVED 3 //reserved by ThingShield for Serial communications
  80. #define PIN_6_RESERVED 6 //reserved by ThingShield (possible future use?)
  81.  
  82. //Motion and Temperature/Humidity Pins (Note: using Analog pins as Digital inputs)
  83. #define PIN_MOTION A0
  84. #define PIN_TEMPERATUREHUMIDITY A1
  85.  
  86. #define PIN_CONTACT_FRONT_DBL_DOOR 5
  87. #define PIN_CONTACT_DINING_RM_WNDW 7
  88. #define PIN_CONTACT_LVNG_PATIO_DOOR 8
  89. #define PIN_CONTACT_KTCH_NOOK_WNDW 9
  90. #define PIN_CONTACT_SUN_ROOM_DOOR 10
  91. #define PIN_CONTACT_SUN_ROOM_WNDW 11
  92. #define PIN_CONTACT_BEDROOM1_WNDW 12
  93. #define PIN_CONTACT_BEDROOM2_WNDW 13
  94. #define PIN_CONTACT_GARAGE_DOOR 14
  95.  
  96.  
  97. //If using SoftwareSerial (e.g. Arduino UNO), must define pins for transmit and receive
  98. #define pinRX 3
  99. #define pinTX 2
  100.  
  101. //******************************************************************************************
  102. //st::Everything::callOnMsgSend() optional callback routine. This is a sniffer to monitor
  103. // data being sent to ST. This allows a user to act on data changes locally within the
  104. // Arduino sktech.
  105. //******************************************************************************************
  106. void callback(const String &msg)
  107. {
  108. Serial.print(F("ST_Anything Callback: Sniffed data = "));
  109. Serial.println(msg);
  110.  
  111. //TODO: Add local logic here to take action when a device's value/state is changed
  112.  
  113. //Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
  114. //st::receiveSmartString("Put your command here!"); //use same strings that the Device Handler would send
  115. }
  116.  
  117. //******************************************************************************************
  118. //Arduino Setup() routine
  119. //******************************************************************************************
  120. void setup()
  121. {
  122. //******************************************************************************************
  123. //Declare each Device that is attached to the Arduino
  124. // Notes: - For each device, there is typically a corresponding "tile" defined in your
  125. // SmartThings DeviceType Groovy code
  126. // - For details on each device's constructor arguments below, please refer to the
  127. // corresponding header (.h) and program (.cpp) files.
  128. // - The name assigned to each device (1st argument below) must match the Groovy
  129. // DeviceType Tile name. (Note: "temphumid" below is the exception to this rule
  130. // as the DHT sensors produce both "temperature" and "humidity". Data from that
  131. // particular sensor is sent to the ST Shield in two separate updates, one for
  132. // "temperature" and one for "humidity")
  133. //******************************************************************************************
  134. //Polling Sensors
  135. //static st::PS_TemperatureHumidity sensor1(F("temphumid"), 120, 10, PIN_TEMPERATUREHUMIDITY, st::PS_TemperatureHumidity::DHT22);
  136.  
  137. //Interrupt Sensors
  138. //static st::IS_Motion sensor2(F("motion"), PIN_MOTION, HIGH, false);
  139. //static st::IS_DoorControl sensor3(F("leftDoor"), PIN_CONTACT_LEFTGARAGE_DOOR, LOW, true, PIN_RELAY_LEFTGARAGE_DOOR, LOW, true, 1000);
  140. //static st::IS_DoorControl sensor4(F("rightDoor"), PIN_CONTACT_RIGHTGARAGE_DOOR, LOW, true, PIN_RELAY_RIGHTGARAGE_DOOR, LOW, true, 1000);
  141.  
  142. static st::IS_Contact sensor1(F("FrontDoubleDoors"), PIN_CONTACT_FRONT_DBL_DOOR, LOW, true);
  143. static st::IS_Contact sensor2(F("DiningRoomWindows"), PIN_CONTACT_DINING_RM_WNDW, LOW, true);
  144. static st::IS_Contact sensor3(F("LivingRoomPatioDoor"), PIN_CONTACT_LVNG_PATIO_DOOR, LOW, true);
  145. static st::IS_Contact sensor4(F("KitchenNookWindows"), PIN_CONTACT_KTCH_NOOK_WNDW, LOW, true);
  146. static st::IS_Contact sensor5(F("SunRoomPatioDoor"), PIN_CONTACT_SUN_ROOM_DOOR, LOW, true);
  147. static st::IS_Contact sensor6(F("SunRoomWindows"), PIN_CONTACT_SUN_ROOM_WNDW, LOW, true);
  148. static st::IS_Contact sensor7(F("Bedroom1Windows"), PIN_CONTACT_BEDROOM1_WNDW, LOW, true);
  149. static st::IS_Contact sensor8(F("Bedroom2Windows"), PIN_CONTACT_BEDROOM2_WNDW, LOW, true);
  150. static st::IS_Contact sensor9(F("GarageDoor"), PIN_CONTACT_GARAGE_DOOR, LOW, true);
  151.  
  152. //Executors
  153. //static st::EX_Switch executor1(F("sampleEX"), PIN_sampleEX, LOW, true);
  154.  
  155.  
  156. //*****************************************************************************
  157. // Configure debug print output from each main class
  158. // -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
  159. // to prevent communication conflicts with the ST Shield communications
  160. //*****************************************************************************
  161. st::Everything::debug = true;
  162. st::Executor::debug = true;
  163. st::Device::debug = true;
  164. st::PollingSensor::debug = true;
  165. st::InterruptSensor::debug = true;
  166.  
  167. //*****************************************************************************
  168. //Initialize the "Everything" Class
  169. //*****************************************************************************
  170.  
  171. //Initialize the optional local callback routine (safe to comment out if not desired)
  172. st::Everything::callOnMsgSend = callback;
  173.  
  174. //Create the SmartThings Thingshield Communications Object based on Arduino Model
  175. #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MINI) //Arduino UNO, NANO, MINI
  176. st::Everything::SmartThing = new st::SmartThingsThingShield(pinRX, pinTX, st::receiveSmartString); //Use Software Serial
  177. #elif defined(ARDUINO_AVR_LEONARDO) //Arduino Leonardo
  178. st::Everything::SmartThing = new st::SmartThingsThingShield(&Serial1, st::receiveSmartString); //Use Hardware Serial
  179. #elif defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) //Arduino MEGA 1280 or 2560
  180. st::Everything::SmartThing = new st::SmartThingsThingShield(&Serial3, st::receiveSmartString); //Use Hardware Serial
  181. #else
  182. //assume user is using an UNO for the unknown case
  183. st::Everything::SmartThing = new st::SmartThingsThingShield(pinRX, pinTX, st::receiveSmartString); //Software Serial
  184. #endif
  185.  
  186. //Run the Everything class' init() routine which establishes communications with SmartThings
  187. st::Everything::init();
  188.  
  189. //*****************************************************************************
  190. //Add each sensor to the "Everything" Class
  191. //*****************************************************************************
  192. st::Everything::addSensor(&sensor1);
  193. st::Everything::addSensor(&sensor2);
  194. st::Everything::addSensor(&sensor3);
  195. st::Everything::addSensor(&sensor4);
  196. st::Everything::addSensor(&sensor5);
  197. st::Everything::addSensor(&sensor6);
  198. st::Everything::addSensor(&sensor7);
  199. st::Everything::addSensor(&sensor8);
  200. st::Everything::addSensor(&sensor9);
  201.  
  202. //*****************************************************************************
  203. //Add each executor to the "Everything" Class
  204. //*****************************************************************************
  205. //st::Everything::addExecutor(&executor1);
  206.  
  207. //*****************************************************************************
  208. //Initialize each of the devices which were added to the Everything Class
  209. st::Everything::initDevices();
  210. //*****************************************************************************
  211. }
  212.  
  213. //******************************************************************************************
  214. //Arduino Loop() routine
  215. //******************************************************************************************
  216. void loop()
  217. {
  218. //*****************************************************************************
  219. //Execute the Everything run method which takes care of "Everything"
  220. //*****************************************************************************
  221. st::Everything::run();
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement